引用的 table 'Card' 中没有与外键中的引用列列表匹配的主键或候选键
There are no primary or candidate keys in the referenced table 'Card' that match the referencing column list in the foreign key
创建 table 个脚本:
Create Table [Card]
(
GiveDate Date not null,
PN nvarchar(50) not null,
FOREIGN KEY (PN) REFERENCES Patient (PN),
PRIMARY KEY (GiveDate,PN)
)
Create table [Registration]
(
EntryDate Date not null,
ExitDate Date,
RoomId int not null,
CardGiveDate date not null,
PN nvarchar(50) not null,
PRIMARY KEY (PN, EntryDate, CardGiveDate),
FOREIGN KEY (PN, CardGiveDate) REFERENCES [Card](PN, GiveDate)
)
我查看了 this,但对我没有帮助。
卡片table有主键
Card
中的 PK 是 (GiveDate, PN)
,但您的 FK 引用了一个键 (PN, GiveDate)
- 列的顺序 必须匹配 !所以在你的 Registration
table:
中试试这个
Create table [Registration]
(
EntryDate Date not null,
ExitDate Date,
RoomId int not null,
CardGiveDate date not null,
PN nvarchar(50) not null,
PRIMARY KEY (PN, EntryDate, CardGiveDate),
-- make sure to specify the columns in the same order as they are defined in the referenced table!
FOREIGN KEY (PN, CardGiveDate) REFERENCES [Card](GiveDate, PN)
)
创建 table 个脚本:
Create Table [Card]
(
GiveDate Date not null,
PN nvarchar(50) not null,
FOREIGN KEY (PN) REFERENCES Patient (PN),
PRIMARY KEY (GiveDate,PN)
)
Create table [Registration]
(
EntryDate Date not null,
ExitDate Date,
RoomId int not null,
CardGiveDate date not null,
PN nvarchar(50) not null,
PRIMARY KEY (PN, EntryDate, CardGiveDate),
FOREIGN KEY (PN, CardGiveDate) REFERENCES [Card](PN, GiveDate)
)
我查看了 this,但对我没有帮助。
卡片table有主键
Card
中的 PK 是 (GiveDate, PN)
,但您的 FK 引用了一个键 (PN, GiveDate)
- 列的顺序 必须匹配 !所以在你的 Registration
table:
Create table [Registration]
(
EntryDate Date not null,
ExitDate Date,
RoomId int not null,
CardGiveDate date not null,
PN nvarchar(50) not null,
PRIMARY KEY (PN, EntryDate, CardGiveDate),
-- make sure to specify the columns in the same order as they are defined in the referenced table!
FOREIGN KEY (PN, CardGiveDate) REFERENCES [Card](GiveDate, PN)
)