Delete data in MySQL and truncate-mysql tutorial

Source: Internet
Author: User
Delete data in MySQL -- delete and truncate ??? There are two ways to DELETE data in MySQL: one is the DELETE statement and the other is the TRUNCATETABLE statement .???? The WHERE clause can be used to select the record to be deleted .??? TRUNCATETABLE will delete all records in the table. Therefore, the DELETE statement is more

Delete data in MySQL -- delete and truncate? ? ? There are two ways to DELETE data in MySQL: one is the DELETE statement and the other is the truncate table statement. ? ? ? ? You can use the WHERE clause to select the record to be deleted. ? ? ? Truncate table deletes all records in the TABLE. Therefore, the DELETE statement is more

Delete data in MySQL -- delete and truncate

? ? ?There are two ways to DELETE data in MySQL: one is the DELETE statement and the other is the truncate table statement.

?

?? ? You can use the WHERE clause to select the record to be deleted.

? ? ? Truncate table deletes all records in the TABLE. Therefore, the DELETE statement is more flexible.

?

To clear all records in the table, you can use the following two methods:

? ? ? ? Delete from table1
? ? ? ? Truncate table table1

The TABLE in the second record is optional.

To DELETE some records in a table, you can only use the DELETE statement.

Delete from table1 WHERE ...;

?

If DELETE does not add a WHERE clause, it is the same as the truncate table, but they are a little different. that is, DELETE can return the number of deleted records, while the truncate table returns 0.

If an auto-increment field exists in a TABLE, after deleting all records using truncate table and DELETE without WHERE clause, the auto-increment field restores the starting value to 1. if you do not want to do this, you can add the permanent WHERE statement in the DELETE statement, such as WHERE 1 or WHERE true.

Delete from table1 WHERE 1;

The preceding statement scans each record during execution. But it is not compared, because the WHERE condition is always true. In this way, although the maximum auto-increment value can be maintained, because it scans all records, the execution cost is much greater than the DELETE without the WHERE clause.

  The biggest difference between DELETE and truncate table is that DELETE can use the WHERE statement to select the record to be deleted. However, the execution speed is not fast. You can also return the number of deleted records. The truncate table cannot delete the specified record, and the deleted record cannot be returned. But it runs very fast.

?

Unlike standard SQL statements, DELETE supports the ORDER BY and LIMIT clauses, which allow us to better control the records to be deleted. For example, if you only want to delete part of the records filtered out BY the WHERE clause, you can use LIMIB. if you want to delete the last few records, you can use order by and LIMIT together. Suppose we want to delete the first six records whose name is "Mike" in the users table. You can use the following DELETE statement:

Delete from users WHERE name = 'Mike 'LIMIT 6;

Generally, MySQL does not determine which 6 records are deleted. to be more secure, we can use order by to sort the records.

Delete from users WHERE name = 'Mike 'order by id desc limit 6;

?

From: http://blog.csdn.net/apache6/article/details/2778878

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.