1, Mysql foreign keys can not be the reason to create 2, CASCADE MySql foreign key constraints of, SET NULL, RESTRICT, NO ACTION and effect analysis

When you create a foreign key in Mysql, often encounter problems and failed Mysql This is because there are many details that we need to pay attention, my own summary and lists several common reasons that access to information.

 

1. not strictly match the type or size of the two fields. For example, if one is int (10), then the foreign key must be provided int (10), instead int (11), it can not be tinyint. In addition, you must also determine whether the two fields is a signed, and the other is unsigned (ie: unsigned), two field must match exactly the same, for more information on signed and unsigned, see: http: //www.verysimple.com/blog/?p=57

 

2. try to set the foreign key index field is not established or is not a primary key (primary key). If one is not a primary key, you must first create an index for it.

 

3 is a table in which one or two tables MyISAM engine. If you want to use a foreign key constraint, the table must be InnoDB engine (in fact, if two tables are MyISAM engine, this error will not happen, but it will not generate foreign keys, indexes only) you need to check engine type table.

 

4. The name of the foreign key can not be repeated. You should check your database to ensure that the foreign key name is unique, or if you add a few random characters behind the keys to test if this is the reason.

 

5. You may set up ON DELETE SET NULL, but related set of key fields and become NOTS NULL value. You may be resolved by modifying the property value cascade of the field or property to allow null.

 

6. Make sure your Charset consistent and Collate option on the table level and the field level.

 

7. You may set a default value provided is a foreign key, such as the default = 0.

 

8. ALTER statement has a syntax error

 

 

 

When an error occurs, double-check the design database tables and fields. I suggest that you try to use tools to design and create the database, which could reduce the probability of error.

 

MySQL There are two common types of engines: MyISAM and InnoDB. Currently, only InnoDB engine type supports foreign key constraints. InnoDB foreign key constraint defined syntax is as follows:

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]

E.g:

ALTER TABLE `user_resource` CONSTRAINT `FKEEAF1E02D82D57F9` FOREIGN KEY (`user_Id`) REFERENCES `sys_user` (`Id`)

InnoDB also supports the use of ALTER TABLE to drop the foreign keys:

ALTER TABLE `user_resource` DROP FOREIGN KEY `FKEEAF1E02D82D57F9`;

 

CASCADE

When the update / delete records in the parent table, the synchronization update / delete records in the child table matches out 

 

SET NULL

When the update / delete records, the sub-matching records in the table to the parent table row null (the foreign key is to be noted sub-column of the table can not be not null)  

 

NO ACTION

If a matching record in the child table is not allowed to the parent table corresponds to a candidate key update / delete operations  

 

RESTRICT

With no action, foreign key constraints are checked immediately

 

SET NULL

When there is a change in the parent table, the child table as a foreign key column provided default values, but can not identify Innodb

 

 

 

NULL、RESTRICT、NO ACTION

Delete: The main table can be deleted from Table record does not exist. Unchanged deleted from the table, the main table

Update: When there is no record from the table, the main table can be updated. Update unchanged from the table, the main table

 

CASCADE

Delete: When you delete a table automatically deleted from the main table. Unchanged deleted from the table, the main table

Update: Automatic updates from the master table updates table. Update unchanged from the table, the main table

 

SET NULL

Delete: Automatic updates from the table value is NULL when you delete the main table. Unchanged deleted from the table, the main table

Update: Automatic updates from the master table updates table value is NULL. Update unchanged from the table, the main table

 

Foreign key constraint attributes: RESTRICT | CASCADE | SET NULL | NO ACTION using foreign key requires the following condition is satisfied:

 

  1. The two tables must be InnoDB tables are, and they are not temporary tables.

 

  2. Establish the corresponding foreign key columns must have a relationship similar InnoDB internal data types.

 

  3. Establish the corresponding column foreign key relationship must be established index.

 

  4. If the explicitly given CONSTRAINT symbol, that symbol must be unique within the database. If not explicitly given, InnoDB will automatically be created.

 

  If the child table trying to create a foreign key value that does not exist in the parent table, InnoDB rejects any INSERT or UPDATE operation. If the parent table UPDATE, or attempts to match the foreign key value exists DELETE or any sub-table, depending on the final action ON UPDATE ON DELETE option and foreign key constraint definition. InnoDB supports five different actions if no ON DELETE or ON UPDATE, the default action is RESTRICT:

 

  1. CASCADE: update or delete the corresponding rows from the parent table, and automatically update or delete rows from the table match. ON DELETE CANSCADE and ON UPDATE CANSCADE are supported by InnoDB.

 

  2. SET NULL: update or delete the corresponding rows from the parent table, and foreign key columns in the child table set to null. Note that these foreign key column if NOT NULL is not set to valid. ON DELETE SET NULL and ON UPDATE SET SET NULL are supported by InnoDB.

 

  3. NO ACTION: InnoDB refused to delete or update the parent table.

 

  4. RESTRICT: refuse to remove or update the parent table. The effect specify RESTRICT (or NO ACTION) and ignore ON DELETE or ON UPDATE option is the same.

 

  5. SET DEFAULT: InnoDB is not currently supported.

 

  Foreign key constraint is nothing less than to use the most two cases:

 

  1) the parent table to update the child table is also updated, deleted items when the parent table child table if there is a match, delete failed;

 

  2) the parent table update also updates the child table, the child table entries that match the parent table to delete delete.

 

  In the former case, the foreign key definition, we use the ON UPDATE CASCADE ON DELETE RESTRICT; the latter case, you can use ON UPDATE CASCADE ON DELETE CASCADE.

 

 

 

When the inspection of the foreign key is performed, InnoDB looked after its parent or child records set up a shared row-level locks. InnoDB checks foreign key constraints immediately check not delay the transaction commits.

 

To make table foreign key relationship reload dump files easier, mysqldump automatically dump output includes a statement to set FOREIGN_KEY_CHECKS 0. This avoids problems when the dump is reloaded, the table had to be related to a particular order reloaded. You can also set this variable manually:

 

mysql> SET FOREIGN_KEY_CHECKS = 0;

 

mysql> SOURCE dump_file_name;

 

mysql> SET FOREIGN_KEY_CHECKS = 1;

 

  If the dump file that contains the foreign key table is incorrect order, which in any order to import the table. This also speed up the import operation. FOREIGN_KEY_CHECKS set to 0, is ignored for foreign key constraints in LOAD DATA and ALTER TABLE operations are also very useful.

 

InnoDB table does not allow you to delete a referenced table FOREIGN KEY constraint, unless you do set the SET FOREIGN_KEY_CHECKS = 0. When you remove a table, the constraints defined in its create statement are also removed.

 

  If you re-create a table to be removed, it must comply with a reference to also define its foreign key constraint. It must have the right column names and types, and as described above, it must have an index of key referenced. If these are not satisfied, MySQL returns error number 1005 and directed to errno 150 in the error message string.

Guess you like

Origin blog.csdn.net/weixin_41808843/article/details/88979673