The role of foreign keys:
Maintain data consistency, integrity, the primary purpose is to control the data stored in the Foreign key table. Make two tables associative, and foreign keys can only refer to the values of columns in the outside!
For example:
A B two tables
Account number in Table A, customer name
Each customer's order is stored in table B
After having the foreign key
You can only delete customer x in table A when you are sure there are no orders for customer x in table B.
The precondition for establishing a foreign key is that the column of this table must be the same as the foreign key type (the foreign key must be the outer primary key).
Specify PRIMARY KEY keyword: foreign key (column name)
Referencing foreign key keywords: references < foreign key table name > (foreign key column name)
Event Trigger limit: On delete and on UPDATE, set parameter cascade (following foreign key changes), restrict (Restrict foreign key changes in appearance), SET null (null value), set default, [Default]no action]
For example:
Outtable table primary key ID type int
To create a table that contains a foreign key:
CREATE TABLE Temp (
ID int,
Name Char (20),
FOREIGN key (ID) references outtable (ID) on the DELETE cascade on UPDATE cascade);
Description: Set the ID column to foreign key reference the ID column of the outer outtable when the value of the foreign key is removed from the corresponding column in this table except when the value of the foreign key changes the corresponding column value in this table.
MySQL FOREIGN key