Foreign key:
Data consistency and integrity are maintained to control data stored in the foreign key table. Associate two tables. The foreign key can only reference the values of columns in the External table!
For example:
A and B
Table A contains the customer ID and customer name.
Orders of each customer exist in table B.
With the foreign key
You can only delete Customer X in Table A after you confirm that there is no Customer X order in table B.
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 ).
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.