This article mainly introduces a simple tutorial on MySQL foreign keys and provides a brief explanation of the foreign key constraints in the InnoDB engine. For more information, see MySQL, tables of the InnoDB engine type support foreign key constraints.
Usage conditions of foreign keys:
1. the two tables must be InnoDB tables, and MyISAM tables do not currently support foreign keys (it is said that foreign keys may be supported in later versions, but at least not supported currently );
2. the foreign key column must have an index. MySQL 4.1.2 and later versions will automatically create an index when creating the foreign key. However, if the foreign key column is in an earlier version, the index must be created;
3. the columns of the foreign key relationship must be of similar data types, that is, columns that can be converted to each other. for example, int and tinyint can be used, but int and char cannot;
Benefits of foreign keys: two tables can be associated to ensure data consistency and perform cascade operations;
Syntax for defining foreign keys:
The code is as follows:
[CONSTRAINT symbol] FOREIGN KEY [id] (index_col_name, …) REFERENCES tbl_name (index_col_name, …) [ON DELETE {RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT}] [ON UPDATE {RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT}]
This syntax can be used in create table and alter table. if the CONSTRAINT symbol is not specified, MYSQL automatically generates a name.
On delete and on update indicate the event trigger limit. you can set the following parameters:
- RESTRICT)
- CASCADE (with foreign key changes)
- Set null (set null)
- SET DEFAULT)
- No action (no action, default)
If a child table tries to create a foreign key value that does not exist in the parent table, InnoDB rejects any INSERT or UPDATE operations. If the parent table tries to UPDATE or DELETE any foreign key values that exist or match in any child table, the final action depends ON the on update and on delete options in the foreign key constraint definition. InnoDB supports five different actions. if on delete or on update is not specified, the default action is RESTRICT:
1. CASCADE: deletes or updates corresponding rows from the parent table, and automatically deletes or updates matched rows from the table. Both on delete canscade and on update canscade are supported by InnoDB.
2. set null: delete or update the corresponding row from the parent table, and SET the foreign key column in the child table to NULL. Note that these foreign key columns are valid only when they are NOT set to not null. Both on delete set null and on update set null are supported by InnoDB.
3. no action: InnoDB refuses to delete or update the parent table.
4. RESTRICT: refuse to delete or update the parent table. The effect of specifying RESTRICT (or no action) is the same as that of ignoring the on delete or on update options.
5. set default: InnoDB currently does not support this function.
The maximum number of foreign key constraints is exceeded in the following two cases:
1) when the parent table is updated, the child table is also updated. when the parent table is deleted, if the child table has matched items, the deletion fails;
2) when the parent table is updated, the child table is also updated. when the parent table is deleted, the matching items of the child table are also deleted.
In the previous case, in the foreign key definition, we use on update cascade on delete restrict; in the latter case, we can use on update cascade on delete cascade.
InnoDB allows you to use alter table to add a new foreign key to an existing TABLE:
ALTER TABLE tbl_name ADD [CONSTRAINT [symbol]] FOREIGN KEY [index_name] (index_col_name, ...) REFERENCES tbl_name (index_col_name,...) [ON DELETE reference_option] [ON UPDATE reference_option]
InnoDB also supports the use of alter table to delete foreign keys:
Alter table tbl_name drop foreign key fk_symbol;