Database principle-Super key, candidate key, primary key, foreign key __ Database

Source: Internet
Author: User

Introduction-Recent database system principles of the textbook, there are some small knowledge point to see really let people silly, even if the psychology of me, do not know what it said, yesterday group meeting discussion, everyone's understanding also have different, the primary key, candidate key, super key, foreign key these seemingly very small concept, discussed for a whole half hour. Let me tell you about my understanding.

--------------------------------------------------------------------first look at what the key codes are and what the key codes represent in different situations.

In the data structure, the key code refers to the data elements that can be identified, such as the login number and the title of the bibliographic information. The key code which can play a unique role is called "The main key code", such as the landing number, otherwise called "sub-key code", such as the title, author name, etc. Usually a data element has only one master code, but it can have multiple codes.
The key code (key, short key) in the database consists of one or more attributes. In actual use, there are the following keys.
(1) Super keys (Super key)
(2) Candidate key (candidate key)
(3) Primary key (Primary key)
(4) Foreign key (Foreign key)

The problem is usually asked to find out a relationship in the key code, the key code is the main key code, is the only sign of the role of the Code.

At this point, he decides the whole relationship, meaning that it can be pushed to export other attributes in the relationship.

Examples:

Relationship mode R (a,b,c,d), F is an FD set established on R, F={ab→c,d→a}
Because D decides a, so AB can decide C is equivalent to DB decision C, so the DB can decide a and C, i.e. the DB can determine the entire R relationship

It is important to distinguish the meaning of key codes in different situations.




the definitions for the various keys in the textbook are as follows: (All keys are a set, this collection can include a set, or it can be multiple sets)

Super Key: A set of attributes that uniquely identifies an element in a relationship is called a hyper-key of the relational pattern.

candidate Key: (Candidate key): A super-key that does not contain extra attributes is called a candidate key. This means that in the candidate key, the property is deleted, not the key.

primary KEY (Primary key): The user is selected as the candidate key of the tuple identity as the primary key. General poor description, the key is the primary key.

foreign KEY (froeign key): If the attribute K in mode R is the primary key of the other mode, then K is called the foreign key in mode R.


the definition of the textbook is too general, I use a Student performance information table to give you a detailed talk about:

Student Achievement Information Form (school number, name, gender, age, department, professional, etc.)

Super key:

Any combination of the student's number or the ID number in the Students ' table is a super-key for this table. such as: (School Number), (school number, name), (school number, gender), etc.

We assume that the student's name is unique and there is no duplication.

Number one, so it's a super key.
Name is unique, so it's a super key
(name, gender) unique, so is a super key
(name, age) unique, so is a super key
(name, gender, age) unique, so is a super key


Candidate Key:

Study number is unique, and there is no extra attribute, so it is a candidate key

Name is unique, and there is no extra attribute, so it is a candidate key

(name, gender) unique, but a single name a property can determine who this person is, so the gender attribute is an extra attribute, so (name, gender) is not a candidate key
(name, age), (name, gender, age) ditto, not candidate key


Primary key:

The primary key is one of the candidate keys, is artificially defined, such as student table, we usually let the "learning number" key, the study number can uniquely identify this tuple.


FOREIGN Key: Foreign key is very simple, if we have a teacher table, each teacher has its own number, assuming that the teacher number in the teacher this level is the primary key, in the student table it is the foreign key.


This said that people are not very clear, summed up a little, will not discuss with the group, the knowledge collision will have descendants.



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.