Home  >  Article  >  Database  >  How to correctly truncate a table in MySQL?

How to correctly truncate a table in MySQL?

王林
王林forward
2023-09-20 21:25:06995browse

如何在 MySQL 中正确截断表?

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete