This means you need to set foreign_key_check to disabled first and then you need to truncate the table. The syntax is as follows -
set FOREIGN_KEY_CHECKS = 0; TRUNCATE TABLE yourTableName1; TRUNCATE TABLE yourTableName2; TRUNCATE TABLE yourTableName3; . . . . TRUNCATE TABLE yourTableNameN; set FOREIGN_KEY_CHECKS = 1;
Now, let’s truncate some tables from our database test. The query is as follows -
mysql> set FOREIGN_KEY_CHECKS = 0; Query OK, 0 rows affected (0.00 sec) mysql> truncate table skiplasttenrecords; Query OK, 0 rows affected (0.97 sec) mysql> truncate table searchtextdemo; Query OK, 0 rows affected (0.89 sec) mysql> set FOREIGN_KEY_CHECKS = 1; Query OK, 0 rows affected (0.00 sec)
Cross-check whether the data exists in the table -
mysql> select *from searchtextdemo; Empty set (0.00 sec) mysql> select *from skiplasttenrecords; Empty set (0.00 sec)
The empty set means there are no records in the table.
The above is the detailed content of How to correctly truncate a table in MySQL?. For more information, please follow other related articles on the PHP Chinese website!