What is the difference between MySQL delete data drop, truncate and delete

There are three common ways to delete data in MySQL, namely DROP, TRUNCATEand DELETE, and their differences are as follows:

  1. DROP

DROPcommand 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. DROPIs 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.

  1. TRUNCATE

TRUNCATEcommand 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, TRUNCATEthe 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.

  1. DELETE

DELETEcommand is used to delete data in a table. Unlike TRUNCATE, DELETEthe operation only deletes the data in the table, but does not delete the table structure, indexes and other content. DELETEThe 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 DELETEneed to manually update the index of the table.
Another point is that DELETEcommands can wheredelete data according to conditions

In general, DROPit is to delete the entire table, TRUNCATEto delete all the data in the table but retain the table structure, DELETEand to delete part of the data in the table. Under normal circumstances, TRUNCATEthe 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 DELETEthe command.

Guess you like

Origin blog.csdn.net/qq_42133976/article/details/130417893