Java項目專欄之數據庫建表
數據庫建表前期準備
1. 安裝mysql:數據庫語言,語法和sql server差不太多,若是習慣於sql server能夠不用mysql。
2. 安裝navicat:可視化數據庫工具,簡化你的建表操做。
正式建表
1. 數據庫的配置我就不一一講解了,直接開始建表吧,建表先建主外鍵約束少或者沒有的表。
2. 建立數據庫,命名爲:shop_manage_system.這裏要注意的是,在團隊開發過程當中,團隊成員數據庫的字符編碼要同樣,不然整合會出大問題。
3. 建立供應商表(tb_supply)
* `CREATE TABLE tb_supply (
sup_id INTEGER not null PRIMARY KEY auto_increment, -- 供應商編號,自增
sup_name VARCHAR(20) not null, -- 供應商名稱
sup_address VARCHAR(20), -- 供應商地址
sup_linkMan VARCHAR(20), -- 供應商聯繫人
sup_phone VARCHAR(11), -- 供應商聯繫電話
sup_status tinyint not null, -- 合做狀態(0:保持合做1:解除合做)
sup_mark varchar(50)-- 備註
);`
4. 建立職務信息表(tb_position)
* `create table tb_position(
posi_id Integer not null PRIMARY KEY auto_increment, -- 職務編號,主鍵 (1:採購員 2:銷售員 3:倉庫管理員 4:管理員)
posi_name Varchar(20) not null, -- 職務名稱
posi_introduction Varchar(50) -- 職務簡介
);`
5. 建立員工表(tb_employee)
* `CREATE table tb_employee(
emp_id Integer not null PRIMARY KEY auto_increment, -- 員工編號
emp_password varchar(20) not null, -- 帳號密碼
emp_name Varchar(20) not null, -- 員工姓名
emp_sex tinyInt not null, -- 員工性別(0:女 1:男)
emp_position_id Integer not null, -- 員工職務編號,外鍵
emp_phone Varchar(11) not null, -- 員工聯繫電話
emp_birthday Date , -- 員工出生日期
emp_salary INTEGER not null, -- 員工工資
emp_status tinyInt not null, -- 狀態(1:在職 0:開除)
emp_mark Varchar(50), -- 備註
constraint fk_type_position foreign key (emp_position_id) references tb_position(posi_id) -- 職位編號是員工表的外鍵
);`
6. 建立倉庫表(tb_storage)
* `create table tb_storage(
sto_id Integer not null PRIMARY KEY auto_increment, -- 主鍵,倉庫編號
sto_name varchar(20) not null, -- 倉庫名稱
sto_empId INTEGER not null, -- 員工編號,外鍵
sto_address varchar(20) , -- 倉庫地址
sto_mark varchar(50) , -- 備註
constraint fk_type_employee foreign key (sto_empId) references tb_employee(emp_id) -- 員工編號是倉庫表的外鍵
;`
7. 建立商品表(tb_good)
* `create table tb_good(
goods_id Integer not null PRIMARY KEY auto_increment, -- 主鍵,商品編號
goods_name varchar(20) not null, -- 商品名稱
goods_units varchar(20) , -- 商品單位
goods_size varchar(10) , -- 商品規格大小
goods_purPrice double not null, -- 商品進價
goods_sellPrice double not null, -- 商品售價
goods_number INTEGER not null, -- 商品數量
goods_stoId INTEGER not null, -- 外鍵,倉庫編號
goods_keepDays INTEGER not null, -- 商品保質期
goods_minNumber INTEGER not null, -- 最低庫存
goods_mark varchar(50), -- 備註
constraint fk_type_storage foreign key (goods_stoId) references tb_storage(sto_id) -- 倉庫編號是商品表的外鍵
);
alter table tb_good auto_increment = 10001; -- 設置商品表的商品編號從10001開始遞增`
8. 建立採購訂單表(tb_purchaseOrder)
* `create table tb_purchaseOrder(
pur_id Integer not null PRIMARY KEY auto_increment, -- 採購訂單號,主鍵,自增
pur_supplyId Integer not null, -- 供貨商編號,外鍵
pur_date Date not null, -- 採購日期
pur_pay double not null, -- 支付總金額
pur_empId Integer not null, -- 員工編號,外鍵
pur_status tinyInt not null, -- 是否審覈(0:未審覈1:已審覈經過 2:審覈未經過退回採購員)
pur_mark Varchar(50), -- 備註
constraint fk_type_supply foreign key (pur_supplyId) references tb_supply (sup_id), -- 供應商編號是採購訂單表的外鍵
constraint fk_type_employee2 foreign key (pur_empId) references tb_employee(emp_id) -- 員工編號是採購訂單表的外鍵
);
alter table tb_purchaseOrder auto_increment = 20171001; -- 設置採購訂單表編號從20171001開始遞增`
9. 建立採購訂單詳情表(tb_purDetail)
* `create table tb_purDetail(
pDet_id Integer not null PRIMARY KEY auto_increment, -- 詳單表編號,主鍵,自增
pDet_purId Integer not null, -- 採購訂單表編號,外鍵
pDet_goodId Integer not null, -- 商品編號
pDet_number Integer not null, -- 採購數量
pDet_goodPrice double not null, -- 每種商品的進價總價格
pDet_status tinyInt not null, -- 採購狀態(0:入庫1:未入庫)
pDet_mark Varchar(50), -- 備註
constraint fk_type_purchaseOrder foreign key (pDet_purId) references tb_purchaseOrder(pur_id), -- 採購訂單表編號是採購訂單詳情表的外鍵
constraint fk_type_purchaseOrder2 foreign key (pDet_goodId) references tb_good(goods_id) -- 商品編號是採購訂單表的外鍵
);`
10. 建立採購計劃表(tb_purchasePlan)
* `create table tb_purchasePlan(
plan_id Integer not null PRIMARY KEY auto_increment, -- 主鍵,採購計劃編號
plan_date Date not null, -- 計劃日期
plan_empId Integer not null, -- 外鍵,員工編號
plan_mark varchar(50), -- 備註
constraint fk_type_employee1 foreign key (plan_empId) references tb_employee(emp_id) -- 員工編號是採購計劃表的外鍵
);
alter table tb_purchasePlan auto_increment = 20173001; -- 設置採購計劃表編號從20173001開始遞增`
11. 建立採購計劃詳單表(tb_purPlanDetail)
* `create table tb_purPlanDetail(
planDet_id Integer not null PRIMARY KEY auto_increment, -- 計劃詳單表編號,主鍵,自增
planDet_purId Integer not null, -- 採購計劃表編號,外鍵
planDet_goodId Integer not null, -- 商品編號
planDet_number Integer not null, -- 採購數量
planDet_goodPrice double not null, -- 每種商品的進價總價格
planDet_mark Varchar(50), -- 備註
constraint fk_type_purchasePlan foreign key (planDet_purId) references tb_purchasePlan(plan_id), -- 採購計劃編號是採購計劃詳單表的外鍵
constraint fk_type_purchasePlan2 foreign key (planDet_goodId) references tb_good(goods_id) -- 商品編號是採購計劃表的外鍵
);`
12. 建立銷售訂單表(tb_sellOrder)
* `CREATE TABLE tb_sellOrder (
sell_id INTEGER NOT NULL PRIMARY KEY auto_increment, -- 銷售訂單編號,主鍵
sell_empId INTEGER NOT NULL,-- 員工編號,外鍵
sell_date Date NOT NULL, -- 銷售日期
sell_profit double NOT NULL, -- 銷售總金額
sell_status INTEGER not null, -- 銷售狀態 (0:已銷售,1:已退貨,2:部分退貨)
sell_mark varchar(50) DEFAULT NULL, -- 備註
constraint fk_type_employee3 foreign key (sell_empId) references tb_employee(emp_id) -- 員工編號是銷售訂單表的外鍵
);
alter table tb_sellOrder auto_increment = 20172001; --設置銷售訂單表編號從20172001開始`
13. 建立銷售訂單詳情表(tb_sellDeteil)
* `CREATE TABLE tb_sellDetail (
sDet_id INTEGER NOT NULL PRIMARY KEY auto_increment,-- 銷售訂單編號,主鍵
sDet_sellId INTEGER NOT NULL, -- 銷售訂單號,外鍵
sDet_goodId INTEGER NOT NULL, -- 商品編號
sDet_number INTEGER NOT NULL, -- 銷售數量
sDet_goodPrice DOUBLE NOT NULL,-- 每件商品的銷售總金額
sDet_status tinyint NOT NULL, -- 銷售狀態(0:已銷售 1:已退貨)
sDet_mark varchar(50), -- 備註
constraint fk_type_sellOrder foreign key (sDet_sellId) references tb_sellOrder(sell_id), -- 銷售訂單表編號是銷售訂單詳情表的外鍵
constraint fk_type_sellOrder2 foreign key (sDet_goodId) references tb_good(goods_id) -- 商品編號是銷售訂單表的外鍵
);`
插入數據
1. 插入供應商數據
* `insert into tb_supply(sup_name,sup_address,sup_linkMan,sup_phone,sup_status,sup_mark)VALUES
("阿里巴巴供應商","浙江杭州","馬雲","15096661111",1,"阿里巴巴首席執行官"),
("百度供應商","北京朝陽區","李彥宏","15096662222",1,"百度CEO"),
("騰訊供應商","廣東深圳","馬化騰","15096663333",1,"騰訊CEO"),
("京東供應商","北京朝陽區","劉強東","15096663333",1,"京東大老闆");`
2. 插入職務信息表數據
* `insert into tb_position(posi_name ,posi_introduction)VALUES
("進貨員","負責採購進貨"),
("銷售員","負責銷售商品"),
("倉管員","負責管理倉庫"),
("財務員","負責數錢");`
3. 插入員工信息表數據
* `insert into tb_employee(emp_name,emp_sex ,emp_password ,emp_position_id ,emp_phone ,emp_birthday ,emp_salary ,emp_status ,emp_mark )VALUES
("熊主席",0,"123456",1,"13272131111","1999-10-10",30000,1,"國服第一採購員,平常剁手採購"),
("小潘潘",1,"123456",2,"13272132222","2000-10-10",20000,1,"國服第一銷售員,平常瘋狂銷售"),
("大佬",1,"123456",3,"13272133333","2010-10-10",1000000,1,"管的了倉庫,寫的了bug"),
("南哥",1,"123456",3,"13272134444","2003-10-10",2000000,1,"上得廳堂,下得廚房");`
4. 插入倉庫表數據
* `insert into tb_storage(sto_name ,sto_empId ,sto_address ,sto_mark )VALUES
("主倉庫",3,"美國紐約","小金庫"),
("飲料庫",3,"美國紐約","大倉庫"),
("酒庫",3,"美國紐約","中倉庫"),
("零食庫",3,"美國紐約","小倉庫");`
5. 插入商品信息表數據
* `insert into tb_good(goods_name ,goods_units ,goods_size ,goods_purPrice ,goods_sellPrice ,goods_number ,goods_stoId ,goods_keepDays ,goods_minNumber ,goods_mark )VALUES
("桃子","個","100g",2.5,3.5,20,1,10,5,"甜的不行"),
("李子","個","80g",3.5,4.5,20,1,10,5,"甜的不行"),
("蘋果","個","100g",2.5,3.5,20,1,10,5,"甜的不行"),
("冰紅茶","瓶","350g",2,5,20,2,60,5,"解渴的不行"),
("紅牛","瓶","350g",4,6,20,2,60,5,"解渴的不行"),
("伊利牛奶","瓶","350g",3,5,20,2,60,5,"解渴的不行"),
("82年拉菲","瓶","500g",1000,2000,20,3,100,5,"酒勁大的不行"),
("雞尾酒","瓶","500g",6,12,20,3,100,5,"酒勁大的不行"),
("瀏陽河","瓶","500g",500,700,20,3,100,5,"酒勁大的不行"),
("威龍","包","200g",3,5,100,4,20,5,"好吃的不行"),
("親嘴燒","包","50g",0.1,0.5,100,4,20,5,"好吃的不行"),
("辣子魚","包","100g",0.5,1,100,4,20,5,"好吃的不行");
insert into tb_good(goods_name ,goods_units ,goods_size ,goods_purPrice ,goods_sellPrice ,goods_number ,goods_stoId ,goods_keepDays ,goods_minNumber ,goods_mark )VALUES
("香蕉","個","100g",2.5,3.5,20,1,10,100,"甜的不行"),
("菠蘿","個","80g",3.5,4.5,20,1,10,100,"甜的不行"),
("西瓜","個","100g",2.5,3.5,20,1,10,100,"甜的不行");`
6. 插入採購訂單表數據
* `insert into tb_purchaseOrder(pur_supplyId ,pur_date ,pur_pay ,pur_empId ,pur_status ,pur_mark )VALUES
(1,"2017-8-12",0,1,0,"無備註"),
(2,"2015-8-12",0,1,0,"無備註"),
(3,"2014-8-12",0,1,0,"無備註"),
(4,"2015-8-12",0,1,0,"無備註"),
(1,"2016-8-12",0,1,0,"無備註"),
(2,"2013-8-12",0,1,0,"無備註"),
(3,"2017-8-12",0,1,0,"無備註"),
(4,"2014-8-12",0,1,0,"無備註"),
(2,"2017-8-12",0,1,0,"無備註"),
(1,"2014-8-12",0,1,0,"無備註"),
(2,"2013-8-12",0,1,0,"無備註"),
(3,"2013-8-12",0,1,0,"無備註");`
7. 插入採購訂單詳情表數據
* `insert into tb_purDetail(pDet_purId ,pDet_goodId ,pDet_number ,pDet_goodPrice ,pDet_status ,pDet_mark )VALUES
(20171001,10001,1,0,1,"無備註"),
(20171002,10002,1,0,1,"無備註"),
(20171003,10003,1,0,1,"無備註"),
(20171004,10004,1,0,1,"無備註"),
(20171005,10005,1,0,1,"無備註"),
(20171006,10006,1,0,1,"無備註"),
(20171007,10007,1,0,1,"無備註"),
(20171008,10008,1,0,1,"無備註"),
(20171009,10009,1,0,1,"無備註"),
(20171010,10010,1,0,1,"無備註"),
(20171001,10011,1,0,1,"無備註"),
(20171002,10012,1,0,1,"無備註");`
8. 插入採購計劃表數據
* `insert into tb_purchasePlan(plan_date ,plan_empId ,plan_mark)VALUES
("2016-10-10",1,"無備註"),
("2017-10-10",1,"無備註"),
("2015-10-10",1,"無備註"),
("2014-10-10",1,"無備註"),
("2013-10-10",1,"無備註"),
("2016-10-10",1,"無備註");`
9. 插入採購訂單詳情表數據
* `insert into tb_purPlanDetail(planDet_purId,planDet_goodId,planDet_number,planDet_goodPrice,planDet_mark)VALUES
(20173001,10001,2,0,"無"),
(20173001,10002,3,0,"無"),
(20173001,10003,4,0,"無"),
(20173001,10004,5,0,"無"),
(20173001,10005,6,0,"無"),
(20173001,10006,7,0,"無"),
(20173002,10001,4,0,"無"),
(20173002,10002,6,0,"無");`
10. 插入銷售訂單表數據
* `insert into tb_sellOrder (sell_empId ,sell_date ,sell_profit ,sell_status ,sell_mark )VALUES
(2,"2010-10-10",0,0,"無備註"),
(2,"2012-10-10",0,0,"無備註"),
(2,"2013-10-10",0,0,"無備註"),
(2,"2012-10-10",0,0,"無備註"),
(2,"2013-10-10",0,0,"無備註"),
(2,"2014-10-10",0,0,"無備註");`
11. 插入銷售訂單詳情表數據
* `insert into tb_sellDetail (sDet_sellId ,sDet_goodId ,sDet_number ,sDet_goodPrice ,sDet_status ,sDet_mark )VALUES
(20172001,10001,1,0,0,"無備註"),
(20172001,10002,2,0,0,"無備註"),
(20172001,10003,3,0,0,"無備註"),
(20172001,10004,4,0,0,"無備註"),
(20172001,10005,5,0,0,"無備註"),
(20172001,10006,6,0,0,"無備註"),
(20172002,10001,1,0,0,"無備註"),
(20172002,10002,1,0,0,"無備註"),
(20172002,10003,1,0,0,"無備註"),
(20172002,10004,1,0,0,"無備註"),
(20172003,10005,1,0,0,"無備註"),
(20172003,10006,1,0,0,"無備註"),
(20172004,10007,1,0,0,"無備註"),
(20172004,10008,1,0,0,"無備註"),
(20172004,10009,1,0,0,"無備註"),
(20172005,10002,1,0,0,"無備註"),
(20172005,10003,1,0,0,"無備註");`