MySQL failed to add foreign keys, error 1452
For example:
Two tables user table: User class table: Grade
Each user in the user table corresponds to a class ID, namely gradeId
Namely: The primary key of the User table gradeId is the Id in the Grade table
The user table is called the primary table and the Grade table is called the slave table
[SQL]
For example:
Two tables user table: User class table: Grade
Each user in the user table corresponds to a class ID, namely gradeId
Namely: The primary key of the User table gradeId is the Id in the Grade table
The user table is called the primary table and the Grade table is called the slave table
[SQL]
alter table user
Add Foreign Key (gradeId) References Grade (Id);
[error]
ERROR 1452 : Cannot add or update a child row: a foreign key constraint fails
“Why”
Some gradeId in the User table do not belong to the ID in the Grade table
【 Solution 】
Modify the data so that all gradeId in the User table belong to ids in the Grade table
Read More:
- MySQL failed to add foreign key: SQL 1452 cannot add or update a child row:a foreign key constraint fails
- Mysql error 1452 – Cannot add or update a child row: a foreign key constraint fails
- MySql ERROR CODE:1215 Cannot add the foreign key constraint
- Solution to error 1452: cannot add or update a child row: a foreign key constraint failures in MySQL
- 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
- MySQL error 1451 23000 foreign key exception handling
- MySQL:Foreign key fails with error Error Code: 1452.
- MYSQL ERROR 1217 (23000): 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 when deleting the table structure: Error Code: 1217. Cannot delete or update a parent row: a foreign key constraint fails
- Ora-02292: integrity constraint (XX) violated – child record found foreign key Association, record cannot be deleted
- mysql Error Code: 1022. Can’t write; duplicate key in table `xxx`
- Add samba user prompt Failed to add entry for user.
- Solution of duplicate entry ‘value’ for key ‘field name’ in MySQL
- Add Samba user prompt failed to add entry for user
- [reprint and save] MySQL does not set the primary key and uses the self growing ID method
- MySQL skip grant tables add user error 1290
- The solution of duplicate entry ‘for key’ primary ‘when inserting data in MySQL
- Txt import MySQL: error 1062 (23000): duplicate entry ‘0’ for key ‘primary’