主键主键外键和索引_主键和外键之间的区别

主键是数据库中用于唯一标识记录的关键字段,而外键是从其他表的主键派生,用于建立表间关联的字段。了解两者间的区别对于数据库设计至关重要,避免可能出现的错误。
摘要由CSDN通过智能技术生成

主键主键外键和索引

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

Difference between Primary Key and Foreign Key

Image Source

图片来源

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

S.no.Primary keyForeign key
1The 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.
2A primary key attribute in a table can never contain a null value.A foreign key attribute may have null values as well.
3Not more than one primary key is permitted in a table.A table can have one or more than one foreign key for referential purposes.
4Duplicity 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.
5Example:

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”

IDNAMEAddress
101ABCx
102DEFy
103GHIz
104JKLw
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, :

TIDNAMETAddressID
1PriyaP101
2RiyaR102
3SamS102
4TomT103

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

主键主键外键和索引

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值