SQLServer外键中的DELETECASCADE和
UPDATECASCADE
In this article, we will review on DELETE CASCADE AND UPDATE CASCADE rules in SQL Server foreign key with different examples.
在本⽂中,我们将使⽤不同的⽰例回顾SQL Server外键中的DELETE CASCADE和UPDATE CASCADE规则。
DELETE CASCADE: When we create a foreign key using this option, it deletes the referencing rows in the child table when the referenced row is deleted in the parent table which has a primary key.
DELETE CASCADE :当我们使⽤此选项创建外键时,当在具有主键的⽗表中删除引⽤⾏时,它将删除⼦表中的引⽤⾏。
UPDATE CASCADE: When we create a foreign key using UPDATE CASCADE the referencing rows are updated in the child table when the referenced row is updated in the parent table which has a primary key.
UPDATE CASCADE:当我们使⽤UPDATE CASCADE创建外键时,在具有主键的⽗表中更新引⽤⾏时,在⼦表中更新引⽤⾏。
We will be discussing the following topics in this article:
我们将在本⽂中讨论以下主题:
1. Creating DELETE and UPDATE CASCADE rule in a foreign key using SQL Server management studio
使⽤SQL Server Management Studio在外键中创建DELETE和UPDATE CASCADE规则
2. Creating DELETE CASCADE and UPDATE CASCADE rule in a foreign key using T-SQL script
使⽤T-SQL脚本在外键中创建DELETE CASCADE和UPDATE CASCADE规则
3. Triggers on a table with DELETE or UPDATE cascading foreign key
使⽤DELETE或UPDATE级联外键在表上触发
Let us see how to create a foreign key with DELETE and UPDATE CASCADE rules along with few e
xamples.
让我们看看如何使⽤DELETE和UPDATE CASCADE规则创建外键以及⼀些⽰例。
使⽤DELETE和UPDATE CASCADE规则创建外键 (Creating a foreign key with DELETE and UPDATE CASCADE rules)
Using the SQL Server Management Studio GUI:
使⽤SQL Server Management Studio GUI:
Login to the SQL Server using SQL Server Management Studio, Navigate to the Keys folder in the child table. Right click on the Keys folder and select New Foreign Key.
使⽤SQL Server Management Studio登录到SQL Server,导航到⼦表中的Keys⽂件夹。 右键单击“ 密钥”⽂件夹,然后选择“ 新建外键” 。
Edit table and columns specification by clicking … as shown in the below image.
通过单击…编辑表和列的规格 ,如下图所⽰。
Select the parent table and the primary key column in the parent table. select the foreign key column in the child table. Click on OK. Please refer to the below sample image.
选择⽗表和⽗表中的主键列。 在⼦表中选择外键列。 单击确定。 请参考下⾯的⽰例图⽚。
在INSERT和UPDATE规范中,选择Cascade作为删除规则。
Click on Close and save the table in the designer. Click Yes in the warning message window.
单击关闭,然后将表保存在设计器中。 在警告消息窗⼝中单击“ 是 ”。
Once you click on Yes, a foreign key with delete rule is created. Similarly, we can create a foreign key with UPDATE CASCADE rule by selecting CASCADE as an action for the update rule in INSERT and UPDATE specifications.
单击“ 是”后,将创建带有删除规则的外键。 类似地,我们可以通过选择CASCADE作为INSERT和UPDATE规范中的更新规则的动作来使⽤UPDATE CASCADE规则创建外键。
Using T-SQL:
使⽤T-SQL:
sql中update什么意思
Please refer to the below T-SQL script which creates a parent, child table and a foreign key on the child table with DELETE CASCADE rule.
请参考下⾯的T-SQL脚本,该脚本使⽤DELETE CASCADE规则在⼦表上创建⽗表,⼦表和外键。
CREATE TABLE Countries
(CountryID INT PRIMARY KEY,
CountryName VARCHAR(50),
CountryCode VARCHAR(3))
CREATE TABLE States
(StateID INT PRIMARY KEY,
StateName VARCHAR(50),
StateCode VARCHAR(3),
CountryID INT)
ALTER TABLE [dbo].[States] WITH CHECK ADD CONSTRAINT [FK_States_Countries] FOREIGN KEY([CountryID])
REFERENCES [dbo].[Countries] ([CountryID])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[States] CHECK CONSTRAINT [FK_States_Countries]
GO
Insert some sample data using below T-SQL script.
使⽤下⾯的T-SQL脚本插⼊⼀些⽰例数据。
INSERT INTO Countries VALUES (1,'United States','USA')
INSERT INTO Countries VALUES (2,'United Kingdom','UK')
INSERT INTO States VALUES (1,'Texas','TX',1)
INSERT INTO States VALUES (2,'Arizona','AZ',1)
Now I deleted a row in the parent table with CountryID =1 which also deletes the rows in the child table which has CountryID =1.
现在,我删除了具有CountryID = 1的⽗表中的⾏,这也删除了具有CountryID = 1的⼦表中的⾏。
Please refer to the below T-SQL script to create a foreign key with UPDATE CASCADE rule.
请参考下⾯的T-SQL脚本,以使⽤UPDATE CASCADE规则创建外键。
CREATE TABLE Countries
(CountryID INT PRIMARY KEY,
CountryName VARCHAR(50),
CountryCode VARCHAR(3))
CREATE TABLE States
(StateID INT PRIMARY KEY,
StateName VARCHAR(50),
StateCode VARCHAR(3),
CountryID INT)
GO
INSERT INTO Countries VALUES (1,'United States','USA')
INSERT INTO Countries VALUES (2,'United Kingdom','UK')
INSERT INTO States VALUES (1,'Texas','TX',1)
INSERT INTO States VALUES (2,'Arizona','AZ',1)
GO
ALTER TABLE [dbo].[States] WITH CHECK ADD CONSTRAINT [FK_States_Countries] FOREIGN KEY([CountryID])
REFERENCES [dbo].[Countries] ([CountryID])
ON UPDATE CASCADE
GO
ALTER TABLE [dbo].[States] CHECK CONSTRAINT [FK_States_Countries]
GO
Now update CountryID in the Countries for a row which also updates the referencing rows in the child table States.现在,将“国家/地区”中的CountryID更新为⼀⾏,这还将更新⼦表“状态”中的引⽤⾏。
UPDATE Countries SET CountryID =3 where CountryID=1
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论