Java項目專欄之數據庫建表


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,"無備注");`


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM