Should I use foreign keys every related tables or shouldn't I? If I should use, why?
我应该在每个相关的表中使用外键还是不应该使用外键?如果我应该使用,为什么?
6 个解决方案
#1
6
"All related tables" is not always clear, so it's not an obvious situation. There can be tables that have a common column but may never see each other.
“所有相关表格”并不总是很清楚,所以这不是一个明显的情况。可能存在具有公共列但可能永远不会彼此看到的表。
It's handy, however, to prevent errors that slip past your primary defenses and allow data to be entered that's not easily traceable.
但是,它可以防止错误超出主要防御并允许输入不易追踪的数据。
They don't help make queries more efficient if you have the proper indexes in place, and a good application will filter input enough that they should never be needed. But mistakes happen, and they are a cheap line of defense.
如果您具有适当的索引,它们无助于提高查询效率,并且一个好的应用程序将过滤输入足以永远不需要它们的输入。但是错误发生了,而且它们是一种廉价的防线。
If you're just getting comfortable with designing databases, they aren't something to spend a lot of time worrying about and refining in great detail, once you have the basic parent/child relationships in place.
如果您只是习惯于设计数据库,那么一旦您掌握了基本的父/子关系,就不会花费大量时间来担心和细化。
Here's some background--
这是一些背景 -
What's wrong with foreign keys?
外键有什么问题?
#2
6
You should. There are three main points:
你应该。主要有三点:
- Depending on your DB system, you could get better performance
- 根据您的数据库系统,您可以获得更好的性能
- Foreign keys ensure data integrity, e.g. can help to avoid orphan records etc
- 外键确保数据完整性,例如可以帮助避免孤儿记录等
- They an explicit way to document your DB structure, which can be used by tools for visualization, code generation etc.
- 它们是一种记录数据库结构的明确方式,可供工具用于可视化,代码生成等。
#3
6
Yes. But on moderate levels. This helps when you are querying data. It helps in indexing your data, so faster queries. Also it helps in maintaining the relationships between entities.
是。但在适度的水平。这有助于查询数据。它有助于索引数据,从而加快查询速度。它还有助于维护实体之间的关系。
#4
5
Using Foreign keys is one of the basis (if not the only) , the main concept of relational databases. You off course should use foreign keys where needed. Because this helps to:
使用外键是关系数据库的主要概念之一(如果不是唯一的话)。你当然应该在需要时使用外键。因为这有助于:
- Make sure of the data validation and integrity
- 确保数据验证和完整性
- Save lots of extra space
- 节省大量额外空间
The first one means instead of adding a value manually to a field that is going to be repeated for other records you just choose from what you have in the related, the primary key table. If you try to enter something that does not exist as a primary key in the other table you'll be rejected (In some databases you can tweak this behavior though).
第一个意味着不是手动将值添加到一个字段,而该字段将重复用于其他记录,您只需从相关的主键表中选择。如果您尝试在另一个表中输入不存在的主键作为主键,您将被拒绝(在某些数据库中,您可以调整此行为)。
The second one means You don't have to write the "United States of America" each time, which would take a lot more space than just writing the ID of the "United States of America".
第二个意味着你不必每次都写“美利坚合众国”,这比仅仅写“美利坚合众国”的ID需要更多的空间。
#5
4
Yes, you should.Foreign keys are just constrains which helps you to make relationships and be sure that you have correct information in your database.You should use them to prevent incorrect data entry from whatsoever.
是的,你应该。外键只是约束,它可以帮助你建立关系,并确保你的数据库中有正确的信息。你应该使用它们来防止不正确的数据输入。
#6
1
There's actually a growing move away from foreign key based relational databases at the moment. Document Database systems such a MongoDB are becoming much more popular. This is largely because the world is becoming much more distributed with the cloud.
实际上,目前正在逐渐远离基于外键的关系数据库。像MongoDB这样的文档数据库系统正变得越来越流行。这很大程度上是因为随着云的发展,世界变得越来越分散。
This means that sometime not reasonable or performant to assume immediate data consistency.
这意味着有时候假设立即数据一致性是不合理或高效的。
Have a read on NoSQL based database, MongoDB CouchDB and evential consistancy if you are interested.
如果您有兴趣,请阅读基于NoSQL的数据库,MongoDB CouchDB和均匀一致性。
Its kinda a weird thing for our relational trained minds to understand but a lot of large scale web solutions
对于我们的关系训练有素的人来说,理解但是很多大规模的网络解决方案,这有点奇怪
#1
6
"All related tables" is not always clear, so it's not an obvious situation. There can be tables that have a common column but may never see each other.
“所有相关表格”并不总是很清楚,所以这不是一个明显的情况。可能存在具有公共列但可能永远不会彼此看到的表。
It's handy, however, to prevent errors that slip past your primary defenses and allow data to be entered that's not easily traceable.
但是,它可以防止错误超出主要防御并允许输入不易追踪的数据。
They don't help make queries more efficient if you have the proper indexes in place, and a good application will filter input enough that they should never be needed. But mistakes happen, and they are a cheap line of defense.
如果您具有适当的索引,它们无助于提高查询效率,并且一个好的应用程序将过滤输入足以永远不需要它们的输入。但是错误发生了,而且它们是一种廉价的防线。
If you're just getting comfortable with designing databases, they aren't something to spend a lot of time worrying about and refining in great detail, once you have the basic parent/child relationships in place.
如果您只是习惯于设计数据库,那么一旦您掌握了基本的父/子关系,就不会花费大量时间来担心和细化。
Here's some background--
这是一些背景 -
What's wrong with foreign keys?
外键有什么问题?
#2
6
You should. There are three main points:
你应该。主要有三点:
- Depending on your DB system, you could get better performance
- 根据您的数据库系统,您可以获得更好的性能
- Foreign keys ensure data integrity, e.g. can help to avoid orphan records etc
- 外键确保数据完整性,例如可以帮助避免孤儿记录等
- They an explicit way to document your DB structure, which can be used by tools for visualization, code generation etc.
- 它们是一种记录数据库结构的明确方式,可供工具用于可视化,代码生成等。
#3
6
Yes. But on moderate levels. This helps when you are querying data. It helps in indexing your data, so faster queries. Also it helps in maintaining the relationships between entities.
是。但在适度的水平。这有助于查询数据。它有助于索引数据,从而加快查询速度。它还有助于维护实体之间的关系。
#4
5
Using Foreign keys is one of the basis (if not the only) , the main concept of relational databases. You off course should use foreign keys where needed. Because this helps to:
使用外键是关系数据库的主要概念之一(如果不是唯一的话)。你当然应该在需要时使用外键。因为这有助于:
- Make sure of the data validation and integrity
- 确保数据验证和完整性
- Save lots of extra space
- 节省大量额外空间
The first one means instead of adding a value manually to a field that is going to be repeated for other records you just choose from what you have in the related, the primary key table. If you try to enter something that does not exist as a primary key in the other table you'll be rejected (In some databases you can tweak this behavior though).
第一个意味着不是手动将值添加到一个字段,而该字段将重复用于其他记录,您只需从相关的主键表中选择。如果您尝试在另一个表中输入不存在的主键作为主键,您将被拒绝(在某些数据库中,您可以调整此行为)。
The second one means You don't have to write the "United States of America" each time, which would take a lot more space than just writing the ID of the "United States of America".
第二个意味着你不必每次都写“美利坚合众国”,这比仅仅写“美利坚合众国”的ID需要更多的空间。
#5
4
Yes, you should.Foreign keys are just constrains which helps you to make relationships and be sure that you have correct information in your database.You should use them to prevent incorrect data entry from whatsoever.
是的,你应该。外键只是约束,它可以帮助你建立关系,并确保你的数据库中有正确的信息。你应该使用它们来防止不正确的数据输入。
#6
1
There's actually a growing move away from foreign key based relational databases at the moment. Document Database systems such a MongoDB are becoming much more popular. This is largely because the world is becoming much more distributed with the cloud.
实际上,目前正在逐渐远离基于外键的关系数据库。像MongoDB这样的文档数据库系统正变得越来越流行。这很大程度上是因为随着云的发展,世界变得越来越分散。
This means that sometime not reasonable or performant to assume immediate data consistency.
这意味着有时候假设立即数据一致性是不合理或高效的。
Have a read on NoSQL based database, MongoDB CouchDB and evential consistancy if you are interested.
如果您有兴趣,请阅读基于NoSQL的数据库,MongoDB CouchDB和均匀一致性。
Its kinda a weird thing for our relational trained minds to understand but a lot of large scale web solutions
对于我们的关系训练有素的人来说,理解但是很多大规模的网络解决方案,这有点奇怪