为什么在MySQL中使用外键约束? [英] Why use Foreign Key constraints in MySQL?

查看:170
本文介绍了为什么在MySQL中使用外键约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,

在MySQL中我将使用约束作为外键的动机是什么,因为我相信我可以规定添加的类型吗?



是否提高了表现? 外键强制参照完整性。这些约束条件保证了一个表 order_details 中有一个字段 order_id 引用了一个 orders 表永远不会有 orders 表中不存在的 order_id 值。

外键不需要有一个可用的关系数据库(实际上是 MySQL的默认存储引擎不支持FK),但它们对于避免断开的关系和孤行(即参照完整性)绝对是必不可少的。在 ACID 中 C 需要在数据库级别强制引用完整性的能力。 / b>。

至于你对性能的关注,一般来说,性能成本是可以忽略不计的。我建议把所有的外键约束放在一起,只有在没有它们的情况下才能进行实验,如果你有真正的性能问题,那么你就不能解决问题了。


I was wondering,

What will be my motivation to use constraint as foreign key in MySQL, as I am sure that I can rule the types that are added?

Does it improve performance?

解决方案

Foreign keys enforce referential integrity. These constraints guarantee that a row in a table order_details with a field order_id referencing an orders table will never have an order_id value that doesn't exist in the orders table.

Foreign keys aren't required to have a working relational database (in fact MySQL's default storage engine doesn't support FKs), but they are definitely essential to avoid broken relationships and orphan rows (ie. referential integrity). The ability to enforce referential integrity at the database level is required for the C in ACID to stand.

As for your concerns regarding performance, in general there's a performance cost, but will probably be negligible. I suggest putting in all your foreign key constraints, and only experiment without them if you have real performance issues that you cannot solve otherwise.

这篇关于为什么在MySQL中使用外键约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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