赞
踩
create table depart( id int auto_increment primary key comment 'ID', name varchar(50) not null comment '部门名' )comment '部门'; insert into depart(id, name)values (1,'研发部'),(2,'市场部'),(3,'财务部'),(4,'销售部'),(5,'总经部'); create table personal( id int auto_increment primary key comment 'ID', name varchar (50) not null comment '姓名', age int comment '年龄', job varchar(20) comment '职位', salary int comment '薪资', woketime date comment'入职时间', lender int comment '直属领导ID', depart_id int comment '部门ID' )comment '员工表'; insert into personal(id, name, age, job, salary, woketime, lender, depart_id) values (1,'天盗',66,'总裁',20000,'2000-1-1',null,5), (2,'天盗时',20,'项目经理',12000,'2002-12-1',1,1), (3,'天盗第',33,'开发',15000,'2021-3-23',2,1), (4,'天盗岁',48,'开发',16000,'2004-11-15',2,1), (5,'天盗大',43,'开发',2600,'2002-10-12',3,1), (6,'天盗就',19,'程序员鼓励师',13000,'2008-3-1',2,1); "外键链接"并且进行指定删除与更新行为 1 alter table personal add constraint fk_personal_depart_id foreign key (depart_id)references depart(id) on update cascade on delete cascade ; -- 链接外键,并且支持连接后,修改父表的字段,与此同时子表外键连接处也要发生变化 2 alter table personal add constraint fk_personal_depart_id foreign key (depart_id)references depart(id) on update set null on delete set null ;
1图(更改数据)
1图(删除数据)
删除ID为5 经理部 这一行
2图(删除数据)
删除ID为9 研发部
变化前
变化后
图形化界面修改外键方法:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。