sql delete同時刪除多表相關聯記錄
sqlserver 支援級聯更新和刪除
oracle 只支援級聯刪除
刪除包含主鍵值的行的操作,該值由其它表的現有行中的外來鍵列引用。在級聯刪除中,還刪除其外鍵值引用刪除的主鍵值的所有行。
如:create database temp
gouse temp
gocreate table userinfo
(userid int identity(1,1) primary key ,
username varchar(20), --使用者名稱
password varchar(20) not null --密碼
)create table userdetails
(id int identity(1,1) primary key,
name varchar(50) not null, --真實姓名
userid int,
foreign key (userid) references userinfo(userid) on delete cascade
)insert userinfo values ('ly','jeff')
insert userinfo values('wzq','wzqwzq')
insert userinfo values('lg','lglg')
insert userdetails values('李四',1)
insert userdetails values('王五',2)
insert userdetails values('劉六',3)
alter table 表名
add constraint 外鍵名
foreign key(欄位名) references 主表名(欄位名)
on delete cascade
語法:foreign key
(column[,...n])
references referenced_table_name[(ref_column[,...n])]
[on delete cascade]
[on update cascade]
注釋:column:列名
referenced_table_name:外來鍵參考的主鍵表名稱
ref_name:外來鍵要參考的表的主鍵列
on delete:刪除級聯
on update:更新級聯
000
(請您對文章做出評價)此時:delete from userinfo where userid = 1 就可刪除userinfo表和userdetails表的userid=1 的內容
看一下mysql資料庫教程怎麼操作多表刪除呢,
有時我們可以直接用delete 來刪除
delete 語法:
delete [low_priority] [quick] from table_name
[where where_definition]
[order by ...]
[limit rows]
ordelete [low_priority] [quick] table_name[.*] [, table_name[.*] ...]
from table-references
[where where_definition]
ordelete [low_priority] [quick]
from table_name[.*] [, table_name[.*] ...]
using table-references
[where where_definition]
示例**為:
1. 刪除乙個表中的資料
delete from department where name='asset management';
2. 刪除兩個表中的資料
delete employee, employeeskills
from employee, employeeskills, department
where employee.employeeid = employeeskills.employeeid
and employee.departmentid = department.departmentid
and department.name='finance';
3. 刪除兩個表中的資料,用using語法
delete from employee, employeeskills
using employee, employeeskills, department
where employee.employeeid = employeeskills.employeeid
and employee.departmentid = department.departmentid
and department.name='finance';
mysql delete語句刪除多張表
功能介紹 來從多個表中刪除資料。mysql允許delete語句中使用inner join子句來從表中刪除和另乙個表中的匹配的行記錄。從符合指定條件的t1和t2表中刪除行記錄 例項 從符合指定條件的t1和t2表中刪除行記錄 delete t1,t2 from t1 inner join t2 on t...
MYSQL delete 從多人表中刪除
語法 1 delete table list a from table list b where bool expression 從delete table list a 刪除 bool expression 為true 的行。例子 1 語法 2 delete from t1,t2 using t1...
MYSQL delete效能優化!
優化項 1 low priority 當沒有連線文章表時才進行刪除操作。delete low priority from t 優化項 2 quick 當刪除行時並不刪除行的索引 如果再次插入這一行,或是也它相近的行時 就可以重用索引。delete quick from t 好處 快 再怎麼說也少刪除...