[英]I can't seem to create a seemingly simple database relationship in SQL Server Management Studio with foreign keys or with triggers
So, I am trying to simplify the problem as much as possible. 因此,我正在尝试尽可能简化问题。 I'm trying to create 2 tables.
我正在尝试创建2个表。
A Users
table that only contains the primary key column, User_ID
. 仅包含主键列
User_ID
Users
表。
And a Friends
table with 3 columns, Friendship_ID
, User_ID
, and Friend_ID
. 还有一个带有3列的
Friends
表, Friendship_ID
, User_ID
和Friend_ID
。
Friendship_ID
is the primary key of the table. Friendship_ID
是表的主键。 I want User_ID
and Friend_ID
to be foreign keys referencing the User_ID
in the Users
table. 我希望
User_ID
和Friend_ID
是引用Users
表中User_ID
外键。
This way, the User_ID
and Friend_ID
must exist in the Users
table to be created in the Friends
table and that Friendship_ID
will be deleted anytime that either of the 2 in the relationship are deleted from the Users
table. 这样,
User_ID
和Friend_ID
必须存在于要在Friends
表中创建的Users
表中,并且无论何时从Users
表中删除关系中的任意两个,都将删除Friendship_ID
。
Thus, I would like for the delete action to cascade through those 2 columns. 因此,我希望delete操作在这2列中级联。 But when I try to specify my second foreign key in a cascade mode, I get the following error.
但是,当我尝试以级联模式指定第二个外键时,出现以下错误。
Unable to create relationship 'FK_Friends_Users1'.
无法创建关系“ FK_Friends_Users1”。
Introducing FOREIGN KEY constraint 'FK_Friends_Users1' on table 'Friends' may cause cycles or multiple cascade paths.在表“朋友”上引入外键约束“ FK_Friends_Users1”可能会导致循环或多个级联路径。 Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。
Here is the database diagram for the layout that I would like, where both of those foreign keys cascade their delete. 这是我想要的布局数据库图,两个外键都级联了它们的删除。
I understand how multiple cascade paths could occur from that layout, so the solution that I had read was to use triggers, so I made sure that I removed all cascading from my foreign keys and instead I chose to use a trigger to accomplish the deleting that I needed. 我知道该布局可能会产生多个级联路径,因此我阅读的解决方案是使用触发器,因此我确保从外键中删除了所有级联,而选择使用触发器来完成删除操作我需要。 Here is the code for my trigger
这是我的触发器的代码
CREATE TRIGGER DeletePrimaryUsers
ON dbo.Users
INSTEAD OF DELETE
AS
BEGIN
SET NOCOUNT ON;
DELETE FROM Friends WHERE User_ID IN (SELECT User_ID FROM deleted)
DELETE FROM Friends WHERE Friend_ID IN (SELECT User_ID FROM deleted)
DELETE FROM Users WHERE User_ID IN (SELECT User_ID FROM deleted)
END
GO
This did not work either. 这也不起作用。 It has no problem when I delete a row from the
Friends
tables, but when I attempt to delete a row from my Users
table, sometimes I get an error. 当我从
Friends
表中删除一行时没有问题,但是当我尝试从Users
表中删除一行时,有时会出现错误。 Here's an example 这是一个例子
Users: 用户:
User_ID
-------
501
502
503
504
505
Friends: 好友:
Friendship_ID User_ID Friend_ID
----------------------------------
5 503 504
6 503 501
8 503 505
When I attempt a delete of row 1 in the Users table, User_ID number 501, I get the following error. 当我尝试删除Users表中的第1行,User_ID号为501时,出现以下错误。
No rows were deleted
没有删除任何行
A problem occurred attempting to delete row 1.
尝试删除第1行时发生问题。
Error Source:.NET SqlClient Data Provider错误源:.NET SqlClient数据提供程序
Error Message: The DELETE statement conflicted with the REFERENCE constraint "FK_Friends_Users1".错误消息:DELETE语句与REFERENCE约束“ FK_Friends_Users1”冲突。 The conflict occurred in database "FriendTest", table "dbo.Friends", column 'Friend_ID'.
数据库“ FriendTest”的表“ dbo.Friends”的列“ Friend_ID”中发生了冲突。
Correct the errors and attempt to delete the row again or press ESC to cancel the change(s).
更正错误,然后尝试再次删除该行,或者按ESC取消更改。
FK_Friends_Users1
is the foreign key for the Friend_ID
column. FK_Friends_Users1
是Friend_ID
列的外键。 That foreign key, like the one for my User_ID
column, is set to No Action
on UPDATE and DELETE for my trigger. 外键,就像一个我
User_ID
列,被设置为No Action
上UPDATE和DELETE我的触发器。
Any help would be greatly appreciated. 任何帮助将不胜感激。
Edit: 编辑:
Foreign Key Creation script. 外键创建脚本。
USE FriendTest
ALTER TABLE dbo.Friends WITH CHECK
ADD CONSTRAINT FK_Friends_Users FOREIGN KEY (User_ID)
REFERENCES dbo.Users (User_ID)
USE FriendTest
ALTER TABLE dbo.Friends WITH CHECK
ADD CONSTRAINT FK_Friends_Users1 FOREIGN KEY (Friend_ID)
REFERENCES dbo.Users (User_ID)
The trigger should be updated as follows: 触发器应更新如下:
CREATE TRIGGER DeletePrimaryUsers
ON dbo.Users
INSTEAD OF DELETE
AS
BEGIN
SET NOCOUNT ON;
DELETE FROM Friends WHERE User_ID IN (SELECT User_ID FROM deleted)
OR Friend_ID IN (SELECT User_ID FROM deleted);
DELETE FROM Users WHERE User_ID IN (SELECT User_ID FROM deleted)
END
GO`
Perhaps an easier way to do it would be to replace the first two lines with this: 也许更简单的方法是用以下内容替换前两行:
DELETE FROM Friends
WHERE friend_id IN (SELECT user_id FROM deleted)
OR user_id IN (SELECT userid FROM deleted);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.