组合键的外键 [英] Foreign key to composite key

查看:185
本文介绍了组合键的外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我需要引用一个外键到另一个表中的组合键。

我的数据库结构如下所示:

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

CREATE TABLE预订(
available_trip_code整数不为空,
客户代码整数不为空,
日期时间不为空,
存款不为空,
total_price float not null,
has_paid float not null,
description_en nvarchar(12)null,
finance_type_code nvarchar(12)非空,
主键(available_trip_code,customer_code, (customer_code),
FOREIGN KEY(customer_code),
FOREIGN KEY(available_trip_code)参考文献available_trip(trip_code,date),


FOREIGN KEY finance_type_code)参考文献finance_type(finance_type_code)
);

我的问题是:如何让 booking.available_trip_code 引用 available_trip.trip_code available_trip.date


$ b $ =h2_lin>解决方案

b

  FOREIGN KEY(available_trip_code,date)
REFERENCES available_trip(trip_code,date)

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


I have a problem i need to reference a single foreign key to a composite key in another table.

My database structure is as following:

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

my question is: how do I let booking.available_trip_code reference to available_trip.trip_code and available_trip.date ?

解决方案

If you reference a composite primary key, your foreign key also needs to contain all those columns - so you need something like:

FOREIGN KEY (available_trip_code, date) 
            REFERENCES available_trip (trip_code, date)

If you don't already have all those columns present in your table, then you'll need to add them.

这篇关于组合键的外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆