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)