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

How to correctly truncate a table in MySQL?

王林
Release: 2023-09-20 21:25:06
forward
1077 people have browsed it

如何在 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;
Copy after login

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)
Copy after login

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)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template