来自同一张桌子的2个外键

时间:2018-03-23 11:26:21

标签: mysql sql database netbeans

所以我试图创建一个在线比萨订单系统。我的问题是在为送货人创建表时,我应该在送货员表中使用 cust_id还是我还应该包含 cust_address

CREATE TABLE CUSTOMER
(
    cust_id varchar(7) not null,
    cust_name varchar(30) not null,
    cust_address varchar(40) not null,
    cust_num varchar(11) not null,
    order_id int,
    primary key(cust_id)


);

CREATE TABLE Delivery_Boy
(
    employee_id varchar(7) not null,
    cust_id varchar(7) not null,
    order_id int,
    primary key(employee_id),
    foreign key(order_id) references PIZZA_ORDER(order_id),
    foreign key (cust_id) REFERENCES Customer(cust_id)
);

1 个答案:

答案 0 :(得分:2)

只有cust_id才能通过使用cust_address加入两个表来获得cust_id

喜欢这个

SELECT DB.cust_id,C.cust_name,C.cust_address,C.cust_num,C.order_id
FROM CUSTOMER C INNER JOIN Delivery_Boy DB
ON C.cust_id = DB.cust_id