MySQL级联删除和级联修改
1、新建主键table
1 create table demo1_zhujian ( 2 id int primary key auto_increment, 3 name varchar(10));
2、新建外键table
1 create table demo2_waijian ( 2 id int primary key auto_increment, 3 _id int not null default 0, 4 name varchar(10), 5 index (_id), 6 FOREIGN KEY (_id) REFERENCES demo1_zhujian(id) ON DELETE CASCADE ON UPDATE CASCADE); 7 8 create table demo3_waijian ( 9 id int primary key auto_increment, 10 _id int not null default 0, 11 name varchar(10), 12 index (_id), 13 FOREIGN KEY (_id) REFERENCES demo1_zhujian(id) ON DELETE CASCADE ON UPDATE CASCADE);
3、添加数据
1 insert into demo1_zhujian 2 (name) 3 values 4 ('11111'); 5 6 insert into demo2_waijian 7 (_id, name) 8 values 9 (1, 'demo2_1'); 10 11 insert into demo3_waijian 12 (_id, name) 13 values 14 (1, 'demo3_1');
4、查询数据
1 select * from demo1_zhujian; 2 select * from demo2_waijian; 3 select * from demo3_waijian;
5、修改主键数据
1 update demo1_zhujian set id=22 where id=1;
6、重复第4步
外键table中修改外键不行
7、主键table中删除数据
1 delete from demo1_zhujian where id=22;
8、重复第4步