问题描述
我使用MySQL Workbench建模我的数据库。在它,我可以选择通过识别外键来创建关系等。这是什么意思?我可以在没有这些关系的情况下运行我的查询。
I'm using MySQL Workbench to model my database. In it, I have the option to create relationships by identifying foreign keys, etc. What's the point of this? I am able to run my queries just fine without these relationships.
我的表有一个实际的外键列,我只是没有在MySQL Workbench中定义它们。意思是,我没有连接表的那些行。
My tables do have an actual column for foreign keys, I just didn't define them in MySQL Workbench. Meaning, I don't have those lines connecting the tables.
推荐答案
外键做几件事。最明显的是,它们为您提供内置的数据完整性,正如Macy Abbey所提到的。
Foreign keys do a few things. Most obviously, they provide you with built in data integrity, as Macy Abbey mentioned.
有趣的是,您最喜欢的RDBMS的查询优化器也可以使用外键等约束来执行其他优化。基本上,数据库可以确定它是否能比你更好地编写查询。外键的存在使数据库做出这些假设。您可以在外键约束是否有助于性能?虽然该链接是SQL Server特定的,不管数据库引擎是什么原理是一样的。
Interestingly, the query optimizer of your favorite RDBMS can also use constraints like foreign keys to do additional optimizations. Basically, the database can determine if it can do a better job of writing a query than you. The presence of foreign keys lets the database make these assumptions. You can find a great example at Do Foreign Key Constraints Help Performance?. While that link is SQL Server specific, the principle is the same regardless of database engine.
这篇关于数据库设计:识别外键的意义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!