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

DELETE - 图1

Grammar

  1. delete ::= DELETE FROM table_name
  2. [ USING TIMESTAMP timestamp_expression ]
  3. WHERE where_expression
  4. [ IF { [ NOT ] EXISTS | if_expression } ];

Where

  • table_name is an identifier (possibly qualified with a keyspace name).
  • Restrictions on where_expression and if_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 and if_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 have USING 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 apply AND 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 and IF NOT EXISTS options are mostly for symmetry with the INSERT and UPDATE 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

  1. cqlsh:example> CREATE TABLE employees(department_id INT,
  2. employee_id INT,
  3. name TEXT,
  4. PRIMARY KEY(department_id, employee_id));
  1. cqlsh:example> INSERT INTO employees(department_id, employee_id, name) VALUES (1, 1, 'John');
  1. cqlsh:example> INSERT INTO employees(department_id, employee_id, name) VALUES (1, 2, 'Jane');
  1. cqlsh:example> INSERT INTO employees(department_id, employee_id, name) VALUES (2, 1, 'Joe');
  1. cqlsh:example> SELECT * FROM employees;
  1. department_id | employee_id | name
  2. ---------------+-------------+------
  3. 1 | 1 | John
  4. 1 | 2 | Jane
  5. 2 | 1 | Joe

Delete statements identify rows by the primary key columns.

  1. cqlsh:example> DELETE FROM employees WHERE department_id = 1 AND employee_id = 1;

Deletes on non-existent rows are no-ops.

  1. cqlsh:example> DELETE FROM employees WHERE department_id = 3 AND employee_id = 1;
  1. cqlsh:example> SELECT * FROM employees;
  1. department_id | employee_id | name
  2. ---------------+-------------+------
  3. 1 | 2 | Jane
  4. 2 | 1 | Joe

Conditional delete using the IF clause

‘IF’ clause conditions will return whether they were applied or not.

  1. cqlsh:example> DELETE FROM employees WHERE department_id = 2 AND employee_id = 1 IF name = 'Joe';

  1. [applied]

  1. True

  1. cqlsh:example> DELETE FROM employees WHERE department_id = 3 AND employee_id = 1 IF EXISTS;

  1. [applied]

  1. False

  1. cqlsh:example> SELECT * FROM employees;
  1. department_id | employee_id | name
  2. ---------------+-------------+------
  3. 1 | 2 | Jane

Delete several rows with the same partition key

  1. cqlsh:example> INSERT INTO employees(department_id, employee_id, name) VALUES (1, 1, 'John');
  1. cqlsh:example> INSERT INTO employees(department_id, employee_id, name) VALUES (2, 1, 'Joe');
  1. cqlsh:example> INSERT INTO employees(department_id, employee_id, name) VALUES (2, 2, 'Jack');
  1. cqlsh:example> SELECT * FROM employees;
  1. department_id | employee_id | name
  2. ---------------+-------------+------
  3. 1 | 1 | John
  4. 1 | 2 | Jane
  5. 2 | 1 | Joe
  6. 2 | 2 | Jack

Delete all entries for a partition key.

  1. cqlsh:example> DELETE FROM employees WHERE department_id = 1;
  1. cqlsh:example> SELECT * FROM employees;
  1. department_id | employee_id | name
  2. ---------------+-------------+------
  3. 2 | 1 | Joe
  4. 2 | 2 | Jack

Delete a range of entries within a partition key.

  1. cqlsh:example> DELETE FROM employees WHERE department_id = 2 AND employee_id >= 2 AND employee_id < 4;
  1. cqlsh:example> SELECT * FROM employees;
  1. department_id | employee_id | name
  2. ---------------+-------------+------
  3. 2 | 1 | Joe

Delete with the USING TIMESTAMP clause

You can do this as shown below.

  1. cqlsh:foo> INSERT INTO employees(department_id, employee_id, name) VALUES (4, 4, 'Ted') USING TIMESTAMP 1000;
  1. cqlsh:foo> SELECT * FROM employees;
  1. department_id | employee_id | name
  2. ---------------+-------------+------
  3. 4 | 4 | Ted
  4. 2 | 1 | Joe
  5. (2 rows)
  1. cqlsh:foo> DELETE FROM employees USING TIMESTAMP 500 WHERE department_id = 4 AND employee_id = 4;

Not applied since timestamp is lower than 1000

  1. cqlsh:foo> SELECT * FROM employees;
  1. department_id | employee_id | name
  2. ---------------+-------------+------
  3. 4 | 4 | Ted
  4. 2 | 1 | Joe
  5. (2 rows)
  1. cqlsh:foo> DELETE FROM employees USING TIMESTAMP 1500 WHERE department_id = 4 AND employee_id = 4;

Applied since timestamp is higher than 1000.

  1. cqlsh:foo> SELECT * FROM employees;
  1. department_id | employee_id | name
  2. ---------------+-------------+------
  3. 2 | 1 | Joe
  4. (1 rows)

See also