简体   繁体   English

我似乎无法在SQL Server Management Studio中使用外键或触发器创建一个看似简单的数据库关系

[英]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_IDUser_IDFriend_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_IDFriend_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_IDFriend_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. 这是我想要的布局数据库图,两个外键都级联了它们的删除。

Database Diagram 数据库图

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_Users1Friend_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.

相关问题 无法在SQL Server 2012 Management Studio中创建数据库 - Can`t create database in SQL Server 2012 Management Studio 我无法在数据库SQL Server中创建关系 - i can't create relationship in my database sql server 为什么无法在SQL Server Management Studio中打开此表? - Why can't I open this table in SQL Server Management Studio? 如何在sql server management studio 2012中创建复合外键 - How to create composite foreign key in sql server management studio 2012 在使用外键向数据库中添加新项目时,是否应该使用SQL触发器在其他表中创建相关行? - When adding new items to a database with foreign keys, should I use SQL triggers to create the related rows in other tables? 在SQL Server Management Studio中创建外键 - Creating foreign key in SQL Server Management Studio 在Visual Studio中创建安装文件时,为什么不能连接到SQL Server数据库? - Why can't I connect to the SQL Server database when I create setup file in Visual studio? 如何使用 Microsoft SQL Server Management Studio 为数据库中的所有触发器生成脚本 - How to Generate Scripts For All Triggers in Database Using Microsoft SQL Server Management Studio 如何在SQL Server Management Studio中将1设置为0…1关系 - How to set 1 to 0…1 relationship in SQL Server Management Studio 一是SQL Server Management Studio中的多对多关系 - One is to many relationship in SQL Server Management studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM