gpt4 book ai didi

sql - ALTER TABLE 语句与 FOREIGN KEY 约束冲突

转载 作者:行者123 更新时间:2023-12-02 06:55:38 28 4
gpt4 key购买 nike

我对 SQL 脚本编写有点陌生,并且在我的一个脚本中遇到了错误。有问题的部分是:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE dbo.WorkspaceSettings
(
Id INT NOT NULL IDENTITY PRIMARY KEY ,
ReportColorRGB1 VARCHAR(15) NOT NULL DEFAULT '61,105,138' ,
ReportColorRGB2 VARCHAR(15) NOT NULL DEFAULT '180,210,121'
)

GO

ALTER TABLE Workspace ADD WorkspaceSettingsId int NOT NULL default 1;

GO
ALTER TABLE Workspace
ADD CONSTRAINT FK_WorkspaceSettings_Workspace
FOREIGN KEY (WorkspaceSettingsId)
REFERENCES WorkspaceSettings(Id);
GO

并收到以下错误消息:

Msg 547, Level 16, State 0, Line 1
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_WorkspaceSettings_Workspace". The conflict occurred in database "ClearRisk2_0", table "dbo.WorkspaceSettings", column 'Id'.

有人可以告诉我哪里出错了吗?

最佳答案

您为 Workspace.WorkspaceSettingsId 列指定的默认值 1 在您的 WorkspaceSettings 表中尚不存在,因此存在 FK 违规。

关于sql - ALTER TABLE 语句与 FOREIGN KEY 约束冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4865873/

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