數(shù)據(jù)庫點滴之SQL級聯(lián)刪除
來源:易賢網(wǎng) 閱讀:1588 次 日期:2015-09-08 17:19:51
溫馨提示:易賢網(wǎng)小編為您整理了“數(shù)據(jù)庫點滴之SQL級聯(lián)刪除”,方便廣大網(wǎng)友查閱!

功能:在刪除主表時,自動刪除副表(外鍵約束)相應內容

刪除包含主鍵值的行的操作,該值由其它表的現(xiàn)有行中的外鍵列引用。在級聯(lián)刪除中,還刪除其外鍵值引用刪除的主鍵值的所有行。

如:

create database temp

go

use temp

go

create 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)

SQL Server支持級聯(lián)更新和刪除

Oracle只支持級聯(lián)刪除

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:刪除級聯(lián)

on update:更新級聯(lián)

更多信息請查看IT技術專欄

更多信息請查看數(shù)據(jù)庫
易賢網(wǎng)手機網(wǎng)站地址:數(shù)據(jù)庫點滴之SQL級聯(lián)刪除
關于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點

版權所有:易賢網(wǎng)