Nov 8, 2018

How to truncate tables which are referenced by a FOREIGN KEY constraints in MySQL

SET FOREIGN_KEY_CHECKS=0;
TRUNCATE table1;
TRUNCATE table2;
SET FOREIGN_KEY_CHECKS=1;


The setting of FOREIGN_KEY_CHECKS only applies on current session. If you want to continue other works in current session, then you "SET FOREIGN_KEY_CHECKS=1;" in the end. Otherwise you don't need to. You can use

show variables like 'FOREIGN_KEY_CHECKS';

to verify.