如何为 SQL 服务器中的 table 创建复合候选键?
How do I create a composite candidate key for a table in SQL Server?
请考虑以下table定义...
Table定义
CREATE TABLE [dbo].[Folders](
[Id] [int] IDENTITY(1,1) NOT NULL,
[UserId] [int] NOT NULL,
[ParentFolderId] [int] NULL
PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
其他table与这个table的主键列有外键关系[id]
.
我希望添加一个自引用外键约束,其中父文件夹 ID 引用同一文件夹中另一条记录的 ID 字段 table 但 UserId 也必须匹配...
自引用外键约束
ALTER TABLE [dbo].[Folders] WITH CHECK ADD CONSTRAINT [FK_Folders_ParentFolder] FOREIGN KEY([UserId], [ParentFolderId])
REFERENCES [dbo].[Folders] ([UserId], [Id])
GO
ALTER TABLE [dbo].[Folders] CHECK CONSTRAINT [FK_Folders_ParentFolder]
GO
...但我收到错误...
错误
Msg 1776, Level 16, State 0, Line 64
There are no primary or candidate keys in the referenced table 'dbo.Folders' that match the referencing column list in the foreign key 'FK_Folders_ParentFolder'.
Msg 1750, Level 16, State 0, Line 64
Could not create constraint or index. See previous errors.
Msg 4917, Level 16, State 0, Line 67
Constraint 'FK_Folders_ParentFolder' does not exist.
Msg 4916, Level 16, State 0, Line 67
Could not enable or disable the constraint. See previous errors.
我试图向 table 添加复合唯一索引以提供候选键,但这没有用,我仍然遇到同样的错误。
唯一索引
CREATE UNIQUE NONCLUSTERED INDEX [IX_Folders_UserParentFolder] ON [dbo].[Folders]
(
[UserId] ASC,
[ParentFolderId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
如何在 [Id]
和 [UserId]
上创建 superkey/candidate 键,以便我可以添加自引用外键?请记住,将主键从单个整数转换为复合键会破坏引用文件夹 table 的其他 table 上的外键。这些外键不需要知道UserId。
编辑:
来自 Dan Guzman 的 suggestion/Comments 添加数据库约束达到了目的。这是约束的定义...
约束定义
ALTER TABLE [dbo].[Folders]
ADD CONSTRAINT AK_Folders_UserId UNIQUE ([UserId], [Id])
GO
外键列引用的列必须是主键、唯一约束或唯一索引。我在这里建议一个独特的约束:
ALTER TABLE dbo.Folders
ADD CONSTRAINT UQ_Folders_UserId_Id UNIQUE(UserId, Id);
请考虑以下table定义...
Table定义
CREATE TABLE [dbo].[Folders](
[Id] [int] IDENTITY(1,1) NOT NULL,
[UserId] [int] NOT NULL,
[ParentFolderId] [int] NULL
PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
其他table与这个table的主键列有外键关系[id]
.
我希望添加一个自引用外键约束,其中父文件夹 ID 引用同一文件夹中另一条记录的 ID 字段 table 但 UserId 也必须匹配...
自引用外键约束
ALTER TABLE [dbo].[Folders] WITH CHECK ADD CONSTRAINT [FK_Folders_ParentFolder] FOREIGN KEY([UserId], [ParentFolderId])
REFERENCES [dbo].[Folders] ([UserId], [Id])
GO
ALTER TABLE [dbo].[Folders] CHECK CONSTRAINT [FK_Folders_ParentFolder]
GO
...但我收到错误...
错误
Msg 1776, Level 16, State 0, Line 64
There are no primary or candidate keys in the referenced table 'dbo.Folders' that match the referencing column list in the foreign key 'FK_Folders_ParentFolder'.
Msg 1750, Level 16, State 0, Line 64
Could not create constraint or index. See previous errors.
Msg 4917, Level 16, State 0, Line 67
Constraint 'FK_Folders_ParentFolder' does not exist.
Msg 4916, Level 16, State 0, Line 67
Could not enable or disable the constraint. See previous errors.
我试图向 table 添加复合唯一索引以提供候选键,但这没有用,我仍然遇到同样的错误。
唯一索引
CREATE UNIQUE NONCLUSTERED INDEX [IX_Folders_UserParentFolder] ON [dbo].[Folders]
(
[UserId] ASC,
[ParentFolderId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
如何在 [Id]
和 [UserId]
上创建 superkey/candidate 键,以便我可以添加自引用外键?请记住,将主键从单个整数转换为复合键会破坏引用文件夹 table 的其他 table 上的外键。这些外键不需要知道UserId。
编辑:
来自 Dan Guzman 的 suggestion/Comments 添加数据库约束达到了目的。这是约束的定义...
约束定义
ALTER TABLE [dbo].[Folders]
ADD CONSTRAINT AK_Folders_UserId UNIQUE ([UserId], [Id])
GO
外键列引用的列必须是主键、唯一约束或唯一索引。我在这里建议一个独特的约束:
ALTER TABLE dbo.Folders
ADD CONSTRAINT UQ_Folders_UserId_Id UNIQUE(UserId, Id);