DELETE
Synopsis
The DELETE
statement removes rows from a specified table that meet a given condition. Currently, YugabyteDB can deletes one row at a time. Deleting multiple rows is not yet supported.
Syntax
Diagram
Grammar
delete ::= DELETE FROM table_name
[ USING TIMESTAMP timestamp_expression ]
WHERE where_expression
[ IF { [ NOT ] EXISTS | if_expression } ];
Where
table_name
is an identifier (possibly qualified with a keyspace name).- Restrictions on
where_expression
andif_expression
are covered in the Semantics section below. - See Expressions for more information on syntax rules.
Semantics
- An error is raised if the specified
table_name
does not exist. - The
where_expression
andif_expression
must evaluate to boolean values. - The
USING TIMESTAMP
clause indicates we would like to perform the DELETE as if it was done at thetimestamp provided by the user. The timestamp is the number of microseconds since epoch. - NOTE: You should either use the
USING TIMESTAMP
clause in all of your statements or none ofthem. Using a mix of statements where some haveUSING TIMESTAMP
and others do not will lead tovery confusing results.
WHERE Clause
- The
where_expression
must specify conditions for all primary-key columns. - The
where_expression
must not specify conditions for any regular columns. - The
where_expression
can only applyAND
and=
operators. Other operators are not yet supported.
IF Clause
- The
if_expression
can only apply to non-key columns (regular columns). - The
if_expression
can contain any logical and boolean operators. - Deleting only some column values from a row is not yet supported.
IF EXISTS
andIF NOT EXISTS
options are mostly for symmetry with theINSERT
andUPDATE
commands.IF EXISTS
works like a normal delete but additionally returns whether the delete was applied (a row was found with that primary key).IF NOT EXISTS
is effectively a no-op since rows that do not exist cannot be deleted (but returns whether no row was found with that primary key).
USING Clause
timestamp_expression
must be an integer value (or a bind variable marker for prepared statements).
Examples
Delete a row from a table
cqlsh:example> CREATE TABLE employees(department_id INT,
employee_id INT,
name TEXT,
PRIMARY KEY(department_id, employee_id));
cqlsh:example> INSERT INTO employees(department_id, employee_id, name) VALUES (1, 1, 'John');
cqlsh:example> INSERT INTO employees(department_id, employee_id, name) VALUES (1, 2, 'Jane');
cqlsh:example> INSERT INTO employees(department_id, employee_id, name) VALUES (2, 1, 'Joe');
cqlsh:example> SELECT * FROM employees;
department_id | employee_id | name
---------------+-------------+------
1 | 1 | John
1 | 2 | Jane
2 | 1 | Joe
Delete statements identify rows by the primary key columns.
cqlsh:example> DELETE FROM employees WHERE department_id = 1 AND employee_id = 1;
Deletes on non-existent rows are no-ops.
cqlsh:example> DELETE FROM employees WHERE department_id = 3 AND employee_id = 1;
cqlsh:example> SELECT * FROM employees;
department_id | employee_id | name
---------------+-------------+------
1 | 2 | Jane
2 | 1 | Joe
Conditional delete using the IF clause
‘IF’ clause conditions will return whether they were applied or not.
cqlsh:example> DELETE FROM employees WHERE department_id = 2 AND employee_id = 1 IF name = 'Joe';
[applied]
[applied]
True
cqlsh:example> DELETE FROM employees WHERE department_id = 3 AND employee_id = 1 IF EXISTS;
[applied]
[applied]
False
cqlsh:example> SELECT * FROM employees;
department_id | employee_id | name
---------------+-------------+------
1 | 2 | Jane
Delete several rows with the same partition key
cqlsh:example> INSERT INTO employees(department_id, employee_id, name) VALUES (1, 1, 'John');
cqlsh:example> INSERT INTO employees(department_id, employee_id, name) VALUES (2, 1, 'Joe');
cqlsh:example> INSERT INTO employees(department_id, employee_id, name) VALUES (2, 2, 'Jack');
cqlsh:example> SELECT * FROM employees;
department_id | employee_id | name
---------------+-------------+------
1 | 1 | John
1 | 2 | Jane
2 | 1 | Joe
2 | 2 | Jack
Delete all entries for a partition key.
cqlsh:example> DELETE FROM employees WHERE department_id = 1;
cqlsh:example> SELECT * FROM employees;
department_id | employee_id | name
---------------+-------------+------
2 | 1 | Joe
2 | 2 | Jack
Delete a range of entries within a partition key.
cqlsh:example> DELETE FROM employees WHERE department_id = 2 AND employee_id >= 2 AND employee_id < 4;
cqlsh:example> SELECT * FROM employees;
department_id | employee_id | name
---------------+-------------+------
2 | 1 | Joe
Delete with the USING TIMESTAMP clause
You can do this as shown below.
cqlsh:foo> INSERT INTO employees(department_id, employee_id, name) VALUES (4, 4, 'Ted') USING TIMESTAMP 1000;
cqlsh:foo> SELECT * FROM employees;
department_id | employee_id | name
---------------+-------------+------
4 | 4 | Ted
2 | 1 | Joe
(2 rows)
cqlsh:foo> DELETE FROM employees USING TIMESTAMP 500 WHERE department_id = 4 AND employee_id = 4;
Not applied since timestamp is lower than 1000
cqlsh:foo> SELECT * FROM employees;
department_id | employee_id | name
---------------+-------------+------
4 | 4 | Ted
2 | 1 | Joe
(2 rows)
cqlsh:foo> DELETE FROM employees USING TIMESTAMP 1500 WHERE department_id = 4 AND employee_id = 4;
Applied since timestamp is higher than 1000.
cqlsh:foo> SELECT * FROM employees;
department_id | employee_id | name
---------------+-------------+------
2 | 1 | Joe
(1 rows)
See also
当前内容版权归 YugabyteDB 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 YugabyteDB .