Home>Article>Database> How to clear data in the table in mysql database

How to clear data in the table in mysql database

little bottle
little bottle forward
2019-04-28 14:55:45 9913browse






#This article mainly talks about using the clear command in the database. It has certain learning value. If necessary, Friends can find out, I hope it can be helpful to you.

When doing data migration, data cleaning or writing web projects, the data needs to be replaced and updated, so sometimes the table needs to be cleared

Commonly used clearing There are two types of SQL statements for the data table:

delete from 表名;
truncate table 表名;

Run the test

I am using MySql. The table to be tested has 20,000 records. Make two copies of it for testing.

Run two SQL statements to clear the table respectively

It can be seen from the results that both statements can achieve the purpose of clearing the table, and the difference between the two is:

truncate is more efficient than delete

truncate does not clear the data. Recording logs, data cannot be restored, which is equivalent to retaining the structure of the mysql table. This table is re-created, and all states are equivalent to new tables.

Delete clears the data and records logs. Data can be restored, which is equivalent to Delete all the records in the table one by one

Which method to choose depends on the actual situation. I usually use the delete method. Although it is slower, it is safer

Related tutorials:

mysql video tutorial

The above is the detailed content of How to clear data in the table in mysql database. For more information, please follow other related articles on the PHP Chinese website!

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