You can execute the DELETE statement to delete table data.
Usage notes
The table on which you execute the
DELETEstatement must have a primary key.You cannot use the alias of a table to execute the
DELETEstatement.If you want to delete data from an entire table or partition, we recommend that you use the
TRUNCATE TABLEorTRUNCATE TABLE PARTITIONstatement, instead of theDELETEstatement. For more information, see TRUNCATE TABLE.When you delete data from multiple tables, you can execute an SQL statement only on a single table.
Syntax
When you delete data from a single table, you can query data from the table and delete the data that meets the conditions specified in the WHERE clause. When you delete data from multiple tables, you can execute an SQL statement to query data from multiple joined tables and delete the data that meets the conditions specified in the WHERE clause from one table.
Delete data from a single table
DELETE FROM table_name
[ WHERE condition ] Delete data from multiple tables
You can delete data from multiple tables only for AnalyticDB for MySQL clusters of V3.2.0.0 or later.
To view and update the minor version of an AnalyticDB for MySQL cluster, log on to the AnalyticDB for MySQL console and go to the Configuration Information section of the Cluster Information page.
DELETE table_name1
FROM table_name1 [INNER JOIN | LEFT JOIN | RIGHT JOIN] table_name2 ON table_name1.column1 = table_name2.column1
[WHERE where_condition]Examples
Delete the data whose
namecolumn isAlexfrom the customer table.DELETE FROM customer WHERE customer_name='Alex';Delete the data whose age column is less than 18 from the customer table.
DELETE FROM customer WHERE age<18;Query data from joined tables based on the
idcolumn of the customer table and theidcolumn of the orders table, and delete the data whoseagecolumn is 18 from the customer table.DELETE customer FROM customer JOIN orders ON customer.id = orders.id WHERE customer.age = 18;
Common errors and troubleshooting
Why does the storage space remain unreduced after I delete data from a table?
Cause: DELETE operations are asynchronous. After you execute the DELETE statement to delete data from a table, the storage space is not immediately reduced.
Solution: After you execute the DELETE statement, trigger a BUILD job.
A BUILD job can be automatically triggered after an AnalyticDB for MySQL cluster meets specific conditions. You can also manually trigger a BUILD job.