Database primary key and foreign key (finally understand)

Source: Internet
Author: User

First, what is the primary key, foreign key:

There are several properties in a record in a relational database, and if one of the attribute groups (note is a group) uniquely identifies a record, the attribute Group You can become a primary key  
such as   
student table (student number, name, gender, Class)  
Each student's school number is unique, The study number is a primary key  
curriculum (course number, course name, credits)  
Where the course number is unique, course number is a primary key  
score table (school number, course number, score)  
A single attribute in the score table cannot uniquely identify a record, The combination of the study number and the course number uniquely identifies a record, so   is a primary key  
  
The school number in the score table is not the primary key of the score table, but it corresponds to the student's form, and the student's table is the primary key of the student table, then said that the school number in the score table is the foreign key of the student table  
  
The course number in the same   score table is the foreign key   of the curriculum,
  
Defining primary and foreign keys is primarily to maintain the integrity of the relational database, summarizing:
1. A primary key is a unique identifier that determines a record, for example, a record that includes a social security number, name, and age.

The ID number is the only person who can identify you, others may have duplicates, so the ID number is the primary key.
2. A foreign key is used to associate with another table. is a field that determines the record of another table, which is used to maintain data consistency.

For example, a field in a table is the primary key of table B, so he can be a foreign key to table A.

Database primary key and foreign key (finally understand)

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.