在使用分区设置创建父/子表方案后创建外键时遇到问题。
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])
会发生什么是我收到错误:
消息 1776,级别 16,状态 0,第 1 行
被引用的表“dbo.MessageHeader”中没有与外键“FK_HeaderID”中的引用列列表匹配的主键或候选键。
我不确定为什么会发生这种情况,因为该列显然是主键!任何帮助表示赞赏。