MySQL foreign key settings are not mentioned during the process of learning MySQL databases. The following describes how to set MySQL Foreign keys in detail, I hope this will help you learn about MySQL foreign key settings.
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 column name)
Event trigger restrictions: on delete and on update. You can set the parameters cascade, restrict, and set Null ), set Default sets the 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.
New Pricing strategies for MySQL database products
Take you to the DB2 Character Set and MySql Character Set
View MySQL database table commands
Hash-based Mysql table sharding
Seven considerations for converting MySQL to Oracle