数据库中主键、超键、候选键、外键是什么?

超键(super key): 在关系中能唯一标识元组的属性集称为关系模式的超键

候选键(candidate key): 不含有多余属性的超键称为候选键。也就是在候选键中,若再删除属性,就不是键了!

主键(primary key): 用户选作元组标识的一个候选键程序主键

外键(foreign key):如果关系模式R中属性K是其它模式的主键,那么k在模式R中称为外键。

举例

比如有如下数据:

学号 姓名 性别 年龄 系别 专业
20020612 李辉 20 计算机 软件开发
20060613 张明 18 计算机 软件开发
20060614 王小玉 19 物理 力学
20060615 李淑华 17 生物 动物学
20060616 赵静 21 化学 食品化学
20060617 赵静 20 生物 植物学
  1. 超键

    在关系中能唯一标识元组的属性集称为关系模式的超键。

    于是我们从例子中可以发现 学号是标识学生实体的唯一标识。那么该元组的超键就为学号。

    除此之外我们还可以把它跟其他属性组合起来,比如:

    (学号性别)

    (学号年龄)

    这样也是超键.

  2. 候选键

    不含多余属性的超键为候选键。

    根据例子可知,学号是一个可以唯一标识元组的唯一标识,因此学号是一个候选键,实际上,候选键是超键的子集,比如 (学号,年龄)是超键,但是它不是候选键。因为它还有了额外的属性。

  3. 主键

    用户选择的候选键作为该元组的唯一标识,那么它就为主键。

    简单的说,例子中的元组的候选键为学号,但是我们选定他作为该元组的唯一标识,那么学号就为主键。

  4. 外键

    外键是相对于主键的,比如在学生记录里,主键为学号,在成绩单表中也有学号字段,因此学号为成绩单表的外键,为学生表的主键。

主键为候选键的子集,候选键为超键的子集,而外键的确定是相对于主键的。


What are Keys in DBMS?

KEYS in DBMS is an attribute or set of attributes which helps you to identify a row(tuple) in a relation(table). They allow you to find the relation between two tables. Keys help you uniquely identify a row in a table by a combination of one or more columns in that table. Key is also helpful for finding unique record or row from the table. Database key is also helpful for finding unique record or row from the table.

Example:

Employee ID FirstName LastName
11 Andrew Johnson
22 Tom Wood
33 Alex Hale

In the above-given example, employee ID is a primary key because it uniquely identifies an employee record. In this table, no other employee can have the same employee ID.

In this tutorial, you will learn:

  • What are Keys?
  • Why we need a Key?
  • Various Keys in Database Management System
  • What is Super key?
  • What is Primary Key?
  • What is Alternate key?
  • What is Candidate Key?
  • What is Foreign key?
  • What is Compound key?
  • What is Composite key?
  • What is Surrogate Key?
  • Difference Between Primary key & Foreign key

Why we need a Key?

Here are some reasons for using sql key in the DBMS system.

  • Keys help you to identify any row of data in a table. In a real-world application, a table could contain thousands of records. Moreover, the records could be duplicated. Keys ensure that you can uniquely identify a table record despite these challenges.
  • Allows you to establish a relationship between and identify the relation between tables
  • Help you to enforce identity and integrity in the relationship.

Types of Keys in Database Management System

There are mainly seven different types of Keys in DBMS and each key has it’s different functionality:

  • Super Key -  A super key is a group of single or multiple keys which identifies rows in a table.
  • Primary Key -  is a column or group of columns in a table that uniquely identify every row in that table.
  • Candidate Key -  is a set of attributes that uniquely identify tuples in a table. Candidate Key is a super key with no repeated attributes.
  • Alternate Key -  is a column or group of columns in a table that uniquely identify every row in that table.
  • Foreign Key -  is a column that creates a relationship between two tables. The purpose of Foreign keys is to maintain data integrity and allow navigation between two different instances of an entity.
  • Compound Key -  has two or more attributes that allow you to uniquely recognize a specific record. It is possible that each column may not be unique by itself within the database.
  • Composite Key -  An artificial key which aims to uniquely identify each record is called a surrogate key. These kind of key are unique because they are created when you don't have any natural primary key.
  • Surrogate Key -  An artificial key which aims to uniquely identify each record is called a surrogate key. These kind of key are unique because they are created when you don't have any natural primary key.

What is the Super key?

A superkey is a group of single or multiple keys which identifies rows in a table. A Super key may have additional attributes that are not needed for unique identification.

Example:

EmpSSN EmpNum Empname
9812345098 AB05 Shown
9876512345 AB06 Roslyn
199937890 AB07 James

In the above-given example, EmpSSN and EmpNum name are superkeys.

What is a Primary Key?

PRIMARY KEY is a column or group of columns in a table that uniquely identify every row in that table. The Primary Key can't be a duplicate meaning the same value can't appear more than once in the table. A table cannot have more than one primary key.

Rules for defining Primary key:

  • Two rows can't have the same primary key value
  • It must for every row to have a primary key value.
  • The primary key field cannot be null.
  • The value in a primary key column can never be modified or updated if any foreign key refers to that primary key.

Example:

In the following example, <code>StudID</code> is a Primary Key.

StudID Roll No First Name LastName Email
1 11 Tom Price abc@gmail.com
2 12 Nick Wright xyz@gmail.com
3 13 Dana Natan mno@yahoo.com

What is the Alternate key?

ALTERNATE KEYS is a column or group of columns in a table that uniquely identify every row in that table. A table can have multiple choices for a primary key but only one can be set as the primary key. All the keys which are not primary key are called an Alternate Key.

Example:

In this table, StudID, Roll No, Email are qualified to become a primary key. But since StudID is the primary key, Roll No, Email becomes the alternative key.

StudID Roll No First Name LastName Email
1 11 Tom Price abc@gmail.com
2 12 Nick Wright xyz@gmail.com
3 13 Dana Natan mno@yahoo.com

What is a Candidate Key?

CANDIDATE KEY is a set of attributes that uniquely identify tuples in a table. Candidate Key is a super key with no repeated attributes. The Primary key should be selected from the candidate keys. Every table must have at least a single candidate key. A table can have multiple candidate keys but only a single primary key.

Properties of Candidate key:

  • It must contain unique values
  • Candidate key may have multiple attributes
  • Must not contain null values
  • It should contain minimum fields to ensure uniqueness
  • Uniquely identify each record in a table

Example: In the given table Stud ID, Roll No, and email are candidate keys which help us to uniquely identify the student record in the table.

StudID Roll No First Name LastName Email
1 11 Tom Price abc@gmail.com
2 12 Nick Wright xyz@gmail.com
3 13 Dana Natan mno@yahoo.com

What is the Foreign key?

FOREIGN KEY is a column that creates a relationship between two tables. The purpose of Foreign keys is to maintain data integrity and allow navigation between two different instances of an entity. It acts as a cross-reference between two tables as it references the primary key of another table.

Example:

DeptCode DeptName
001 Science
002 English
005 Computer
Teacher ID Fname Lname
B002 David Warner
B017 Sara Joseph
B009 Mike Brunton

In this key in dbms example, we have two table, teach and department in a school. However, there is no way to see which search work in which department.

In this table, adding the foreign key in Deptcode to the Teacher name, we can create a relationship between the two tables.

Teacher ID DeptCode Fname Lname
B002 002 David Warner
B017 002 Sara Joseph
B009 001 Mike Brunton

This concept is also known as Referential Integrity.

What is the Compound key?

COMPOUND KEY has two or more attributes that allow you to uniquely recognize a specific record. It is possible that each column may not be unique by itself within the database. However, when combined with the other column or columns the combination of composite keys become unique. The purpose of the compound key in database is to uniquely identify each record in the table.

Example:

OrderNo PorductID Product Name Quantity
B005 JAP102459 Mouse 5
B005 DKT321573 USB 10
B005 OMG446789 LCD Monitor 20
B004 DKT321573 USB 15
B002 OMG446789 Laser Printer 3

In this example, OrderNo and ProductID can't be a primary key as it does not uniquely identify a record. However, a compound key of Order ID and Product ID could be used as it uniquely identified each record.

What is the Composite key?

COMPOSITE KEY is a combination of two or more columns that uniquely identify rows in a table. The combination of columns guarantees uniqueness, though individually uniqueness is not guaranteed. Hence, they are combined to uniquely identify records in a table.

The difference between compound and the composite key is that any part of the compound key can be a foreign key, but the composite key may or maybe not a part of the foreign key.

What is a Surrogate key?

SURROGATE KEYS is An artificial key which aims to uniquely identify each record is called a surrogate key. This kind of partial key in dbms is unique because it is created when you don't have any natural primary key. They do not lend any meaning to the data in the table. Surrogate key is usually an integer. A surrogate key is a value generated right before the record is inserted into a table.

Fname Lastname Start Time End Time
Anne Smith 09:00 18:00
Jack Francis 08:00 17:00
Anna McLean 11:00 20:00
Shown Willam 14:00 23:00

Above, given example, shown shift timings of the different employee. In this example, a surrogate key is needed to uniquely identify each employee.

Surrogate keys in sql are allowed when

  • No property has the parameter of the primary key.
  • In the table when the primary key is too big or complicated.

Difference Between Primary key & Foreign key

Primary Key Foreign Key
Helps you to uniquely identify a record in the table. It is a field in the table that is the primary key of another table.
Primary Key never accept null values. A foreign key may accept multiple null values.
Primary key is a clustered index and data in the DBMS table are physically organized in the sequence of the clustered index. A foreign key cannot automatically create an index, clustered or non-clustered. However, you can manually create an index on the foreign key.
You can have the single Primary key in a table. You can have multiple foreign keys in a table.

Summary

  • A key in SQL is an attribute or set of attributes which helps you to identify a row(tuple) in a relation(table)
  • DBMS keys allow you to establish a relationship between and identify the relation between tables
  • Seven Types of DBMS keys are Super, Primary, Candidate, Alternate, Foreign, Compound, Composite, and Surrogate Key.
  • A super key is a group of single or multiple keys which identifies rows in a table.
  • A column or group of columns in a table which helps us to uniquely identifies every row in that table is called a primary key
  • All the keys which are not primary key are called an alternate key
  • A super key with no repeated attribute is called candidate key
  • A compound key is a key which has many fields which allow you to uniquely recognize a specific record
  • A key which has multiple attributes to uniquely identify rows in a table is called a composite key
  • An artificial key which aims to uniquely identify each record is called a surrogate key
  • Primary Key never accept null values while a foreign key may accept multiple null values.

参考:主键 超键 候选键 外键是什么

参考:Difference Between Primary and Candidate Key

参考:DBMS Keys: Candidate, Super, Primary, Foreign (Example)

数据库中主键、超键、候选键、外键是什么?相关推荐

  1. 数据库中什么是主键,什么是外键?

    数据库中什么是主键,什么是外键? 作者:不染丹心  来源:博客园  发布时间:2009-12-17 22:57  阅读:128 次  原文链接   [收藏]   这需要理清几个概念: 1)候选键: 关 ...

  2. mysql主键和外键示例_SQL数据库中主键和外键的应用实例

    数据库 什么是数据库主键(Primary Key)? 数据库主键(Primary Key):指的是一个列或多列的属性组合,其属性值能唯一标识一条记录,通过它可强制表的实体完整性. 例如: (tb_Bo ...

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

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

  4. 数据库建表时一定要设置外键约束关系吗?

    数据库建表时一定要设置外键约束关系吗? 我们都知道每张数据表都有一个能够确定每行数据唯一性的字段,也就是主键.而在关系数据库中,常常有两表存在一定关系的情况.即一张表的主键跟另一张的外键存在对应关系, ...

  5. mysql建表时建外键约束_数据库建表时一定要设置外键约束关系吗?

    如果被引用的表需要做分库分表,那么无法建立外键约束. 使用外键会降低数据库性能,这个说法并不细致.需要详细分析. 建立外键,那么一般引用字段上需要建立索引.如果不建立索引,被引用的表上删除数据,会全表 ...

  6. 外键查询_详解MySQL数据库删除所有表的外键约束、禁用外键约束相关脚本

    概述 数据库的外键虽然能保证数据数据一致性和完整性,但是也一定程度地影响了数据更新的性能.在开发中,我们使用PowerDesigner建立物理数据模型时,为了结构的清晰,增加可读性,会创建表与表之间的 ...

  7. 数据库表设置外键VS不设置外键,哪个更好?

    转发自极光轻学,原文链接:https://jiguangqingxue.com/info/24 必须设置外键VS不要设置外键的争论 数据库表到底要不要设置外键约束,一直具有非常大的争议.我认为完全没有 ...

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

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

  9. mysql设置外键_mysql怎么增加外键

    mysql增加外键的方法:1.在CREATE TABLE语句中,通过FOREIGN KEY关键字来添加外键:2.在ALTER TABLE语句中,通过ADD和FOREIGN KEY关键字来添加外键. M ...

  10. 是否有必要使用外键?为什么不用外键?

    什么是外键: 如果一个实体的某个字段指向另一个实体的主键,就称为外键.被指向的实体,称之为主实体(主表),也叫父实体(父表).负责指向的实体,称之为从实体(从表),也叫子实体(子表) 外键的作用: ① ...

最新文章

  1. JS 中如何判断 undefined 和 null
  2. 微信支付(JSAPI) - chooseWXPay fail 问题解决
  3. 【Android 应用开发】 ActionBar 基础
  4. SQL中varchar和nvarchar有什么区别?
  5. EMF+GEF的属性页问题
  6. 计算机网络的协议与体系结构
  7. java开发架构设计_跪了!阿里技术官出品:Java架构设计之完美,看完秒进大厂。...
  8. 四十六、Qt网络(六)UDP
  9. mysql error1045 yes,MySQLERROR1045(28000)错误的解决办法
  10. ReviewBoard安装和配置札记
  11. DBA 14条职业选择路线,你适合哪种?
  12. 雷军:电视机越大才越舒服!
  13. MSSQL 触发器 暂停 和 启动
  14. MongoDB日志工作流程
  15. 详细教程:编译Linux最新内核
  16. C# winfrom 添加Log4Net日志库
  17. 382.链表随机节点
  18. JavaScript 中的FileReader对象(实现上传图片预览)
  19. 抖音开屏广告和信息流广告相比较哪一种效果更好?
  20. ink css,ink - 在线工具 - 前端之家

热门文章

  1. 数据库基础知识七:同义词
  2. 高中计算机教室标语,高中教室标语
  3. java清空表格怎么实现_Java 创建、删除、操作PPT中的表格
  4. windows服务器迁到_windows服务器搬迁指南
  5. html+antd 本地化
  6. python 处理excel公式_python 处理excel踩过的坑——data_only,公式全部丢失
  7. csgo fps不稳定和服务器,CSGO fps不稳定,求FPS 优化设置
  8. 【方法】如何提高专注能力
  9. 666. 三角形类型
  10. Windows Phone 7 日历应用的实现