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

更新时间:2023-07-17 04:43:51 阅读: 评论:0

数据库中主键、超键、候选键、外键是什么?数据库中主键、超键、候选键、外键是什么?
超键(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. 超键
在关系中能唯⼀标识元组的属性集称为关系模式的超键。
于是我们从例⼦中可以发现 学号是标识学⽣实体的唯⼀标识。那么该元组的超键就为学号。
identify除此之外我们还可以把它跟其他属性组合起来,⽐如:
图库的英文(学号,性别)
(学号,年龄)
这样也是超键.
2. 候选键
不含多余属性的超键为候选键。spdb
在线发音词典根据例⼦可知,学号是⼀个可以唯⼀标识元组的唯⼀标识,因此学号是⼀个候选键,实际上,候选键是超键的⼦集,⽐如 (学号,年龄)是超键,但是它不是候选键。因为它还有了额外的属性。
3. 主键
⽤户选择的候选键作为该元组的唯⼀标识,那么它就为主键。
简单的说,例⼦中的元组的候选键为学号,但是我们选定他作为该元组的唯⼀标识,那么学号就为主键。
4. 外键
外键是相对于主键的,⽐如在学⽣记录⾥,主键为学号,在成绩单表中也有学号字段,因此学号为成绩单表的外键,为学⽣表的主键。
主键为候选键的⼦集,候选键为超键的⼦集,⽽外键的确定是相对于主键的。
What are Keys in DBMS?
KEYS in DBMS is an attribute or t 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. Databa key is also helpful for finding unique record or row from the table.
Example:
Employee ID FirstName LastName
11Andrew Johnson
22Tom Wood
33Alex Hale
In the above-given example, employee ID is a primary key becau it uniquely identifies an employee record. In this table, no other employee can have the same employee ID.
In this tutorial, you will learn:
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 the challenges.
Allows you to establish a relationship between and identify the relation between tables
速度与激情7字幕文件
Help you to enforce identity and integrity in the relationship.
Types of Keys in Databa Management System
converter是什么意思
gandaThere are mainly ven 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 t 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 purpo 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 itlf within the databa.
Composite Key -  An artificial key which aims to uniquely identify each record is called a surrogate key. The kind of key are unique becau they are created when you don't have any natural primar
y key.
Surrogate Key -  An artificial key which aims to uniquely identify each record is called a surrogate key. The kind of key are unique becau 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
9812345098AB05Shown
9876512345AB06Roslyn
199937890AB07James
简单韩语学习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
111Tom Price
212Nick Wright
313Dana Natan
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 t 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
111Tom Price
212Nick Wrightyishui
313Dana Natan
What is a Candidate Key?
CANDIDATE KEY is a t of attributes that uniquely identify tuples in a table. Candidate Key is a super key with no repeated attributes. The Primary key should be lected 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
111Tom Price
212Nick Wright
313Dana Natan
What is the Foreign key?
FOREIGN KEY is a column that creates a relationship between two tables. The purpo 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
001Science
002English
005Computer
Teacher ID Fname Lname
B002David Warner
B017Sara Joph
B009Mike Brunton
In this key in dbms example, we have two table, teach and department in a school. However, there is no way to e which arch 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
B002002David Warner
B017002Sara Joph
音节的划分B009001Mike 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 itlf within the databa. However, when combined with the other column or columns the combination of composite keys become unique. The purpo of the compound key in databa is to uniquely identify each record in the table.
Example:
OrderNo PorductID Product Name Quantity

本文发布于:2023-07-17 04:43:51,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/179866.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:学号   属性   超键   候选   标识
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图