Skip to content

DELETE statement

View as Markdown

The DELETE statement removes rows from one or more tables. A WHERE clause selects the rows to remove; without one, every row of the table is deleted. To empty a whole table quickly, use TRUNCATE instead.

-- one table
DELETE [FROM] table_name [WHERE <search_condition>] [LIMIT row_count]
-- several tables, FROM form
DELETE table_name [, table_name ...] FROM <table_specifications> [WHERE <search_condition>]
-- several tables, USING form
DELETE FROM table_name [, table_name ...] USING <table_specifications> [WHERE <search_condition>]
  • table_name: the table to delete rows from. With a single table, the FROM keyword is optional. In the multi-table forms, list the tables (or their aliases) whose rows are deleted.
  • table_specifications: the tables to read, written like the FROM clause of a SELECT, including joins. Aliases are defined here and can be used in the rest of the statement.
  • search_condition: only rows that satisfy the condition are deleted.
  • row_count: the maximum number of rows to delete. It can be an unsigned integer, a host variable or a simple expression. LIMIT is allowed only when one table is involved.

Order 455 has three lines:

SELECT order_id, product_id, quantity, discount_pct
FROM order_items
WHERE order_id = 455
ORDER BY product_id;
order_id product_id quantity discount_pct
455 123 4 0
455 335 1 0
455 1795 1 25

Remove one of them:

DELETE FROM order_items WHERE order_id = 455 AND product_id = 1795;
1 row affected.

LIMIT caps how many of the matching rows are deleted. There are 2,758 single-unit lines without a discount; the statement below deletes only five of them:

DELETE FROM order_items
WHERE discount_pct = 0 AND quantity = 1
LIMIT 5;
5 rows affected.

Without ORDER BY, which matching rows are removed is not defined, so use LIMIT for batch clean-up jobs rather than to pick specific rows.

A row that other rows still reference through a foreign key cannot be deleted. Order 455 still has lines in order_items, which references orders:

DELETE FROM orders WHERE id = 455;
ERROR: Update/Delete operations are restricted by the foreign key 'fk_order_items_order_id'.

Delete the referencing rows first, then the parent row, as shown in the next section. The behavior depends on the ON DELETE action of the foreign key; see FOREIGN KEY constraint.

The multi-table forms read rows through a join and delete from the tables you list. Nine orders have not shipped yet, and together they have 29 lines. Remove the lines first:

DELETE oi
FROM order_items oi
INNER JOIN orders o ON oi.order_id = o.id
WHERE o.shipped_date IS NULL;
29 rows affected.

Then the orders themselves:

DELETE FROM orders WHERE shipped_date IS NULL;
9 rows affected.

The USING form expresses the same join in a different order. This statement deletes the same 29 lines as the first one:

DELETE FROM oi
USING order_items oi, orders o
WHERE oi.order_id = o.id AND o.shipped_date IS NULL;
29 rows affected.

Listing both tables in one statement does not get around the foreign key. CoraDB checks the constraint while it deletes, so deleting orders and their lines together fails:

DELETE oi, o
FROM orders o
INNER JOIN order_items oi ON oi.order_id = o.id
WHERE o.shipped_date IS NULL;
ERROR: Update/Delete operations are restricted by the foreign key 'fk_order_items_order_id'.

DELETE VERTEX FROM and DELETE EDGE FROM remove rows of vertex and edge tables; a plain DELETE FROM works too. Deleting a vertex also deletes the edges connected to it, so no edge is left pointing to a missing vertex.

CREATE VERTEX TABLE warehouse (id INT PRIMARY KEY, name VARCHAR(64));
CREATE EDGE TABLE stocks (quantity INT);
INSERT VERTEX INTO warehouse VALUES (1, 'Bucharest DC'), (2, 'Cluj DC');
INSERT EDGE FROM (SELECT warehouse FROM warehouse WHERE id = 1)
TO (SELECT product FROM product WHERE id = 1795) INTO stocks VALUES (40);
INSERT EDGE FROM (SELECT warehouse FROM warehouse WHERE id = 2)
TO (SELECT product FROM product WHERE id = 1795) INTO stocks VALUES (25);
DELETE EDGE FROM stocks WHERE quantity = 25; -- 1 edge left
DELETE VERTEX FROM warehouse WHERE id = 1; -- also deletes its edge: 0 edges left