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

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

刪除包含主鍵值的行的操作,該值由其它表的現(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ù)專欄

更多信息請查看數(shù)據(jù)庫
易賢網(wǎng)手機網(wǎng)站地址:數(shù)據(jù)庫點滴之SQL級聯(lián)刪除
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇剩?/div>
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)