mysql primary key (leadership), foreign key (employee)

References:
1. Foreign key foreign key
in mysql 2. Several concepts of MySQL: primary key, foreign key, index, unique index

One, the primary key of mysql

The main function of mysql's primary key is to keep the data unique and non-empty; and it is also a special index, the primary key index.

Two, mysql foreign key

If the non-primary key field of one table points to the primary key field of another table, then this field is called a foreign key.
There are two main functions of mysql's foreign key:
1. When operating a sub-table (the table where the foreign key is located, generally the value of the foreign key added or modified), if the primary key field of the primary table pointed to has no value, it will Report an error.
2. When operating the parent table (the table where the primary key is located, generally refers to modifying the value of the primary key column; or deleting data), if it is referenced by the foreign key of the child table , an error will be reported.
Summary: As long as the value of the primary key is referenced by the value of the foreign key, the value of the primary key cannot be moved or deleted; the addition of the foreign key must be added within the scope of the primary key.

2.1. Customization of foreign keys-three constraint modes:

district: Strict mode (default), the parent table cannot delete or update a record referenced by the child table.
Cascade: cascade mode, after the parent table is operated, the data associated with the child table is also operated together.
set null: blanking mode, provided that the foreign key field is allowed to be NLL, after the parent table is operated, the field corresponding to the child table is blanked.

Guess you like

Origin blog.csdn.net/weixin_43983411/article/details/110001563