mysql 主键外键sql

A Foreign Key is a key used to link two tables. The table with the Foreign Key Constraint (aka “child table”) is connected to another table (aka, the “parent table”). The connection is between the child table’s Foreign Key Constraint and the parent table’s Primary Key.

外键是用于链接两个表的键。 具有外键约束的表(也称为“子表”)已连接到另一个表(即“父表”)。 连接是在子表的外键约束与父表的主键之间。

Foreign Key Constraints are used to help maintain consistency between the tables. For example, if a parent table record is deleted and the child table has records, the system could also delete the child records.

外键约束用于帮助保持表之间的一致性。 例如,如果删除了父表记录而子表中有记录,则系统也可以删除子记录。

They also help prevent entering inaccurate data in the child table by requiring that a parent table record exists for every record that is entered in the child table.

它们还通过要求在子表中输入的每个记录都存在一个父表记录,来帮助防止在子表中输入不正确的数据。

使用例 (Example of use)

For this guide we’ll take a closer look at the student (parent) and student contact (child) tables.

对于本指南,我们将仔细查看学生(父母)和学生联系方式(孩子)表。

父表的主键 (The parent table’s primary key)

Note that the student table has a one column primary key of studentID.

请注意,student表具有一列的StudentID主键。

SHOW index FROM student;
+---------+------------+----------+--------------+-------------+
| Table   | Non_unique | Key_name | Seq_in_index | Column_name |
+---------+------------+----------+--------------+-------------+
| student |          0 | PRIMARY  |            1 | studentID   |
+---------+------------+----------+--------------+-------------+
1 row in set (0.00 sec) (some columns removed on the right for clarity)

子表的主键和外键 (The child table’s primary and foreign keys)

The student contact info table has one primary key that is also the studentID. This is because there is a one-to-one relationship between the two tables. In other words, we expect only one student and one student contact record per student.

学生联系信息表具有一个主键,也是学生ID。 这是因为两个表之间存在一对一的关系。 换句话说,我们期望每个学生只有一名学生和一名学生联系记录。

SHOW index FROM `student-contact-info`;
+----------------------+------------+----------+--------------+-------------+
| Table                | Non_unique | Key_name | Seq_in_index | Column_name |
+----------------------+------------+----------+--------------+-------------+
| student-contact-info |          0 | PRIMARY  |            1 | studentID   |
+----------------------+------------+----------+--------------+-------------+
1 row in set (0.00 sec) (some columns removed on the right for clarity)
SELECT concat(table_name, '.', column_name) AS 'foreign key',
concat(referenced_table_name, '.', referenced_column_name) AS 'references'
FROM information_schema.key_column_usage
WHERE referenced_table_name IS NOT NULL
AND table_schema = 'fcc_sql_guides_database'
AND table_name = 'student-contact-info';
+--------------------------------+-------------------+
| foreign key                    | references        |
+--------------------------------+-------------------+
| student-contact-info.studentID | student.studentID |
+--------------------------------+-------------------+
1 row in set (0.00 sec)

使用学生父表和联系子表的示例报告 (Example report using the student parent table and the contact child table)

SELECT a.studentID, a.FullName, a.programOfStudy,
b.`student-phone-cell`, b.`student-US-zipcode`
FROM student AS a
JOIN `student-contact-info` AS b ON a.studentID = b.studentID;
+-----------+------------------------+------------------+--------------------+--------------------+
| studentID | FullName               | programOfStudy   | student-phone-cell | student-US-zipcode |
+-----------+------------------------+------------------+--------------------+--------------------+
|         1 | Monique Davis          | Literature       | 555-555-5551       |              97111 |
|         2 | Teri Gutierrez         | Programming      | 555-555-5552       |              97112 |
|         3 | Spencer Pautier        | Programming      | 555-555-5553       |              97113 |
|         4 | Louis Ramsey           | Programming      | 555-555-5554       |              97114 |
|         5 | Alvin Greene           | Programming      | 555-555-5555       |              97115 |
|         6 | Sophie Freeman         | Programming      | 555-555-5556       |              97116 |
|         7 | Edgar Frank "Ted" Codd | Computer Science | 555-555-5557       |              97117 |
|         8 | Donald D. Chamberlin   | Computer Science | 555-555-5558       |              97118 |
+-----------+------------------------+------------------+--------------------+--------------------+

结论 (Conclusion)

Foreign Key Constraints are a great data integrity tool. Take the time to learn them well.

外键约束是一个很好的数据完整性工具。 花一些时间来学习它们。

As with all of these SQL things there is MUCH MORE to them than what’s in this introductory guide.

与所有这些SQL事物一样,它们比本入门指南中的内容要多得多。

I hope this at least gives you enough to get started.

我希望这至少能给您足够的入门。

Please see the manual for your database manager and have fun trying different options yourself.

请参阅数据库管理员的手册,并尝试自己尝试其他选项,这很有趣。

翻译自: https://www.freecodecamp.org/news/sql-foreign-key-vs-primary-key-explained-with-mysql-syntax-examples/

mysql 主键外键sql

mysql 主键外键sql_SQL外键VS主键说明了MySQL语法示例相关推荐

  1. mysql主外键添加_mysql 增加外键:(+创建主键)

    MySQL是开源免费的数据库软件,是一款很优秀的软件,作为我们的学生或者科研人员在开发软件的时候,最好用也是最省钱的.所以MySQL学好是至关重要的! SQL语言包含4个部分: ※ 数据定义语言(DD ...

  2. mysql主键约束和外键约束的作用_MySQL中的主键约束和外键约束

    1.主键约束 表通常具有包含唯一标识表中每一行的值的一列或一组列. 这样的一列或多列称为表的主键 (PK),用于强制表的实体完整性. 由于主键约束可保证数据的唯一性,因此经常对标识列定义这种约束. 如 ...

  3. 主码索引、聚集索引、非主码索引(辅助索引)、唯一索引、外键索引、复合索引、非主码索引、聚集主码(聚集索引)、单列索引、多列索引、普通索引等...

    强烈建议看了第一个参考文献再来看这个篇博文,因为此处不准备讲底层数据结构的实现. 索引:索引(Index)是帮助MySQL高效获取数据的数据结构.提取句子主干,就可以得到索引的本质:索引是数据结构.其 ...

  4. mysql navicat如何为表添加外键?

    mysql navicat如何为表添加外键? 1.使用Navicat设置 打开设计表项: 点击外键项: 外键是当前表可以指向其他表的主键或数据唯一属性的属性. 当前表的sno属性是一个外键,它参考的是 ...

  5. mysql数据库的创建外键_Mysql表创建外键报错解决方案

    数据库表A: CREATE TABLE task_desc_tab ( id INT(11) PRIMARY KEY NOT NULL COMMENT '自增主键' AUTO_INCREMENT, t ...

  6. mysql 所有外键_mysql中的外键

    mysql中的外键 1.默认的外键存在之后,会对数据进行约束. 1)约束1:如果子表中添加的数据,外键字段对应的数据如果在父表中不存在,那么添加失败. 有数据之后: 修改:可以修改跟外键不相关的任何字 ...

  7. SQL语句 -非空约束 - 唯一约束 - 主键约束 - 默认约束 -外键约束

    文章目录 约束 约束介绍和分类 非空约束 唯一约束 主键约束 默认约束 案例练习 外键约束 约束 约束介绍和分类 约束的概念: 约束是作用于表中列上的规则,用于限制加入表的数据 约束的存在保证了数据库 ...

  8. mysql建表 外键_mysql建表外键怎么设

    mysql建表外键怎么设 mysql建表时设置外键的方法:在"CREATE TABLE"语句中,通过"[CONSTRAINT ] FOREIGN KEY 字段名 [,字段 ...

  9. mysql建表外键_mysql建表外键怎么设?

    mysql建表时设置外键的方法:在"CREATE TABLE"语句中,通过"[CONSTRAINT ] FOREIGN KEY 字段名 [,字段名2,-] REFEREN ...

最新文章

  1. cglib代理的使用
  2. 编程入门:准备学Python入门编程 为什么前辈一直劝我不行?
  3. Coding更改程序的变式(report variant change)
  4. 如何测试 SAP OData的filter功能
  5. java对excel经行读写
  6. SAP License:财务与会计的区别
  7. GitLab Elasticsearch 私密群组数据泄露 bug 值3000美元
  8. sizeof和strlen的简单使用方法和区别
  9. react-tv-focusable
  10. 使用AccessibilityService来做一个自动抢红包插件
  11. windows7 旗舰版 集成 usb3.0 NVMe 支持 AM4
  12. Java I/O体系(三)
  13. urllib3爬取网页源代码(爬虫)
  14. 努比亚Z5Smini刷机包 正式版时间锁屏 音量唤醒 精简优化 流畅稳定
  15. linux中时间转换date
  16. 泰山OFFICE技术讲座:标点关系穷举研究-05
  17. 做个清醒的程序员之拥抱AI
  18. Kolmogorov-Smirnov检验
  19. MathType不能正常右对齐解决方法
  20. android动态交换控件位置,Android DynamicGrid实现拖曳交换位置功能

热门文章

  1. Kubernetes-DaemonSet(六)
  2. 实例变量 成员变量 java 1615135036
  3. private的用法,为什么要来一个取值方法和设置值方法
  4. 循环结构格式 java
  5. 使用记事本完成第一个java程序
  6. 移动端相关 em rem px 区别和关联
  7. requests-发送post请求
  8. pthon-递归实战-操作文件
  9. 我的高质量软件发布心得
  10. java 把文件转化为字节数组