2023年8月3日发(作者:)
SQLServer外键中的DELETECASCADE和UPDATECASCADEIn this article, we will review on DELETE CASCADE AND UPDATE CASCADE rules in SQL Server foreign key with differentexamples.在本⽂中,我们将使⽤不同的⽰例回顾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 whenthe referenced row is deleted in the parent table which has a primary CASCADE :当我们使⽤此选项创建外键时,当在具有主键的⽗表中删除引⽤⾏时,它将删除⼦表中的引⽤⾏。UPDATE CASCADE: When we create a foreign key using UPDATE CASCADE the referencing rows are updated in the childtable when the referenced row is updated in the parent table which has a primary 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 examples.让我们看看如何使⽤DELETE和UPDATE CASCADE规则创建外键以及⼀些⽰例。使⽤DELETE和UPDATE CASCADE规则创建外键 (Creating a foreign key with DELETE andUPDATE 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 onthe 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. Clickon OK. Please refer to the below sample image.选择⽗表和⽗表中的主键列。 在⼦表中选择外键列。 单击确定。 请参考下⾯的⽰例图⽚。In the INSERT and UPDATE specifications, select Cascade for the delete rule.在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 UPDATECASCADE 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:Please refer to the below T-SQL script which creates a parent, child table and a foreign key on the child table with DELETECASCADE 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 CASCADEGO
ALTER TABLE [dbo].[States] CHECK CONSTRAINT [FK_States_Countries]GOInsert 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 CASCADEGO
ALTER TABLE [dbo].[States] CHECK CONSTRAINT [FK_States_Countries]GONow 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=1Following is the T-SQL script which creates a foreign key with cascade as UPDATE and DELETE rules.以下是T-SQL脚本,该脚本创建带有级联的外键作为UPDATE和DELETE规则。ALTER TABLE [dbo].[States] WITH CHECK ADD CONSTRAINT [FK_States_Countries] FOREIGN KEY([CountryID])REFERENCES [dbo].[Countries] ([CountryID])ON UPDATE CASCADEON DELETE CASCADEGO
ALTER TABLE [dbo].[States] CHECK CONSTRAINT [FK_States_Countries]GOTo know the update and delete actions in the foreign key, query n_keys view. Replace the constraint name in thescript.要了解外键中的更新和删除操作,请查询n_keys视图。 在脚本中替换约束名称。SELECT name,delete_referential_action,delete_referential_action_desc,update_referential_action,update_referential_action_desc FROM n_keyThe below image shows that a DELETE CASCADE action and no UPDATE action is defined on the foreign key.下图显⽰在外键上定义了DELETE CASCADE操作,⽽未定义UPDATE操作。Let’s move forward and check the behavior of delete and update rules the foreign keys on a child table which acts asparent table to another child table. The below example demonstrates this scenario.让我们继续前进,检查删除和更新规则的⾏为,该⾏为将作为另⼀个⼦表的⽗表的⼦表上的外键。 下⾯的⽰例演⽰了这种情况。In this case, “Countries” is the parent table of the “States” table and the “States” table is the parent table of Citiestable.在这种情况下,“ 国家 ”是“ 国家 ”表和“ 国 ”表的⽗表是城市表的⽗表。We will create a foreign key now with cascade as delete rule on States table which references to CountryID in parent tableCountries.现在,我们将在⽗表“国家/地区”中引⽤CountryID的“国家/地区”表上使⽤级联作为删除规则来创建外键。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
CREATE TABLE Cities(CityID INT,CityName varchar(50),StateID 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)
INSERT INTO Cities VALUES(1,'Texas City',1)INSERT INTO Cities values (1,'Phoenix',2)
GO
ALTER TABLE [dbo].[States] WITH CHECK ADD CONSTRAINT [FK_States_Countries] FOREIGN KEY([CountryID])REFERENCES [dbo].[Countries] ([CountryID])ON DELETE CASCADEGONow on the Cities table, create a foreign key without a DELETE CASCADE rule.现在,在“ 城市”表上,创建没有DELETE CASCADE规则的外键。ALTER TABLE [dbo].[Cities] WITH CHECK ADD CONSTRAINT [FK_Cities_States] FOREIGN KEY([StateID])REFERENCES [dbo].[States] ([StateID])GOIf we try to delete a record with CountryID =1, it will throw an error as delete on parent table “Countries” tries to delete thereferencing rows in the child table States. But on Cities table, we have a foreign key constraint with no action for delete andthe referenced value still exists in the table.如果我们尝试删除CountryID = 1的记录,则在⽗表“ countries ”上的delete尝试删除⼦表States的引⽤⾏时,它将引发错误。 但是在“ 城市”表上,我们有⼀个外键约束,没有任何删除操作,并且引⽤值仍然存在于表中。DELETE FROM Countries where CountryID =1The delete fails at the second foreign key.删除在第⼆个外键上失败。When we create the second foreign key with cascade as delete rule then the above delete command runs successfully bydeleting records in the child table “States” which in turn deletes records in the second child table “Cities”.当我们使⽤级联作为删除规则创建第⼆个外键时,上述删除命令将通过删除⼦表“ States ”中的记录⽽成功运⾏,这⼜会删除第⼆个⼦表“ Cities ”中的记录。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
CREATE TABLE Cities(CityID INT,CityName varchar(50),StateID 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)
INSERT INTO Cities VALUES(1,'Texas City',1)INSERT INTO Cities values (1,'Phoenix',2)
GO
ALTER TABLE [dbo].[States] WITH CHECK ADD CONSTRAINT [FK_States_Countries] FOREIGN KEY([CountryID])REFERENCES [dbo].[Countries] ([CountryID])ON DELETE CASCADEGO
ALTER TABLE [dbo].[Cities] WITH CHECK ADD CONSTRAINT [FK_Cities_States] FOREIGN KEY([StateID])REFERENCES [dbo].[States] ([StateID])ON DELETE CASCADEGO
DELETE FROM Countries where CountryID =1使⽤删除级联或更新级联外键在表上触发 (Triggers on a table with delete cascade or updatecascade foreign key)An instead of an update trigger cannot be created on the table if a foreign key on with UPDATE CASCADE already exists onthe table. It throws an error “Cannot create INSTEAD OF DELETE or INSTEAD OF UPDATE TRIGGER ‘trigger name’ ontable ‘table name’. This is because the table has a FOREIGN KEY with cascading DELETE or UPDATE.”如果表上已经存在带有UPDATE CASCADE的外键,则不能在表上创建替代更新触发器。 它将引发错误“⽆法在表'表名'上创建INSTEADOF DELETE或INSTEAD OF UPDATE触发器'触发名'。 这是因为该表具有带有级联DELETE或UPDATE的FOREIGN KEY。”Similarly, we cannot create INSTEAD OF DELETE trigger on the table when a foreign key CASCADE DELETE rule alreadyexists on the table.同样,当表上已经存在外键CASCADE DELETE规则时,我们⽆法在表上创建INSTEAD OF DELETE触发器。结论 (Conclusion)In this article, we explored a few examples on DELETE CASCADE and UPDATE CASCADE rules in SQL Server foreign key. Incase you have any questions, please feel free to ask in the comment section below.在本⽂中,我们探索了⼀些有关SQL Server外键中的DELETE CASCADE和UPDATE CASCADE规则的⽰例。 如果您有任何疑问,请随时在下⾯的评论部分中提问。Please refer to this article, to dig in more details on delete and update rules in SQL Server foreign key.请参阅本⽂以深⼊了解SQL Server外键中的删除和更新规则。
2023年8月3日发(作者:)
SQLServer外键中的DELETECASCADE和UPDATECASCADEIn this article, we will review on DELETE CASCADE AND UPDATE CASCADE rules in SQL Server foreign key with differentexamples.在本⽂中,我们将使⽤不同的⽰例回顾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 whenthe referenced row is deleted in the parent table which has a primary CASCADE :当我们使⽤此选项创建外键时,当在具有主键的⽗表中删除引⽤⾏时,它将删除⼦表中的引⽤⾏。UPDATE CASCADE: When we create a foreign key using UPDATE CASCADE the referencing rows are updated in the childtable when the referenced row is updated in the parent table which has a primary 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 examples.让我们看看如何使⽤DELETE和UPDATE CASCADE规则创建外键以及⼀些⽰例。使⽤DELETE和UPDATE CASCADE规则创建外键 (Creating a foreign key with DELETE andUPDATE 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 onthe 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. Clickon OK. Please refer to the below sample image.选择⽗表和⽗表中的主键列。 在⼦表中选择外键列。 单击确定。 请参考下⾯的⽰例图⽚。In the INSERT and UPDATE specifications, select Cascade for the delete rule.在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 UPDATECASCADE 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:Please refer to the below T-SQL script which creates a parent, child table and a foreign key on the child table with DELETECASCADE 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 CASCADEGO
ALTER TABLE [dbo].[States] CHECK CONSTRAINT [FK_States_Countries]GOInsert 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 CASCADEGO
ALTER TABLE [dbo].[States] CHECK CONSTRAINT [FK_States_Countries]GONow 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=1Following is the T-SQL script which creates a foreign key with cascade as UPDATE and DELETE rules.以下是T-SQL脚本,该脚本创建带有级联的外键作为UPDATE和DELETE规则。ALTER TABLE [dbo].[States] WITH CHECK ADD CONSTRAINT [FK_States_Countries] FOREIGN KEY([CountryID])REFERENCES [dbo].[Countries] ([CountryID])ON UPDATE CASCADEON DELETE CASCADEGO
ALTER TABLE [dbo].[States] CHECK CONSTRAINT [FK_States_Countries]GOTo know the update and delete actions in the foreign key, query n_keys view. Replace the constraint name in thescript.要了解外键中的更新和删除操作,请查询n_keys视图。 在脚本中替换约束名称。SELECT name,delete_referential_action,delete_referential_action_desc,update_referential_action,update_referential_action_desc FROM n_keyThe below image shows that a DELETE CASCADE action and no UPDATE action is defined on the foreign key.下图显⽰在外键上定义了DELETE CASCADE操作,⽽未定义UPDATE操作。Let’s move forward and check the behavior of delete and update rules the foreign keys on a child table which acts asparent table to another child table. The below example demonstrates this scenario.让我们继续前进,检查删除和更新规则的⾏为,该⾏为将作为另⼀个⼦表的⽗表的⼦表上的外键。 下⾯的⽰例演⽰了这种情况。In this case, “Countries” is the parent table of the “States” table and the “States” table is the parent table of Citiestable.在这种情况下,“ 国家 ”是“ 国家 ”表和“ 国 ”表的⽗表是城市表的⽗表。We will create a foreign key now with cascade as delete rule on States table which references to CountryID in parent tableCountries.现在,我们将在⽗表“国家/地区”中引⽤CountryID的“国家/地区”表上使⽤级联作为删除规则来创建外键。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
CREATE TABLE Cities(CityID INT,CityName varchar(50),StateID 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)
INSERT INTO Cities VALUES(1,'Texas City',1)INSERT INTO Cities values (1,'Phoenix',2)
GO
ALTER TABLE [dbo].[States] WITH CHECK ADD CONSTRAINT [FK_States_Countries] FOREIGN KEY([CountryID])REFERENCES [dbo].[Countries] ([CountryID])ON DELETE CASCADEGONow on the Cities table, create a foreign key without a DELETE CASCADE rule.现在,在“ 城市”表上,创建没有DELETE CASCADE规则的外键。ALTER TABLE [dbo].[Cities] WITH CHECK ADD CONSTRAINT [FK_Cities_States] FOREIGN KEY([StateID])REFERENCES [dbo].[States] ([StateID])GOIf we try to delete a record with CountryID =1, it will throw an error as delete on parent table “Countries” tries to delete thereferencing rows in the child table States. But on Cities table, we have a foreign key constraint with no action for delete andthe referenced value still exists in the table.如果我们尝试删除CountryID = 1的记录,则在⽗表“ countries ”上的delete尝试删除⼦表States的引⽤⾏时,它将引发错误。 但是在“ 城市”表上,我们有⼀个外键约束,没有任何删除操作,并且引⽤值仍然存在于表中。DELETE FROM Countries where CountryID =1The delete fails at the second foreign key.删除在第⼆个外键上失败。When we create the second foreign key with cascade as delete rule then the above delete command runs successfully bydeleting records in the child table “States” which in turn deletes records in the second child table “Cities”.当我们使⽤级联作为删除规则创建第⼆个外键时,上述删除命令将通过删除⼦表“ States ”中的记录⽽成功运⾏,这⼜会删除第⼆个⼦表“ Cities ”中的记录。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
CREATE TABLE Cities(CityID INT,CityName varchar(50),StateID 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)
INSERT INTO Cities VALUES(1,'Texas City',1)INSERT INTO Cities values (1,'Phoenix',2)
GO
ALTER TABLE [dbo].[States] WITH CHECK ADD CONSTRAINT [FK_States_Countries] FOREIGN KEY([CountryID])REFERENCES [dbo].[Countries] ([CountryID])ON DELETE CASCADEGO
ALTER TABLE [dbo].[Cities] WITH CHECK ADD CONSTRAINT [FK_Cities_States] FOREIGN KEY([StateID])REFERENCES [dbo].[States] ([StateID])ON DELETE CASCADEGO
DELETE FROM Countries where CountryID =1使⽤删除级联或更新级联外键在表上触发 (Triggers on a table with delete cascade or updatecascade foreign key)An instead of an update trigger cannot be created on the table if a foreign key on with UPDATE CASCADE already exists onthe table. It throws an error “Cannot create INSTEAD OF DELETE or INSTEAD OF UPDATE TRIGGER ‘trigger name’ ontable ‘table name’. This is because the table has a FOREIGN KEY with cascading DELETE or UPDATE.”如果表上已经存在带有UPDATE CASCADE的外键,则不能在表上创建替代更新触发器。 它将引发错误“⽆法在表'表名'上创建INSTEADOF DELETE或INSTEAD OF UPDATE触发器'触发名'。 这是因为该表具有带有级联DELETE或UPDATE的FOREIGN KEY。”Similarly, we cannot create INSTEAD OF DELETE trigger on the table when a foreign key CASCADE DELETE rule alreadyexists on the table.同样,当表上已经存在外键CASCADE DELETE规则时,我们⽆法在表上创建INSTEAD OF DELETE触发器。结论 (Conclusion)In this article, we explored a few examples on DELETE CASCADE and UPDATE CASCADE rules in SQL Server foreign key. Incase you have any questions, please feel free to ask in the comment section below.在本⽂中,我们探索了⼀些有关SQL Server外键中的DELETE CASCADE和UPDATE CASCADE规则的⽰例。 如果您有任何疑问,请随时在下⾯的评论部分中提问。Please refer to this article, to dig in more details on delete and update rules in SQL Server foreign key.请参阅本⽂以深⼊了解SQL Server外键中的删除和更新规则。
发布评论