在创建具有分区设置的父/子表格方案后遇到创建外键问题。尝试创建外键时出现主键错误
CREATE TABLE [dbo].[MessageHeader]
([MessageHeaderID] [int] IDENTITY(1,1) NOT NULL,
[MessageHeaderGlobalId] [uniqueidentifier] NULL,
[CreatedDateTime] [datetime] NOT NULL
)
GO
ALTER TABLE [dbo].[MessageHeader]
ADD CONSTRAINT [PC_MessageHeader_CreatedDateTime_1]
PRIMARY KEY CLUSTERED ([MessageHeaderID], [CreatedDateTime] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = OFF)
ON [PS_Monthly] ([CreatedDatetime])
CREATE TABLE [dbo].[MessageDataInfo]
([MessageDataInfoID] [int] IDENTITY(1,1) NOT NULL,
[MessageHeaderID] [int] NOT NULL,
[CreatedDateTime] [datetime] NOT NULL)
GO
ALTER TABLE [dbo].[MessageDataInfo]
ADD CONSTRAINT [PC__CreatedDateTime_1]
PRIMARY KEY CLUSTERED ([MessageDataInfoID], [CreatedDateTime])
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = OFF)
ON [PS_Monthly] ([CreatedDatetime])
GO
ALTER TABLE [dbo].[MessageDataInfo] WITH CHECK
ADD CONSTRAINT [FK_HeaderID]
FOREIGN KEY([MessageHeaderID])
REFERENCES [dbo].[MessageHeader] ([MessageHeaderID])
会发生什么事是我的错误:
Msg 1776, Level 16, State 0, Line 1
There are no primary or candidate keys in the referenced table 'dbo.MessageHeader' that match the referencing column list in the foreign key 'FK_HeaderID'.
我不知道为什么会这样,因为该列是明显的主键!任何帮助表示赞赏。
我在第一个'ALTER'语句出现错误:'列名'MessageHeaderID'在目标表或视图中不存在“。应该说'MessageReceivedHeaderID'? –
Ahhh是的,很抱歉试图缩短名称,使其更具可读性。编辑。 – maczealot
我建议你在你的主键表中有一个额外的ID列,然后引用你的外键。 – Sonam