关于MYSQL外键和孤行的问题
如果我在两个表之间添加外键,之后是否可以添加孤立行?另外,当我在两个表之间创建外键时,有什么方法可以创建它而忽略孤立行吗?
我的下一个问题是关于外键的效率。我一直认为他们在表中的一个键和另一个表中的相应键之间创建了一个索引,这本质上使其在进行连接时成为线性查找。
外键是否比仅拥有索引更有效,或者它们是否相同?
谢谢。
If I add a foreign key between two tables, am I allowed to add orphan rows afterwards? Also when I'm creating a foreign key between two tables, is there any way to create it ignoring the orphan rows?
My next question is about the efficiency of foreign keys. I had always thought that they created an index between one key in a table and the corresponding key in another table which essentially made it a linear lookup when doing a join.
Is a foreign key much more efficient then simply having an index or are they the same?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
外键关系通常是一个表中的外键与另一个表中的主键之间的关系。
主键确实隐式创建索引。
不是外键。大多数情况下,在外键列上添加索引是一个很好的做法。
键是保证数据一致性的约束,索引可以用来提高数据的访问性能。因此,这些是您在实践中经常组合的不同事物,因此经常会被混淆。
关于孤行,我认为键的概念是为了防止这种情况发生。但我不完全确定我是否完全理解你在这里问的问题。我认为添加孤立行是不可能的,并且如果存在孤立行则创建键听起来有点不可能。
另请参阅有关处理外键/索引主题的其他问题。
这里有一些与主键/键/索引相关的更好的答案
foreign key relationship is often between a foreign key in one table and a primary key in another.
primary key do implicitly create an index.
Not foreign keys. Mostly it's good practice to add index on a foreign key column.
keys are constraints to guarantee data consistency and index can be used improve access performance on the data. So these are different things that you often combine in practices and thus are often confused.
Regarding orphan rows I think the concept of keys is to prevent this. But I'm not completely sure If I understand exactly what you are asking here. I think adding orphan rows is not possible and creating keys if orphan rows exist sounds kind of impossible.
see also other questions on SO dealing with foreign key / index topic.
Here some more good answers related to primary key / key / index