标签:表操作 mil ack double table category reference into 技术
多表操作
1.分类表
create table category( cid varchar(32) primary key, cname varchar(100) ); 2.商品表 create table product( pid varchar(32) primary key, pname varchar(40), price double, category_id varchar(32) ); insert into category(cid,cname) values(‘c001‘,‘家电‘); insert into category(cid,cname) values(‘c002‘,‘服饰‘); insert into category(cid,cname) values(‘c003‘,‘化妆品‘); insert into product(pid,pname,price,category_id) values(‘p001‘,‘联想‘,‘5000‘,‘c001‘); insert into product(pid,pname,price,category_id) values(‘p002‘,‘海尔‘,‘5000‘,‘c001‘); insert into product(pid,pname,price,category_id) values(‘p003‘,‘雷神‘,‘5000‘,‘c001‘); insert into product(pid,pname,price,category_id) values(‘p004‘,‘JACK JONES‘,‘800‘,‘c002‘); insert into product(pid,pname,price,category_id) values(‘p005‘,‘真维斯‘,‘200‘,‘c002‘); insert into product(pid,pname,price,category_id) values(‘p006‘,‘花花公子‘,‘440‘,‘c002‘); insert into product(pid,pname,price,category_id) values(‘p007‘,‘劲霸‘,‘2000‘,‘c002‘); insert into product(pid,pname,price,category_id) values(‘p008‘,‘香奈儿‘,‘800‘,‘c003‘); insert into product(pid,pname,price,category_id) values(‘p009‘,‘相宜本草‘,‘200‘,‘c003‘);
3.联合主外键
alter table product add foreign key(category_id) references category(cid);
4.声明外键约束
要想删除主表中与从表有关联关系的数据,可以这么做:
delete from product where pid in (‘p008‘,‘p009‘)
标签:表操作 mil ack double table category reference into 技术
原文地址:https://www.cnblogs.com/sacai/p/10602752.html