Solution : Cannot delete or update a parent row: a foreign key constraint fails

Whenever you want to delete a particular row from a parent table, usually a database engine checks whether this parent data primary key value refers to any child table data.

For Example,

Table: user

id
login_Id
user_Name
1
demo001
Note
2
demo002
Book

Table: userAddress

id
user_id
Country
State
1
1
India
Himachal Pradesh
2
2
India
Tamil Nadu

Primary Key
Foreign Key

Table UserAddress has “user_id” is a foreign key reference from Table User.

So User Table is Parent Table and UserAddress Table is Child Table

If you are try to delete any data in child table then you might be not facing an error but if you try to delete any data in parent table then you will face problem “Cannot delete or update a parent row: a foreign key constraint fails” because parent table data is referencing to child table.

Execute this Query

SET FOREIGN_KEY_CHECKS = 0;

After executing the above query

Now try to delete the parent table data which is referencing to child table data then database engine won’t be checking and it will delete without any problem.

To Do the Reset

SET FOREIGN_KEY_CHECKS = 1;

Popular posts from this blog

Database Multi Language Support Format

Five More Indian Languages - Google Translate