Home > Common Problem > What is foreign key

What is foreign key

王林
Release: 2020-11-26 14:35:15
Original
8384 people have browsed it

Foreign keys are keywords. If a common key is the primary key in a relationship, then the common key is called the foreign key of another relationship. Foreign keys represent the relevant link between two relationships. Foreign keys can maintain data consistency and integrity, and their main purpose is to control the data stored in the foreign key table.

What is foreign key

Foreign key introduction:

If the public key is the primary key in a relationship, then the public key is is called a foreign key to another relationship. It can be seen that foreign keys represent the relevant links between two relationships. The table with the foreign key of another relationship as the primary key is called the master table, and the table with the foreign key is called the slave table of the master table. Foreign keys are also called foreign keywords.

(Learning video sharing: mysql video tutorial)

Function:

Maintain data consistency and integrity, the main purpose is to control the storage of foreign keys data in the table. To associate two tables, the foreign key can only refer to the values ​​of columns in the table or use null values.

Prevent execution

  • Insert a new row from the table, and the foreign key value is not the primary key value of the main table to prevent the insertion;

  • Modify the foreign key value from the slave table. If the new value is not the primary key value of the master table, the modification will be blocked;

  • Delete the row from the master table, and its primary key value will exist in the slave table. Prevent deletion (if you want to delete, you must first delete the relevant rows from the slave table);

  • #The master table modifies the primary key value. If the old value exists in the slave table, it will prevent the modification (if you want to modify it) , the relevant rows from the table must first be deleted).

Cascade execution

  • Delete rows from the main table and delete related rows from the table together;

  • Modify the primary key value of the main table, and modify the foreign key value of the related rows of the slave table together. Two methods are provided for users to choose. No matter which method is chosen, there will be no extra rows from the table. From another perspective, the same thing is achieved by rejecting the inconsistency between the flags in the slave table and the master table to achieve consistency with the flags in the master table.

Related recommendations: mysql tutorial

The above is the detailed content of What is foreign key. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template