[Database] The role of foreign keys

Preface

When it comes to foreign keys , constraints are usually involved . Regardless of constraints, the starting foreign key is an ordinary field (Column), which plays the role of an association.

Let’s put the constraints aside first and see what the foreign keys do.


Establish a one-to-one relationship between records in a table

Student table:

Mobile phone table:

One field for students is mobile phone ID, and mobile phones have multiple attributes. Now, create a separate table for mobile phones.

In this way, if you want to check all mobile phone numbers that day, it is more efficient to check this table directly. The PhoneId at this time is the foreign key of the student table. And it is the primary key of the mobile phone table.

One feature here is that the foreign key of one table must be the primary key in another table. So the foreign key is usually just an ID. This ID associates two tables. If it is one-to-one, it is very simple to join the tables.


One-to-many (many-to-one) relationship

Suppose there are three students and two teachers in this school. Each student can only choose one teacher.

Student table:

Teacher list:

As you can see, student 12 chooses tutor Song, which means multiple students choose one teacher. This is almost the same as one-on-one. That is to tell you that the foreign key itself can be repeated.

In this way, another benefit is discovered. In the case of many-to-one, compared with building one table, building two tables through foreign keys saves more space.


many-to-many relationship

But what if students can choose multiple teachers and teachers can have multiple students?

This is many-to-many.

Student table:

Teacher list:

Foreign key table:

It is found that in the case of multiple pairs, there are no foreign keys in the student table and teacher table. The foreign key exists in another intermediate table, called the foreign key table (of course, this table also has a primary key, but it is omitted here to focus on the key points)

Through this foreign key table, it can be clearly seen that student 1 has two teachers (2,3), and student 3 has three teachers (1,2,3)

Teacher 2 has two students (1, 3), and teacher 3 has two students (3, 1).


constraint

What better reflects the role of foreign keys is constraints . Constraints are actually defined rules that simplify database operations. That is, certain operations are automatically completed after defining constraints! This is another benefit of defining foreign keys.

Constraints are divided into delete and update. Delete is used more often.


Delete constraints

Student table:

Mobile phone table:

Taking a one-to-one example, tables with foreign keys are usually called subtables. Here, the student table is the subtable, and the mobile phone table is the main table of the student table. Deletion constraints mean that when a record in the main table is deleted, how to deal with the corresponding record in the sub-table?

Method 1 : When a record in the main table is deleted, the foreign key in the corresponding record in the sub-table is automatically set to Null.

Method 2: When a record in the main table is deleted, the elements in the corresponding sub-table are automatically deleted.

Method 3: When a record in the main table is deleted, if a record in the sub-table is associated with the main table record, the record in the main table cannot be deleted at this time, and an error will be reported.


Update constraints

This situation occurs rarely, that is, when the PhoneId in the mobile phone table changes, the PhoneId in the student table will also automatically change accordingly.

Guess you like

Origin blog.csdn.net/wangshiqi666/article/details/131492489