Create a foreign key for mysql. Prerequisites for creating a foreign key: the columns in the table must be of the same type as the foreign key (the foreign key must be the external primary key ). Foreign key: associate two tables. a foreign key can only reference the values of columns in the external table! The premise of creating a foreign key: the columns in the table must be of the same type as the foreign key (the foreign key must be the external primary key ).
Foreign key: associate two tables. a foreign key can only reference the values of columns in the external table!
Specify the primary key keyword: foreign key (column name)
Reference foreign key keyword: references <Foreign key table name> (Foreign key column name)
Event trigger restrictions: on delete and on update. you can set the parameters cascade, restrict, and set Null ), set Default (set Default value), [Default] no action
For example:
OutTable table primary key id type int
Create a table with foreign keys:
Create table temp (
Id int,
Name char (20 ),
Foreign key (id) references outTable (id) on delete cascade on update cascade );
Note: set the id column as the foreign key reference external table id column as the foreign key value delete this table corresponding to the column sieve when the foreign key value changes the corresponding column value in this table.
Foreign (the foreign key must be the external primary key ). Foreign key: associate two tables. a foreign key can only reference the values of columns in the external table! ...