There are three common ways to delete data in MySQL, namely DROP
, TRUNCATE
and DELETE
, and their differences are as follows:
DROP
DROP
command is used to delete the entire table, including table structure and data. After the command is executed DROP
, the table does not exist, and the table needs to be recreated to operate. DROP
Is a very dangerous operation, because it will completely delete the data in the table, so you need to be very careful before performing this operation.
TRUNCATE
TRUNCATE
command is used to delete all the data in the table, but keep the table structure. After the command is executed TRUNCATE
, all data in the table will be cleared, but the table structure, indexes, triggers, etc. will not be deleted. Compared with DROP
, TRUNCATE
the operation is more efficient because it does not delete the table structure, but directly clears the data in the table. However, it also needs to be used with caution, because once the action is performed, it cannot be undone.
DELETE
DELETE
command is used to delete data in a table. Unlike TRUNCATE
, DELETE
the operation only deletes the data in the table, but does not delete the table structure, indexes and other content. DELETE
The command is also dangerous, because it will delete the data in the table, but it will not automatically update the index in the table, so after executing the command, you DELETE
need to manually update the index of the table.
Another point is that DELETE
commands can where
delete data according to conditions
In general, DROP
it is to delete the entire table, TRUNCATE
to delete all the data in the table but retain the table structure, DELETE
and to delete part of the data in the table. Under normal circumstances, TRUNCATE
the operation efficiency is the highest, but it should be noted that the deleted data is irreversible, so it needs to be used with caution. If you only need to delete part of the data, you can use DELETE
the command.