1

创建表脚本:

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)
)

我看着这个,但它对我没有帮助。

卡表有主键

4

1 回答 1

2

中的 PK Card(GiveDate, PN),但您的 FK 引用了一个键(PN, GiveDate) - 列的顺序必须匹配!所以在你的Registration桌子上试试这个:

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)
)
于 2021-06-20T11:30:12.717 回答