Home Database Mysql Tutorial mysql——delete syntax

mysql——delete syntax

Nov 23, 2016 am 10:30 AM
delete mysql

Single table syntax:

DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name
    [WHERE where_definition]
    [ORDER BY ...]
    [LIMIT row_count]
Copy after login

Multiple table syntax:

DELETE [LOW_PRIORITY] [QUICK] [IGNORE]
    tbl_name[.*] [, tbl_name[.*] ...]
    FROM table_references
    [WHERE where_definition]
Copy after login

or:

DELETE [LOW_PRIORITY] [QUICK] [IGNORE]
    FROM tbl_name[.*] [, tbl_name[.*] ...]
    USING table_references
    [WHERE where_definition]
Copy after login

Some rows in tbl_name satisfy the conditions given by where_definition. DELETE is used to delete these rows and returns the number of deleted records.

If you write a DELETE statement without a WHERE clause, all rows will be deleted. When you don't want to know the number of deleted rows, there is a faster way, which is to use TRUNCATE TABLE.

If the row you delete includes the maximum value for the AUTO_INCREMENT column, that value is reused in the BDB table, but not in the MyISAM table or the InnoDB table. If you use DELETE FROM tbl_name (without a WHERE clause) in AUTOCOMMIT mode to delete all rows in a table, the sequence is reordered for all table types (except InnoDB and MyISAM).

For MyISAM and BDB tables, you can specify the AUTO_INCREMENT secondary column into a multi-column keyword. In this case, the value removed from the top of the sequence is used again, even for MyISAM tables.

The DELETE statement supports the following modifiers:

· If you specify LOW_PRIORITY, the execution of DELETE is delayed until no other client reads this table.

· For MyISAM tables, if you use the QUICK keyword, the storage engine will not merge the index end nodes during the deletion process, which can speed up some types of deletion operations.

· During the process of deleting rows, the IGNORE keyword causes MySQL to ignore all errors. (Errors encountered during the analysis phase are handled in the normal manner.) Errors that are ignored due to the use of this option are returned as warnings.

In MyISAM tables, deleted records are kept in a linked list, and subsequent INSERT operations will reuse the old record location. To reuse unused space and reduce the file size, use the OPTIMIZE TABLE statement or the myisamchk application to reorganize the table. OPTIMIZE TABLE is simpler, but myisamchk is faster.

QUICK modifier will affect whether the index end nodes are merged during the deletion operation. DELETE QUICK is most useful when the index value for the deleted row is replaced by a similar index value from a later inserted row. In this case, the holes left by the deleted values ​​are reused.

If the index block that is not full spans a certain range of index values, a new insertion will occur. DELETE QUICK has no effect when the deleted value results in an underfilled index block. In this case, using QUICK can result in waste space in unused indexes. The following is an example of this situation:

1. Create a table that contains the indexed AUTO_INCREMENT column.

2. Insert many records into the table. Each insertion produces an index value, which is added to the high end of the index.

3. Use DELETE QUICK to delete a group of records from the low end of the column.

In this case, the index blocks related to the deleted index values ​​become underfilled, but due to the use of QUICK, these index blocks will not be merged with other index blocks. When new values ​​are inserted, these index blocks remain underfilled because the new records do not contain index values ​​within the deleted range. In addition, even if you later use DELETE without including QUICK, these index blocks will still be unfilled, unless some of the deleted index values ​​happen to be in or adjacent to these unfilled blocks. In these cases, if you want to reuse unused index space, use OPTIMIZE TABLE.

If you plan to delete many rows from a table, using DELETE QUICK coupled with OPTIMIZE TABLE can speed things up. Doing so re-indexes rather than doing a large number of index block merge operations.

MySQL’s only LIMIT row_count option for DELETE is used to tell the server the maximum number of rows to be deleted before the control command is returned to the client. This option is used to ensure that a DELETE statement does not take up too much time. You can just repeat the DELETE statement until the number of relevant rows is less than the LIMIT value.

If the DELETE statement includes an ORDER BY clause, the rows are deleted in the order specified in the clause. This clause only works when used in conjunction with LIMIT. For example, the following clause is used to find rows corresponding to the WHERE clause, use timestamp_column for classification, and delete the first (oldest) row:

DELETE FROM somelog
WHERE user = 'jcole'
ORDER BY timestamp_column
LIMIT 1;
Copy after login

You can specify multiple tables in one DELETE statement, based on multiple Deletes rows from a table or multiple tables based on specific conditions in the table. However, you cannot use ORDER BY or LIMIT in a multi-table DELETE statement.

table_references section lists the tables included in the union.

For the first syntax, only delete the corresponding rows in the table listed before the FROM clause. For the second syntax, only the corresponding rows in the table listed in the FROM clause (before the USING clause) are deleted. What this does is, you can delete rows from many tables at the same time and search using the other tables:

DELETE t1, t2 FROM t1, t2, t3 WHERE t1.id=t2.id AND t2.id=t3.id;
Copy after login

or:

DELETE FROM t1, t2 USING t1, t2, t3 WHERE t1.id=t2.id AND t2.id=t3.id;
Copy after login

When searching for rows to be deleted, these statements use all three tables, but only from the table Delete the corresponding rows in t1 and table t2.

The above example shows an inner join using the comma operator, but the multi-table DELETE statement can use all types of joins allowed in the SELECT statement, such as LEFT JOIN.

本语法允许在名称后面加.*,以便与Access相容。

如果您使用的多表DELETE语句包括InnoDB表,并且这些表受外键的限制,则MySQL优化程序会对表进行处理,改变原来的从属关系。在这种情况下,该语句出现错误并返回到前面的步骤。要避免此错误,您应该从单一表中删除,并依靠InnoDB提供的ON DELETE功能,对其它表进行相应的修改。

注释:当引用表名称时,您必须使用别名(如果已给定):

DELETE t1 FROM test AS t1, test2 WHERE ...
Copy after login

进行多表删除时支持跨数据库删除,但是在此情况下,您在引用表时不能使用别名。举例说明:

DELETE test1.tmp1, test2.tmp2 FROM test1.tmp1, test2.tmp2 WHERE ...
Copy after login

目前,您不能从一个表中删除,同时又在子查询中从同一个表中选择。


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Centos install mysql Centos install mysql Apr 14, 2025 pm 08:09 PM

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

See all articles