SQL Server級(jí)聯(lián)刪除的實(shí)現(xiàn)
作者:佚名
SQL Server中的級(jí)聯(lián)刪除用于在刪除主表時(shí),自動(dòng)刪除副表(外鍵約束)相應(yīng)內(nèi)容,下面就讓我們一起了解一下級(jí)聯(lián)刪除的實(shí)現(xiàn)過程。
如果要實(shí)現(xiàn)SQL Server級(jí)聯(lián)刪除,應(yīng)該如何操作呢?下面就為您介紹SQL Server級(jí)聯(lián)刪除的方法,如果您對(duì)SQL Server級(jí)聯(lián)刪除方面感興趣的話,不妨一看。
SQL Server級(jí)聯(lián)刪除功能:在刪除主表時(shí),自動(dòng)刪除副表(外鍵約束)相應(yīng)內(nèi)容
刪除包含主鍵值的行的操作,該值由其它表的現(xiàn)有行中的外鍵列引用。在級(jí)聯(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, --真實(shí)姓名
- userId int,
- foreign key (userId) references UserInfo(UserId) on delete cascade
【編輯推薦】
責(zé)任編輯:段燃
來源:
互聯(lián)網(wǎng)