Today, when you add a foreign key to a mysql table, you always report an error. Here is the SQL statement:
Alter table A
Add constraint FK_1 Foreign Key (‘ relation_ID ‘) References B(‘ id ‘) on UPDATE Cascade on DELETE Cascade
Error code: 1452
See the table is also no problem, and the field of RELATION_ID is no foreign key ah ~~~
Finally, a solution is found in Google. The general method is as follows:
mysql> SET foreign_key_checks = 0; mysql> alter table tblUsedDestination add constraint f_operatorId foreign key(iOperatorId) references tblOperators (iOperatorId); Query OK, 8 rows affected (0.23 sec) Records: 8 Duplicates: 0 Warnings: 0 mysql> SET foreign_key_checks = 1;
Make foreign_key_checks invalid first, then add foreign keys to the table, and finally make foreign_key_checks effective!
There is a reason for foreign_key_checks. If you cannot add a foreign key because it violates the constraint, you should correct the data first. Turning off checks and then adding keys puts you in an inconsistent state. Foreign key checks add overhead, and if you don’t want to use them, use myisam instead of
Read More:
- MySQL failed to add foreign key: SQL 1452 cannot add or update a child row:a foreign key constraint fails
- Solution to error 1452: cannot add or update a child row: a foreign key constraint failures in MySQL
- MYSQL ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails
- Mysql error when deleting the table structure: Error Code: 1217. Cannot delete or update a parent row: a foreign key constraint fails
- MySQL error code 1217 (ER_ROW_IS_REFERENCED): Cannot delete or update a parent row: a foreign key co
- MySql ERROR CODE:1215 Cannot add the foreign key constraint
- Failed to add the foreign key constraint. Missing index for constraint ‘stu_ibfk_1’ in the reference
- 1822 – Failed to add the foreign key constraint. Missing index for constraint ‘tb_emp_ibfk_1’ in the
- Ora-02292: integrity constraint (XX) violated – child record found foreign key Association, record cannot be deleted
- MySQL failed to add foreign key error 1452
- MySQL:Foreign key fails with error Error Code: 1452.
- MySQL error 1451 23000 foreign key exception handling
- About MySQL error: subquery returns more than 1 row
- Matlab delete row or col to delete the row or column of the matrix
- MySQL: if the remote connection using navicatip fails, prompt “is not allowed to connect to this MySQL server”
- mysql Error Code: 1022. Can’t write; duplicate key in table `xxx`
- mysql5.7.26:[ERR] 1118 – Row size too large (> 8126)
- Syntax error or access violation: 1071 specified key was too long; max key length is 767 bytes
- About node error NPM update check failed try running with sudo or get access to the local update
- Global lock, table lock and row lock in MySQL