Primary key and foreign key of the database

Source: Internet
Author: User

Primary key and foreign key of the database

first, what is the primary key, foreign key: There are several attributes in a record in a relational database, which can be a primary key if one of the attribute groups (note is a group) uniquely identifies a record. For example:  

Student table (school number, name, gender, Class)  
Each student's school number is unique, the study number is a primary key  
Curriculum (course number, course name, credits)  


A single attribute in the score table cannot uniquely identify a record, and the combination of the study number and course number uniquely identifies a record, so The attribute group for the   number and course number is a primary key  
The number in the score table is not the primary key of the score table, but it corresponds to the school number in the student table, And the student table in the student table is the primary key, it is said that the school number is the student table of the foreign key  
empathy   The course number in the score table is the foreign key to the curriculum (s)  
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 an identity plus sign, 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.

Ii. differences between primary key, foreign key, and index

Primary key FOREIGN key Index
Defined: Uniquely identifies a record, cannot be duplicated, is not allowed to be empty The foreign key of the table is the primary key of the other table, the foreign key can have duplicate, can be a null value The field does not have duplicate values, but can have a null value
Role: To ensure data integrity Used to establish contact with other tables. is to improve the speed of query sorting
Number: The primary key can only have one A table can have more than one foreign key A table can have multiple unique indexes

What is the difference between a clustered index and a nonclustered index?

A clustered index must be a unique index. But a unique index is not necessarily a clustered index.

Clustered indexes, where the data is stored directly in the index page, and the non-clustered index is indexed on the index page, which points to the data of the dedicated data page.

Primary key and foreign key of the database

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.