我应该在哪里存储外键? [英] Where should I store a foreign key?

查看:75
本文介绍了我应该在哪里存储外键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在两个表之间都有关系(两个表都有其自己的主键),那么在决定存储外键的表上,应该怎么做呢?我知道关系的性质可能很重要(一对一,一对多,多对多,单向,双向),访问模式也可能很重要.不过,做出该决定的系统方法是什么?

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.

这篇关于我应该在哪里存储外键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆