SQL 会创建多表及多表的关系:
需求:分类表和商品表之间是不是有关系? 如果有关系,在数据库中如何表示这种关系
create table category( cid int primary key auto_increment, cname varchar(10), cdesc varchar(31) ); insert into category values(null,'手机数码','电子产品,黑马生产'); insert into category values(null,'鞋靴箱包','江南皮鞋厂倾情打造'); insert into category values(null,'香烟酒水','黄鹤楼,茅台,二锅头'); insert into category values(null,'酸奶饼干','娃哈哈,蒙牛酸酸乳'); insert into category values(null,'馋嘴零食','瓜子花生,八宝粥,辣条'); select * from category; select cname,cdesc from category;
所有商品:
1.商品ID
2.商品名称
3.商品的价格
4.生产日期
5.商品分类ID
商品和商品分类 : 所属关系
create table product( pid int primary key auto_increment, pname varchar(10), price double, pdate timestamp, cno int );
insert into product values(null,'小米mix4',998,null,1); insert into product values(null,'锤子',2888,null,1); insert into product values(null,'阿迪王',99,null,2); insert into product values(null,'老村长',88,null,3); insert into product values(null,'劲酒',35,null,3); insert into product values(null,'小熊饼干',1,null,4); insert into product values(null,'卫龙辣条',1,null,5); insert into product values(null,'旺旺大饼',1,null,5);
//插入数据会失败 insert into product values(null,'充气的',1,null,12);
多表之间的建表原则:
一对多:商品和分类
建表原则:在多的一方添加一个外键,指向一的一方的主键
多对多:老师和学生,学生和课程
建表原则:建立一张中间表,将多对多的关系,拆分成一对多的管理,中间表至少要有两个外键。分别指向原来的那两张表
一对一:班级和班长,公民和身份证,国家和国旗
建表原则:
将一对一的情况,当作是一对多情况处理,在任意一张表添加一个外键,并且这个外键要唯一,指向另外一张表
直接将两张表合并成一张表
将两张表的主键建立起连接,让两张表里面的主键相等
网上商城表实例的分析: 用户购物流程:
用户表 (用户的ID,用户名,密码,手机)
create table user( uid int primary key auto_increment, username varchar(31), password varchar(31), phone varchar(11) ); insert into user values(1,'zhangsan','123','13811118888');
订单表 (订单编号,总价,订单时间 ,地址,外键用户的ID)
create table orders( oid int primary key auto_increment, sum int not null, otime timestamp, address varchar(100), uno int, foreign key(uno) references user(uid) ); insert into orders values(1,200,null,'黑马前台旁边小黑屋',1); insert into orders values(2,250,null,'黑马后台旁边1702',1);
商品表 (商品ID, 商品名称,商品价格,外键cno)
create table product( pid int primary key auto_increment, pname varchar(10), price double, cno int, foreign key(cno) references category(cid) ); insert into product values(null,'小米mix4',998,1); insert into product values(null,'锤子',2888,1); insert into product values(null,'阿迪王',99,2); insert into product values(null,'老村长',88,3); insert into product values(null,'劲酒',35,3); insert into product values(null,'小熊饼干',1,4); insert into product values(null,'卫龙辣条',1,5); insert into product values(null,'旺旺大饼',1,5);
订单项: 中间表(订单ID,商品ID,商品数量,订单项总价)
create table orderitem( ono int, pno int, foreign key(ono) references orders(oid), foreign key(pno) references product(pid), ocount int, subsum double );
给1号订单添加商品 200块钱的商品
insert into orderitem values(1,7,100,100); insert into orderitem values(1,8,101,100);
给2号订单添加商品 250块钱的商品 ()
insert into orderitem values(2,5,1,35); insert into orderitem values(2,3,3,99);
商品分类表(分类ID,分类名称,分类描述)
create table category( cid int primary key auto_increment, cname varchar(15), cdesc varchar(100) ); insert into category values(null,'手机数码','电子产品,黑马生产'); insert into category values(null,'鞋靴箱包','江南皮鞋厂倾情打造'); insert into category values(null,'香烟酒水','黄鹤楼,茅台,二锅头'); insert into category values(null,'酸奶饼干','娃哈哈,蒙牛酸酸乳'); insert into category values(null,'馋嘴零食','瓜子花生,八宝粥,辣条');