Tuesday 7 February 2023

How to truncate a foreign key constrained table?

 Recent time I've faced challenge while mysql table truncation data,

Cannot truncate a table referenced in a foreign key constraint 

In above case error, 

MySQL Query: 

  SET FOREIGN_KEY_CHECKS = 0;

  truncate table mydb.TableName

  SET FOREIGN_KEY_CHECKS = 1;

You could able to do truncate entire table which having foreign key as well.