复合键的外键

sn0*_*0ep 8 sql-server foreign-keys

我有一个问题,我需要将一个外键引用到另一个表中的复合键.

我的数据库结构如下:

CREATE TABLE available_trip (
trip_code integer not null,
date datetime not null,
primary key(trip_code, date),
FOREIGN KEY (trip_code) REFERENCES trip (trip_code)
);

CREATE TABLE booking (
    available_trip_code integer not null,
    customer_code integer not null,
    date datetime not null,
    deposit float not null,
    total_price float not null,
    has_paid float not null,
    description_en nvarchar(12) null,
    finance_type_code nvarchar(12) not null,
    primary key(available_trip_code, customer_code, date),
    FOREIGN KEY (available_trip_code) REFERENCES available_trip (trip_code, date),


FOREIGN KEY (customer_code) REFERENCES customer (customer_code),
            FOREIGN KEY (finance_type_code) REFERENCES finance_type (finance_type_code)
        );
Run Code Online (Sandbox Code Playgroud)

我的问题是:我如何让booking.available_trip_code参考available_trip.trip_codeavailable_trip.date

mar*_*c_s 12

如果引用复合主键,则外键也需要包含所有这些列 - 因此您需要以下内容:

FOREIGN KEY (available_trip_code, date) 
            REFERENCES available_trip (trip_code, date)
Run Code Online (Sandbox Code Playgroud)

如果您的表中还没有所有这些列,那么您需要添加它们.