gpt4 book ai didi

database - 规范化 - 重复外键

转载 作者:搜寻专家 更新时间:2023-10-30 19:49:02 29 4
gpt4 key购买 nike

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

例如一家 express 公司有一个订单表和一个客户表,订单表会记录从哪个客户那里取件(Customer_ID),并且还会有一列用于说明要交付给哪个客户(也是 Customer_ID)。

最佳答案

完全可以接受;这两个索引将分开维护。这两个关系可以分开使用...

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;

或一起(小心)

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;

小心级联删除;)

关于database - 规范化 - 重复外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13139796/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com