📜  主键和超级键的区别

📅  最后修改于: 2021-09-10 01:51:15             🧑  作者: Mango

先决条件 – 关系模型中的键
1.超级钥匙:
超级键是一个属性(或一组属性),用于唯一标识关系中的所有属性。所有超级键都不能是候选键,但反之亦然。在一种关系中,超级键的数量大于候选键的数量。

例子:
我们有一个给定的关系 R(A, B, C, D, E, F) ,我们将通过以下给定的依赖关系检查是否是超级键:

Functional dependencies         Super key
AB->CDEF                         YES
CD->ABEF                         YES
CB->DF                           NO
D->BC                            NO 

通过使用键AB,我们可以识别表的其余属性(CDEF) 。同样的Key CD。但是,通过使用密钥CB我们只能识别DF而不是AE 。同样的键D

2. 主键:
候选键是一组唯一标识关系或表中元组的属性(或属性)。可以有多个相关的候选键,可以从中选择一个作为主键。

例子:

Student{Stud_No, Stud_name, Stud_phone, 
             Stud_state, Stud_country, Stud_age} 

在这里我们可以看到两个候选键Stud_NoStud_phone。可以选择 STUD_No 作为主键(只有一个候选键)。

超级键和主键的区别:

S.NO Super Key Primary Key
1. Super Key is an attribute (or set of attributes) that is used to uniquely identifies all attributes in a relation. Primary Key is a minimal set of attribute (or set of attributes) that is used to uniquely identifies all attributes in a relation.
2. All super keys can’t be primary keys. Primary key is a minimal super key.
3. Various super keys together makes the criteria to select the candidate keys. We can choose any of the minimal candidate key to be a primary key.
4. In a relation, number of super keys are more than number of primary keys. While in a relation, number of primary keys are less than number of super keys.
5. Super key’s attributes can contain NULL values. Primary key’s attributes cannot contain NULL values.