DELETE
Function
DELETE deletes rows that satisfy the WHERE clause from the specified table. If the WHERE clause is absent, the effect is to delete all rows in the table. The result is a valid, but an empty table.
Precautions
- The owner of a table, users granted with the DELETE permission on the table, or users granted with the DELETE ANY TABLE permission can delete data from the table. The system administrator has the permission to delete data from the table by default, as well as the SELECT permission on any table in the USING clause or whose values are read in condition.
- For row-store tables, DELETE can be used if the tables have primary key constraints or the execution plan can be pushed down.
- For column-store tables, DELETE can be used only when the execution plan can be pushed down.
- For column-store tables, the RETURNING clause is currently not supported.
- For a time series table, only deletion by time is supported. The RETURNING clause is not supported.
Syntax
1 2 3 4 5 |
[ WITH [ RECURSIVE ] with_query [, ...] ] DELETE [/*+ plan_hint */] [FROM] [ ONLY ] table_name [ * ] [ [ AS ] alias ] [ USING using_list ] [ WHERE condition | WHERE CURRENT OF cursor_name ] [ RETURNING { * | { output_expr [ [ AS ] output_name ] } [, ...] } ]; |
Parameter Description
- WITH [ RECURSIVE ] with_query [, ...]
Specifies one or more subqueries that can be referenced by name in the main query, which is equivalent to a temporary table.
If RECURSIVE is specified, it allows a SELECT subquery to reference itself by name.
Format of with_query:
with_query_name [ ( column_name [, ...] ) ] AS [ [ NOT ] MATERIALIZED ] ( {select | values | insert | update | delete} )
– with_query_name specifies the name of the result set generated by a subquery. Such names can be used to access the result sets of subqueries in a query.
– column_name specifies the column name displayed in the subquery result set.
– Each subquery can be a SELECT, VALUES, INSERT, UPDATE or DELETE statement.
– You can use MATERIALIZED or NOT MATERIALIZED to modify the CTE.- If MATERIALIZED is specified, the WITH query will be materialized, and a copy of the subquery result set is generated. The copy is directly queried at the reference point. Therefore, the WITH subquery cannot be jointly optimized with the SELECT statement trunk (for example, predicate pushdown and equivalence class transfer). In this scenario, you can use NOT MATERIALIZED for modification. If the WITH query can be executed as a subquery inline, the preceding optimization can be performed.
- If the user does not explicitly declare the materialized attribute, comply with the following rules: If the CTE is referenced only once in the trunk statement to which it belongs and semantically supports inline execution, it will be rewritten as subquery inline execution. Otherwise, the materialized execution will be performed in CTE Scan mode.
- plan_hint clause
Follows the DELETE keyword in the /*+ */ format. It is used to optimize the plan of a DELETE statement block. For details, see Hint-based Tuning. In each statement, only the first /*+ plan_hint */ comment block takes effect as a hint. Multiple hints can be written.
- ONLY
If ONLY is specified before the table name, matching rows are deleted from the named table only. If ONLY is not specified, matching rows are also deleted from any tables inheriting from the named table.
- table_name
Specifies the name (optionally schema-qualified) of the table to delete rows from.
Value range: an existing table name
- alias
Specifies a substitute name for the target table.
Value range: a string. It must comply with the identifier naming convention.
- using_list
Specifies the USING clause.
- condition
Specifies an expression that returns a value of type Boolean. Only rows for which this expression returns true will be deleted. You are not advised to use numeric types such as int for condition, because such types can be implicitly converted to bool values (non-zero values are implicitly converted to true and 0 is implicitly converted to false), which may cause unexpected results.
- WHERE CURRENT OF cursor_name
This parameter is reserved.
- output_expr
Specifies an expression to be computed and returned by the DELETE statement after each row is deleted. The expression can use any column names of the table. Write * to return all columns.
- output_name
Specifies a name to use for a returned column.
Value range: a string. It must comply with the identifier naming convention.
Examples
1 2 3 4 5 6 7 8 9 10 11 |
-- Create the tpcds.customer_address_bak table. openGauss=# CREATE TABLE tpcds.customer_address_bak AS TABLE tpcds.customer_address; -- Delete employees whose ca_address_sk is smaller than 14888 from the tpcds.customer_address_bak table. openGauss=# DELETE FROM tpcds.customer_address_bak WHERE ca_address_sk < 14888; -- Delete all data from the tpcds.customer_address_bak table. openGauss=# DELETE FROM tpcds.customer_address_bak; Delete the tpcds.customer_address_bak table. openGauss=# DROP TABLE tpcds.customer_address_bak; |
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot