gpt4 book ai didi

SQL Server 删除级联错误

转载 作者:搜寻专家 更新时间:2023-10-30 19:43:42 25 4
gpt4 key购买 nike

我正在设计一个数据库模式,但我被困在一个部分。下面是我的架构的简化版本。有人可以解释为什么以下 SQL:

CREATE TABLE Users (
UserID INT NOT NULL PRIMARY KEY,
FName VARCHAR(64) NOT NULL,
LName VARCHAR(64) NOT NULL,
UName VARCHAR(64) NOT NULL UNIQUE,
PWord CHAR(32) NOT NULL,
Role VARCHAR(13) NOT NULL
);

...

CREATE TABLE Sale (
SaleID INT NOT NULL PRIMARY KEY,
Book INT NOT NULL REFERENCES Books(BookID) ON DELETE NO ACTION,
Merchant INT NOT NULL REFERENCES Users(UserID) ON DELETE CASCADE,
Upload DATETIME NOT NULL,
Sold BIT NOT NULL,
Price DECIMAL(10, 2) NOT NULL,
Condition VARCHAR(9) NOT NULL,
Written BIT NOT NULL,
Comments VARCHAR(8000) NULL
);

...

CREATE TABLE Purchases (
PurchaseID INT NOT NULL PRIMARY KEY,
Buyer INT NOT NULL REFERENCES Users(UserID) ON DELETE CASCADE,
Merchant INT NOT NULL REFERENCES Users(UserID) ON DELETE NO ACTION,
Sale INT NOT NULL REFERENCES Sale(SaleID) ON DELETE CASCADE UNIQUE,
Time DATETIME NOT NULL
);

...导致此错误,以及我如何克服它:

Msg 1785, Level 16, State 0, Line 38
Introducing FOREIGN KEY constraint 'FK__Purchases__Sale__25869641' on table
'Purchases' may cause cycles or multiple cascade paths. Specify ON DELETE
NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

如果可能的话,我想为 Purchases.Sale 属性保留 ON DELETE CASCADE

感谢您的宝贵时间。

最佳答案

这很可能是因为您从两个不同的表中引用了“用户”——采购中的买家和销售中的商家。数据库可能没有意识到这两条记录永远不会相同 - 但它知道这是同一张表,因此会提示潜在的级联问题。

关于SQL Server 删除级联错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13446914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com