在设计mysql表的时候,外键的放置有什么规定吗?
即一些开发人员会这样做。
//Place the "address_id" inside the customer table
//Customer Table
Customer_id, Address_id, Customer_Name
1 1 Jay
//Address Table
Address_id, City
1 London
有些人会这样做。
//Place the "customer_id" inside the address table
//Customer Table
Customer_id, Customer_Name
1 Jay
//Address Table
Address_id, Customer_id, City
1 1 London
哪种方法更好,为什么?这些开发人员在做出与外键放置相关的决策时会考虑什么?
这可能是一个愚蠢的问题,对某些人来说可能是显而易见的。但我真的很难消除这种困惑,非常感谢您的帮助。
最佳答案
第一种方法允许每个客户使用一个地址,第二种方法允许每个客户使用多个地址。这完全基于需求。例如,如果您想记录办公室和家庭地址,则首选第二种方法。记录现在和通信地址、总公司地址和分公司地址等多种场景。
关于mysql - 设计 mysql 表时的外键放置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17526901/