Cainiao database (4) superkeys, candidate keys, primary keys, foreign keys

Source: Internet
Author: User
Some experiences over the years tell me that many beginners are confused by keys in the database, such as superkeys and candidate keys. Let's talk about the keys in the database. First, let's take a look at the definition of various keys: superkey: the attribute set that uniquely identifies a tuples in a link is called the candidatekey

Some experiences over the years tell me that many beginners are confused by keys in the database, such as superkeys and candidate keys. Let's talk about the keys in the database. First, let's take a look at the definition of various keys: super key: the attribute set that uniquely identifies a tuples in a link is called the candidate key.

Some experiences over the years tell me that many beginners are confused by keys in the database, such as superkeys and candidate keys. Let's talk about the keys in the database.


First, let's look at the definition of various keys:


Superkey (Super key): the attribute set that uniquely identifies a tuples in a link is called the super key of the link mode.

Candidate Key(Candidate key): Super keys without redundant attributes are called candidate keys.

Primary Key (Primary key): The primary key of a candidate key program selected as the tuples.

Foreign key(Foreign key) If an attribute set in relational model R1 is not the primary key of R1, but the primary key of another relational model R2, this attribute set is the foreign key of relational model R1.



According to the specific explanations of the instance:


Assume there are two tables:

Student (student ID, name, gender, ID card number, instructor ID)

Instructor (Instructor ID, name, salary)


Superkeys:

According to the definition of the superkey, any combination of student IDs or student IDs is the superkey of the table. For example: (student ID), (student ID, name), (ID number, gender), etc.


Candidate Key:

The candidate key is a super key, and it is the smallest super key. That is to say, if you remove any attribute of the candidate key, it is no longer a super key. The candidate keys in the student table are: (student ID) and (ID card number ).


Primary key:

The primary key is one of the candidate keys, which is defined by humans. For example, in the student table, we usually use the student ID as the primary key, and in the instructor table, the instructor ID as the primary key.


Foreign key:

The foreign key is relatively simple. The foreign key in the student table is the "instructor ID ". The foreign key is mainly used to describe the relationship between two tables.


In fact, these keys are easy to differentiate. just a beginner can see several similar concepts with similar names at once, so it is easy to confuse himself and create difficulties for himself. Read the definitions twice, and then combine the instances to easily separate them. I hope this article will help beginners a little bit.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.