MySQL Delete Data Delete and truncate statement comparison

Source: Internet
Author: User
Tags mysql delete

Its grammatical structure is:

The code is as follows Copy Code
TRUNCATE [TABLE] Tbl_name

Here's a simple example,

I want to delete all the records in the Friends table, you can use the following statement:

The code is as follows Copy Code

TRUNCATE TABLE friends;

The effect of the delete is a bit like deleting all the records in the MySQL table to the end, and truncate is equivalent to preserving the MySQL table structure, recreating the table, and all States are equivalent to the new table.

Grammar:

The definition of the DELETE statement.

The code is as follows Copy Code

DELETE [low_priority] [QUICK] [IGNORE] from Tbl_name

[WHERE Where_definition]

[Order BY ...]

[LIMIT Row_count]

MySQL's syntax is similar to spoken language, you have to point out which table you want to delete data, and delete which data, this is enough. Just like when writing a narrative, time, place, character, environment and plot elements are essential.
The example is the most image and the most descriptive question. So, not the whole useless, directly into the subject. Following the syntax structure above, I want to delete all the user_name equals Simaopig records in the Firends table,
You can use the following SQL statement:

The code is as follows Copy Code

Delete from friends where user_name = ' Simaopig ';d elete

Note:
From the syntax structure, we can see that, like the update syntax, we can omit the WHERE clause. But this is a very dangerous behavior. Because if you do not specify a WHERE clause, delete deletes all the records in the table and deletes them immediately, even if you have no place to cry, and no time, because you need to immediately acknowledge the error with the supervisor, and immediately find the MySQL log and roll back the record. But once you have had such an experience, I believe it must be impressive.


The strange thing is that I am on the InnoDB and MyISAM on the large amount of data, the index file under great circumstances, it is almost impossible to delete the success! Left to settle.

Summarize

However, the truncate command is fast, but it is not as secure as the delete command for transaction processing. Therefore, if we want to perform a transaction on a table that truncate deletes, this command generates an exit and generates 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.