MySQL – Disable Foreign Key Checks or Constraints
A Relational database management system (RDBMS) is a database management system (DBMS) that is based on the relational model as introduced by E. F. Codd. RDBMS is the basis for SQL, and for all modern database systems like MS SQL Server, Oracle and MySQL.So came into existence the concepts of primary keys, foreign keys, foreign key constraints and whole bunch of other terms like composite keys, referential integrity, indexes .
Most of the time we use MySQL because it is opensource and free. We maintain relations using primary key and foreign keys. When a foreign key constraint exists we can not delete the main record unless we delete the dependent foriegn key data. In that case we have to set the foreign key checks for database .
When we want delete some user records in tbl_user without deleting roles tbl_roles and deleting pivot table tbl_user_roles. we use foreign key checks following way.
SET foreign_key_checks = 0; DELETE FROM tbl_users WHERE id=7; DELETE FROM tbl_users_roles WHERE user_id=7; SET foreign_key_checks = 1;