sql檔案:
set foreign_key_checks=0;
-- ----------------------------
-- table structure for stu_tea
-- ----------------------------
drop
table
ifexists
`stu_tea`;
create
table
`stu_tea` (
`stu_id`
int(11) not
null,
`tea_id`
int(11) not
null,
primary
key (`stu_id`,`tea_id`),
key`stu_tea_ibfk_2` (`tea_id`),
constraint
`stu_tea_ibfk_2`
foreign
key (`tea_id`) references
`teacher` (`id`),
constraint
`stu_tea_ibfk_1`
foreign
key (`stu_id`) references
`student` (`id`)
) engine=innodb default charset=utf8;
-- ----------------------------
-- records of stu_tea
-- ----------------------------
insert
into
`stu_tea`
values ('4', '2');
-- ----------------------------
-- table structure for student
-- ----------------------------
drop
table
ifexists
`student`;
create
table
`student` (
`id`
int(11) not
null auto_increment,
`name`
varchar(255) default
null,
primary
key (`id`)
) engine=innodb auto_increment=101
default charset=utf8;
-- ----------------------------
-- records of student
-- ----------------------------
insert
into
`student`
values ('4', '申達股份');
insert
into
`student`
values ('6', '的方式到');
-- ----------------------------
-- table structure for teacher
-- ----------------------------
drop
table
ifexists
`teacher`;
create
table
`teacher` (
`id`
int(11) not
null auto_increment,
`name`
varchar(255) default
null,
primary
key (`id`)
) engine=innodb auto_increment=3
default charset=utf8;
-- ----------------------------
-- records of teacher
-- ----------------------------
insert
into
`teacher`
values ('1', '張老師');
insert
into
`teacher`
values ('2', '***');
student和stu_tea兩張表內關聯刪除:
delete st,s
from
student s
inner
join stu_tea st on s.id = st.stu_id
分析:
刪除順序:根據內關聯的前後順序進行刪除,上述sql語句先刪除表student ,後刪除表stu_tea,不是按照st,s
這個抒寫順序刪除的
student和stu_tea兩張表外關聯刪除:
delete s,st
from
stu_tea st
left
join student s on s.id = st.stu_id
分析:
刪除順序:根據外關聯的前後順序進行刪除,左關聯先刪除左邊的關聯表,右關聯先刪除右邊的表,上述sql語句左關聯先刪除表stu_tea,後刪除表student;
刪除順序對於外來鍵約束restrict、no action,刪除順序不對可能導致sql無法執行;
mysql 多表關聯刪除
兩張表關聯刪除 delete a,b from table1 a inner join table2 b on a.id b.aid where a.id 1 或者也可以 delete a,b from table1 a,table2 b where a.id b.aid and a.id 1 三張...
Mysql 多表關聯修改與刪除
資料庫表設計過程中,會把關鍵的字段 一列或多列 提取出來,建立多個維度的資訊表,供業務功能使用。假如我們在需要在a維度表中的滿足非關鍵字段部分條件欄位的記錄,需要在b維度表中修改列值,我們就會使用到多表關聯update。使用方式 update tablea a 直接left join 關聯表 還有表...
mysql 資料庫 多表關聯刪除
1 從資料表t1中把那些id值在資料表t2裡有匹配的記錄全刪除掉 delete t1 from t1,t2 where t1.id t2.id 或delete from t1 using t1,t2 where t1.id t2.id 2 從兩個表中找出相同記錄的資料並把兩個表中的資料都刪除掉 de...