Delete and truncate in mysql

Source: Internet
Author: User


In mysql databases, the delete and truncate statements and the truncate table statements can be used to DELETE data. However, they are different, the following describes two common methods for empty mysqll table content: delete and truncate. The delete statement without the where parameter can delete all content in the mysql table. You can also use the truncate table to clear all content in the mysql table. In terms of efficiency, truncate is faster than delete, but after truncate is deleted, mysql logs are not recorded and data cannot be recovered. Www.2cto.com 1. The syntax structure of truncate is: SQL code TRUNCATE [TABLE] tbl_name. Below is a simple example: to delete all the records in the test TABLE, you can use the following statement: SQL code truncate table test; 2. delete is a bit like deleting all records in the mysql table, and truncate is equivalent to retaining the structure of the mysql table and re-creating the table, all statuses are equivalent to new tables. Its syntax structure is: SQL code DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name [WHERE where_definition] [ORDER BY...] [LIMIT row_count] According to the above syntax structure, you can use the following SQL statement to delete all records whose infoId is equal to abced in the test table: SQL code delete from test where infoId = 'abced '; Note: from the syntax structure, we can see that, like the update syntax, We can omit the where clause. However, this is a very dangerous behavior. If the where clause is not specified, delete will delete all records in the table and delete them immediately. Even if you want to cry, there is no time, because you need to acknowledge the error with the supervisor immediately, and immediately finds mysql logs and rolls back and forth records. In summary, the truncate command is fast and fast, but it is not as secure as the delete command for transaction processing. Therefore, if the table to be deleted by truncate is being processed in a transaction, this command will generate an exit and generate an error message.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.