sql串聯刪除

來源:互聯網
上載者:User

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

刪除包含主索引值的行的操作,該值由其它表的現有行中的外鍵列引用。在串聯刪除中,還刪除其外索引值引用刪除的主索引值的所有行。

如:

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)

sqlserver 支援串聯更新和刪除

oracle 只支援串聯刪除

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:更新級聯

00

0

(請您對文章做出評價)

此時:Delete From UserInfo Where UserId = 1 就可刪除UserInfo表和UserDetails表的UserId=1 的內容 

其它相關:
      
轉自:http://www.cnblogs.com/jyshi/

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

刪除包含主索引值的行的操作,該值由其它表的現有行中的外鍵列引用。在串聯刪除中,還刪除其外索引值引用刪除的主索引值的所有行。

如:

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)

sqlserver 支援串聯更新和刪除

oracle 只支援串聯刪除

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:更新級聯

00

0

(請您對文章做出評價)

此時:Delete From UserInfo Where UserId = 1 就可刪除UserInfo表和UserDetails表的UserId=1 的內容 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.