table primary key and foreign key

A relational table is actually a collection of a class of entities with common properties. According to the definition of a set, the elements in the set cannot be repeated. Also, there should be no duplicate records in relational tables. For example, in the student table, it is unnecessary and inappropriate to have two identical student records.

In the real world, in addition to the information of things themselves, there are many connections between things, which are reflected in the database as connections between tables. For example, in Figure 1, the student table stores student information, and the department table stores department information. But there is a relationship between the student table and the department table: each student belongs to a department, and each department can contain multiple students, so there is a "belonging" relationship between the student table and the department table.

write picture description here

(figure 1)

In a relational database, how to store information about this connection? It is achieved with the help of "foreign keys". If a column in one table is the primary key in another table, then this column is called a foreign key. For example, the "series number" in the student table in Figure 1 is the foreign key, because the system number is the primary key of the department table (the premise of establishing a primary-foreign key relationship is that the two tables have the same fields and attributes). In the student table, a column of "Department Number" is designed to store the contact information between students and departments.

A foreign key is a link that connects two tables. Through the equivalent connection of foreign key and primary key, as shown in Figure 1, related records in different tables can be connected together, thereby realizing the search of related data in the database. Using the foreign key, you can query the information of each student's department, and you can also query the student information contained in the specified department.

When the two tables are connected through the "foreign key-primary key", it is necessary to maintain the consistency of the data of the two tables. For example, when inserting a student record, the value of the foreign key (the department number) must be a valid value of the primary key in the department table (there must be this department), or a null value (the department of the student has not yet been determined); another example, when deleting When recording in the department table, if there are still records of students in the department in the student table (there are still students in the department), then the department records cannot be deleted.

Let's analyze an example of an employee information management system. The database has a basic employee information table (person), a department code table (deparment) and an education code table (education), as shown in Figure 2:

write picture description here

(figure 2)

The department code table saves the department number and department name. The department number (DepID) is the primary key. The number of each department is unique in the table, so that each row can be identified by the primary key.

The education code table saves the education number and education name, and the education number (EduID) is the primary key.

The employee basic information table stores the basic information of employees, and needs to include fields such as work ID number, name, department number, title, salary, education ID number, etc. The work ID number (ID) is the primary key of the table. In this table, the Department column is a foreign key, matching the DepID primary key in the department code table; the Education column is also a foreign key, matching the EduID primary key in the education education code table. Using the connection between the foreign key and the primary key, a person can be queried. For example, Zhang San's department is the manager's office, and his academic degree is a master's degree.

Using the "foreign key-primary key" connection method can better simplify the database design process, reduce data redundancy, and improve database efficiency.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325432933&siteId=291194637