How do I truncate tables properly?

China☆狼群 提交于 2019-11-29 20:53:16
Devart

Plan A:

SET FOREIGN_KEY_CHECKS = 0; -- Disable foreign key checking.
TRUNCATE TABLE forums;
TRUNCATE TABLE dates;
TRUNCATE TABLE remarks;
SET FOREIGN_KEY_CHECKS = 1; -- Enable foreign key checking.

Plan B:

You should truncate child tables firstly, then parent tables.

Disabling foreign key checks risks entering rows into your tables that don't adhere to the constraints which can cause undefined behavior.

Instead of using Disable foreign key checking.

You can use the below code.

DELETE FROM forums;
ALTER TABLE forums AUTO_INCREMENT = 1;

DELETE FROM dates;
ALTER TABLE dates AUTO_INCREMENT = 1;

DELETE FROM remarks;
ALTER TABLE remarks AUTO_INCREMENT = 1;

It will just delete all the rows and make id increment from 1 onwards.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!