如果我在两个表之间都有关系(两个表都有其自己的主键),那么在决定存储外键的表上,应该怎么做呢?我知道关系的性质可能很重要(一对一,一对多,多对多,单向,双向),访问模式也可能很重要.不过,做出该决定的系统方法是什么?
If I have a relationship between two tables (both tables have their own primary keys) what should guide my decision as to which table should store the foreign key? I understand that the nature of the relationship probably matters (one-to-one, one-to-many, many-to-many, uni-directional, bi-directional), and probably access patterns matter too. What is a systematic way of making that decision though?
推荐答案关系中的哪个表是子表?回答这个问题,您就知道哪个表需要外键列,它引用了父级的(通常是)主键.那是一对多的关系...
Which table is the child in the relationship? Answer that, and you know which table needs the foreign key column, referencing the parent's [typically] primary key. That's for a one-to-many relationship...
多对多要求您使用第三个表中的两个键作为主键来添加第三个表.
A many-to-many would require you to add a third table, using the keys from both of the two tables as it's primary key.
更多推荐
我应该在哪里存储外键?
发布评论