Truncate table with foreign key constraints

Truncate table with foreign key constraints

When handling MySQL migrations or other testing instances on databases you often want to delete data from the database. But of course your applications uses constraints. Why wouldn't it, it makes the application development way safer. But it's annoying while doing the testing. So when you're on the sql, you can directly disable the checks. Just don't forget to enable them afterwards:

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

The same applies for deleting tables if there are constraints on it.