Home >Backend Development >PHP Tutorial >Database foreign keys, database_PHP tutorial
Created by:
Method 1: Use foreign key (field name) references table name (primary key) when creating the table;
Method 2: Modify the table structure add foreign key (field name) references table name (primary key);
Delete:
alter table table name drop foreign key foreign key name;
If you want to view the foreign key name: show create table table name;
Foreign key constraints:
A certain field in table B is the primary key field of table A.
Table A is the master table and table B is the slave table:
The master table is constrained by the slave table.
1. Table A itself cannot be deleted at will
2. Changes to the records in table A cannot be deleted or updated at will, and there are certain restrictions behind the words
Leave the constraint blank
When a record in the main table is deleted, the foreign key field call in the record in the slave table is null
Syntax:
on delete set null
Cascading Constraints
When the records in the main table are updated, the foreign key fields in the records in the slave table are updated synchronously.
The slave table is constrained by the master table
1. Data from the slave table: The value of the foreign key field in the slave table must be a value in the primary key field in the master table.