How To Truncate A Foreign Key Constrained Table

Hostnbit

You cannot TRUNCATE a table that has FK constraints applied on it. Becaue Truncate is not same as DELETE.

We can do that by a very simple sql commands.

SET FOREIGN_KEY_CHECKS = 0; 
TRUNCATE table $table_name; 
SET FOREIGN_KEY_CHECKS = 1;

So what does it do

  1. Remove constraints
  2. Perform TRUNCATE
  3. Create constraints
Hostnbit

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Menu Title
Scroll to Top