3

规范化数据时是否可以在同一个表中重复使用外键?

例如,一家快递公司有一个订单表格和一个客户表格,订单表格将记录从哪个客户取货(Customer_ID),并且还会有一列用于将其交付给哪个客户(也是 Customer_ID)。

4

1 回答 1

2

It is perfectly acceptable; the two indexes will be maintained separately. The two relationships can be used separately...

select item, name, address from orders inner join customers on orders.sender = customers.id where orders.id = 5;
select name, address from orders inner join customers on orders.recipient = customers.id where orders.id = 5;

or together (with care)

select item, name as sender_name, address as sender_address, recipients.name as as recipient_name, recipients.address as recipient_address from orders inner join customers on orders.sender = customers.id inner join customers as recipients on orders.recipient = recipients.id;

Do be careful with cascade deletes ;)

于 2012-10-30T13:35:24.217 回答