在創建具有分區設置的父/子表格方案後遇到創建外鍵問題。嘗試創建外鍵時出現主鍵錯誤
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