SQL Server級聯刪除的實現
作者:佚名
SQL Server中的級聯刪除用于在刪除主表時,自動刪除副表(外鍵約束)相應內容,下面就讓我們一起了解一下級聯刪除的實現過程。
如果要實現SQL Server級聯刪除,應該如何操作呢?下面就為您介紹SQL Server級聯刪除的方法,如果您對SQL Server級聯刪除方面感興趣的話,不妨一看。
SQL Server級聯刪除功能:在刪除主表時,自動刪除副表(外鍵約束)相應內容
刪除包含主鍵值的行的操作,該值由其它表的現有行中的外鍵列引用。在級聯刪除中,還刪除其外鍵值引用刪除的主鍵值的所有行。
如:
- 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
【編輯推薦】
責任編輯:段燃
來源:
互聯網