## MySQLThe purpose of foreign keys is to control the storage in foreign key tables The data is related to the two tables. It is a very important part of the MySQL database and is worthy of our in-depth understanding. So, MySQLWhat role do foreign keys play? The following will take you to explore the secrets.
MySQLTwo tables
aThe table contains customer number and customer name
bThe order of each customer is stored in the table
With the foreign key, you can only confirmb Customer x can be deleted from the a table only after there is no order from customer x.
#Prerequisites for establishing a foreign key The columns of this table must be of the same type as the foreign key(The foreign key must be the primary key of the foreign key).
Specify the primary key keyword:foreign key(Column name)
Quote the foreign key keyword:references <Foreign key table name>(Foreign key column name)
Event trigger limit on deleteand on update, can set the parametercascade(follows the external Key changes), restrict(Restrict foreign key changes in the table), set Null(SET NULL value),set Default(set default value),[Default]no action
Exampletable primary key id type int
Create Table containing foreign keys:create table temp( id int, name char(20), foreign key(id) references outTable(id) on delete cascade on update cascade);
id column to the MySQL foreign key, refer to the table The id column of outTable. When the value of the foreign key is deleted, the corresponding column in this table is deleted;When the value of the foreign key changes, the value of the corresponding column in this table changes.
Note MySQLA table can only have one primary key, and the primary key can be composed of multiple fields.
The above is the role of mysql advanced (11) foreign keys in the database Content, please pay attention to the PHP Chinese website (m.sbmmt.com) for more related content!