主键主键外键和索引

A “key” is one of the most basic and important concepts of the Database Management System (DBMS); the keys helps us work easily with the records and the tables in our database by providing us with the ease of accessing and retrieving the data from the table. A primary key is the first key to be understood as it uniquely identifies the particular record of any table. The primary key and foreign key however, must not be confused with each other as they have a completely different definition as well as usage. Here are the differences between the two.

“密钥”是数据库管理系统(DBMS)的最基本和最重要的概念之一; 这些键通过使我们能够轻松地访问和检索表中的数据,从而帮助我们轻松地处理数据库中的记录和表。 主键是第一个要理解的键,因为它唯一地标识任何表的特定记录。 但是,主键和外键不能相互混淆,因为它们的定义和用法完全不同。 这是两者之间的区别。

Image Source

图片来源

主键和外键之间的区别 (Difference between Primary Key and Foreign Key)

S.no. Primary key Foreign key
1 The primary key of a particular table is the attribute which uniquely identifies every record and does not contain any null value. The foreign key of a particular table is simply the primary key of some other table which is used as a reference key in the second table.
2 A primary key attribute in a table can never contain a null value. A foreign key attribute may have null values as well.
3 Not more than one primary key is permitted in a table. A table can have one or more than one foreign key for referential purposes.
4 Duplicity is strictly prohibited in the primary key; there cannot be any duplicate values. Duplicity is permitted in the foreign key attribute, hence duplicate values are permitted.
5 Example:

Consider the table student, which keeps record of students in a class:

Here, Id is the primary key which uniquely identifies each record in the table “student”

ID NAME Address
101 ABC x
102 DEF y
103 GHI z
104 JKL w
Now, let us consider the table teacher, which records the teachers in the school:

Here Tid or teacher id is the unique primary key, for each record in the table. This table uses the “ID” attribute which is the primary key of the “student” table as its reference key or foreign key to refer to the student the particular teacher teaches, :

TID NAME TAddress ID
1 Priya P 101
2 Riya R 102
3 Sam S 102
4 Tom T 103

Since one student is taught by more than one teachers, the ID attribute (which is the foreign key in this table), contains duplicate values.

序号 首要的关键 外键
1个 特定表的主键是可唯一标识每条记录且不包含任何空值的属性。 特定表的外键只是其他表的主键,在第二个表中用作参考键。
2 表中的主键属性永远不能包含空值。 外键属性也可以具有空值。
3 一个表中最多只能有一个主键。 一个表可以具有一个或多个外键以供参考。
4 主键中严格禁止重复; 不能有任何重复的值。 外键属性中允许重复,因此允许重复值。
5 例:

考虑一下表格学生,该表格保留班级学生的记录:

在此,Id是唯一标识表“ student”中每个记录的主键

ID 名称 地址
101 美国广播公司 X
102 防御 ÿ
103 GHI ž
104 JKL w
现在,让我们考虑表老师,它记录了学校中的老师:

对于表中的每个记录,此处的Tid或Teacher ID是唯一的主键。 该表使用作为“学生”表主键的“ ID”属性作为其参考键或外键来引用特定老师所教的学生:

工业贸易署 名称 地址 ID
1个 普里亚 P 101
2 里雅 [R 102
3 山姆 小号 102
4 汤姆 Ť 103

由于一位学生由多位老师教,因此ID属性(此表中的外键)包含重复的值。

The foreign key of one table although is derived from the primary key of the other table, holds completely different characteristics and these differences are crucial which should be taken care of to avoid any errors.

一个表的外键虽然是从另一表的主键派生而来的,但它们具有完全不同的特征,这些差异至关重要,应注意避免任何错误。

Comment below if you have queries related to primary key vs foreign key.

如果您对主键和外键有疑问,请在下面评论。

翻译自: https://www.thecrazyprogrammer.com/2019/04/difference-between-primary-key-and-foreign-key.html

主键主键外键和索引

主键主键外键和索引_主键和外键之间的区别相关推荐

  1. mysql主键和唯一索引_主键和唯一索引的有什么区别

    主键和唯一索引的区别 -- 区别 主键是一种约束,唯一索引是一种索引,两者在本质上是不同的. 主键创建后一定包含一个唯一性索引,唯一性索引并不一定就是主键. 唯一性索引列允许空值,而主键列不允许为空值 ...

  2. mysql 主键 聚集索引_主键索引就是聚集索引吗?

    这个问题很刁钻! 答案是错的. 的确在mysql数据库innodb引擎里面,主键的确就是聚集索引. 但是myisam引擎里面主键也不是聚集索引. 另外在sql server中还可以显示的指定聚集索引. ...

  3. 主键主键外键和索引_主键和外键的目的/用途是什么?

    主键主键外键和索引 Primary and foreign keys are a way in which to constrain related data together to ensure d ...

  4. mysql非主键索引_主键索引和非主键索引的区别

    1. 什么是最左前缀原则? 以下回答全部是基于MySQL的InnoDB引擎 例如对于下面这一张表 如果我们按照 name 字段来建立索引的话,采用B+树的结构,大概的索引结构如下 如果我们要进行模糊查 ...

  5. mysql非主键索引_主键索引和非主键索引解析

    1. 什么是最左前缀原则? 以下回答全部是基于MySQL的InnoDB引擎 例如对于下面这一张表 如果我们按照 name 字段来建立索引的话,采用B+树的结构,大概的索引结构如下 如果我们要进行模糊查 ...

  6. jpa 每次启动都要创建外键索引_什么情况下外键建立索引?是必须的吗

    朋友问了一个问题: 首先,外键什么情况下采用呢? 外键是否采用看业务应用场景,以及开发成本的 1. 互联网行业应用不推荐使用外键: 用户量大,并发度高,为此数据库服务器很容易成为性能瓶颈,尤其受IO能 ...

  7. 点击鼠标左键 自动锁定计算机图标,鼠标一按左键桌面图标就消失了怎么办_为什么按鼠标左键时桌面图标都不见了...

    最近有不少用户反馈说碰到这样一个奇怪的现象,就是鼠标只要在桌面一按左键,左面上的图标就会消失了,而且点击鼠标也没有什么用,那么为什么按鼠标左键时桌面图标都不见了呢?原因有很多,可能是系统故障或者中毒等 ...

  8. java内部类为什么会持有外部类的引用_为什么内部类持有外部类的引?

    对普通类(没有内部类的类)来说,内部类和外部类都与他无关:对有内部类的类来说,它们就是其内部类的外部类,外部类是个相对的说法,其实就是有内部类的类.所以,要回答这个问题,只需要讲解内部类是什么:Jav ...

  9. 外循环java作用_“双循环”中的外循环意味着什么?

    内循环是指自产自销,外循环则指的是进口与出口. 中国国际经济交流中心副理事长黄奇帆在9月12日召开的中国金融四十人"曲江论坛"上谈到"双循环"形势下" ...

  10. mysql哪些字段适合索引_(转)Mysql哪些字段适合建立索引

    简单梳理如下:1) 事务支持 MyISAM不支持事务,而InnoDB支持.InnoDB的AUTOCOMMIT默认是打开的,即每条SQL语句会默认被封装成一个事务,自动提交,这样会影响速度,所以最好是把 ...

最新文章

  1. 字典树--Xor问题
  2. 《中国人工智能学会通讯》——12.58 大数据不确定性学习的研究
  3. 需求用例分析之七:业务用例之小结
  4. mysql聚簇索引存储结构_MySQL聚簇索引的实际应用于结构描述
  5. 软件工程启程篇章:C#和四则运算生成与运算
  6. 32添加组件_软件开发32条法则:经过实践检验的实用建议和经验教训
  7. 转载! 一图读懂 SignalR
  8. Eigen 3.3.7 MatrixVector的运算
  9. Windows Phone 7完整版模拟器
  10. 【jzoj2173】【DFS】无根树
  11. windows系统中误删文件恢复
  12. docker: Error response from daemon: driver failed programming external connectivity on endpoint mys
  13. Mars3D开发基础学习:三维场景 Map
  14. Nao机器人运行B-human代码之开始篇 1
  15. PB9核心之——数据窗口对象使用
  16. facebook最全面中文介绍,让你更加了解facebook
  17. java 动态添加字段 以及 动态新增字段注解
  18. Excel根据内容自动调整行高和列宽
  19. 虚拟存储技术-virtual memory technologies
  20. Android Studio 在library中引用本地arr的办法

热门文章

  1. PNP型三极管____本人备注
  2. Ubuntu18.04安装carla0.9.9 UE4.24踩坑版
  3. 文件备份云服务器,文件备份云服务器
  4. python检测图像中的矩形_详解利用OpenCV提取图像中的矩形区域(PPT屏幕等)
  5. http响应码code含义
  6. Hadoop系列之什么是Hadoop(1)
  7. (MDY)2021秋季软件工程 alpha冲刺完善
  8. 解决“远程会话已断开连接,因为访问被拒绝导致许可证存储的创建失败,请使用提升的权限运行远程桌面客户端”问题...
  9. Unite 2018 | 《崩坏3》:在Unity中实现高品质的卡通渲染(上)
  10. IOS描述文件mobileprovision问题