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,"无备注");`
原文地址:https://www.cnblogs.com/liu-en-ci/p/7449184.html