MySQL 外键的基础知识?

Basics of Foreign Keys in MySQL?(MySQL 外键的基础知识?)
本文介绍了MySQL 外键的基础知识?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

关于如何使用 MySQL 的外键结构有什么好的解释吗?

Is there any good explanation of how to use MySQL's foreign key construct?

我不太了解 MySQL 文档本身.到目前为止,我一直在处理诸如带有连接和编程代码的外键之类的事情.

I don't quite get it from the MySQL docs themselves. Up until now I've been handling things like foreign keys with joins and programming code.

问题的第二部分,使用 MySQL 的内置外键是否有任何改进?

And the second part of the question, are there any improvements to be made by using MySQL's inbuilt foreign keys?

推荐答案

FOREIGN KEYS 只需确保您的数据一致即可.

FOREIGN KEYS just ensure your data are consistent.

他们并没有提高效率意义上的查询,他们只是让一些错误的查询失败.

They do not improve queries in sense of efficiency, they just make some wrong queries fail.

如果你有这样的关系:

CREATE TABLE department (id INT NOT NULL)
CREATE TABLE employee (id INT NOT NULL, dept_id INT NOT NULL, FOREIGN KEY (dept_id) REFERENCES department(id))

,那么你不能删除一个部门,如果它有一些employee.

, then you cannot delete a department if it has some employee's.

如果您将 ON DELETE CASCADE 提供给 FOREIGN KEY 定义,则引用行将与引用行一起自动删除.

If you supply ON DELETE CASCADE to the FOREIGN KEY definition, the referencing rows will be deleted automatically along with the referenced ones.

作为约束,FOREIGN KEY 实际上会稍微减慢查询速度.

As a constraint, FOREIGN KEY actually slows down the queries a little.

从引用表中删除或插入引用表时需要执行额外检查.

Extra checking needs to be performed when deleting from a referenced table or inserting into a referencing one.

这篇关于MySQL 外键的基础知识?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Can#39;t Create Entity Data Model - using MySql and EF6(无法创建实体数据模型 - 使用 MySql 和 EF6)
MySQL select with CONCAT condition(MySQL选择与CONCAT条件)
Capitalize first letter of each word, in existing table(将现有表格中每个单词的首字母大写)
How to retrieve SQL result column value using column name in Python?(如何在 Python 中使用列名检索 SQL 结果列值?)
Update row with data from another row in the same table(使用同一表中另一行的数据更新行)
Exporting results of a Mysql query to excel?(将 Mysql 查询的结果导出到 excel?)