Foreign database key, database
Foreign key
Create:
Method 1: when creating a table, use foreign key (field name) references table name (primary key );
Method 2: add foreign key (field name) references table name (primary key) by modifying the table structure );
Delete:
Alter table Name drop foreign key name;
If you want to view the foreign key name: show create table name;
Foreign key constraints:
A field in Table B is the primary key field in table.
Table A is the master table, and table B is the slave table:
The master table is restricted by the slave table.
1. Table A cannot be deleted at will.
2. Changes in the records in Table A cannot be deleted or updated at will. The implication is certain.
Empty Constraint
When a record in the primary table is deleted, the call from the record's foreign key field is null.
Syntax:
On delete set null
Cascade Constraints
When the record of the primary table is updated, the foreign key field in the record is updated synchronously.
The slave table is restricted by the master table.
1. Data in the slave table: the value of the foreign key field in the slave table. It must be a value in the primary key field of the master table.