DELETE statement
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.
Syntax
Section titled “Syntax”-- one tableDELETE [FROM] table_name [WHERE <search_condition>] [LIMIT row_count]
-- several tables, FROM formDELETE table_name [, table_name ...] FROM <table_specifications> [WHERE <search_condition>]
-- several tables, USING formDELETE FROM table_name [, table_name ...] USING <table_specifications> [WHERE <search_condition>]- table_name: the table to delete rows from. With a single table, the
FROMkeyword 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
FROMclause 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.
LIMITis allowed only when one table is involved.
Deleting from one table
Section titled “Deleting from one table”Order 455 has three lines:
SELECT order_id, product_id, quantity, discount_pctFROM order_itemsWHERE order_id = 455ORDER 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.Limiting the number of deleted rows
Section titled “Limiting the number of deleted rows”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_itemsWHERE discount_pct = 0 AND quantity = 1LIMIT 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.
Foreign keys
Section titled “Foreign keys”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.
Deleting from several tables
Section titled “Deleting from several tables”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 oiFROM order_items oi INNER JOIN orders o ON oi.order_id = o.idWHERE 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 oiUSING order_items oi, orders oWHERE 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, oFROM orders o INNER JOIN order_items oi ON oi.order_id = o.idWHERE o.shipped_date IS NULL;ERROR: Update/Delete operations are restricted by the foreign key 'fk_order_items_order_id'.Deleting vertices and edges
Section titled “Deleting vertices and edges”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 leftDELETE VERTEX FROM warehouse WHERE id = 1; -- also deletes its edge: 0 edges leftSee also
Section titled “See also”- TRUNCATE: empty a table
- UPDATE
- WITH (CTE): use a common table expression in a DELETE
- Transactions