SQL reference (MySQL and MariaDB dialects)

DELETE statement

Single-table and multi-table DELETE with ORDER BY, LIMIT and RETURNING, as the Raft DB parser accepts it.

DELETE removes rows. A statement is all or nothing.

Syntax#

SQL
DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM [db_name.]tbl_name
    [WHERE condition]
    [ORDER BY order_expr [ASC | DESC] [, ...]]
    [LIMIT row_count]
    [RETURNING select_item [, select_item] ...]

DELETE [LOW_PRIORITY] [QUICK] [IGNORE] tbl_name [, tbl_name] ...
    FROM table_references
    [WHERE condition]

DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name [, tbl_name] ...
    USING table_references
    [WHERE condition]

Arguments#

NameMeaningDefault
LOW_PRIORITY, QUICK, IGNOREAccepted.off
WHERERows to remove. Without it every row goes (TRUNCATE TABLE is faster and resets the counter).all rows
ORDER BY, LIMITSingle table form only; LIMIT takes one number.none
RETURNINGThe removed rows as a result set (single-table form). See RETURNING.none

Remarks#

  • In the multi-table forms the rows are found by the SELECT executor; every table named before FROM (or after FROM in the USING form) loses its matching rows.
  • A parent row that other rows refer to: RESTRICT / NO ACTION refuse (error 1451); CASCADE deletes the referring rows (depth 15); SET NULL clears them.
  • BEFORE DELETE and AFTER DELETE triggers run for the single-table form. A multi-table DELETE does not run triggers yet.
  • A subquery on the table being deleted from is error 1093.
  • Privileges: DELETE on the table; SELECT on the columns WHERE reads.
  • Deleting from a view of one table is done on its table.
  • MariaDB's DELETE ... RETURNING is not restricted to the MariaDB dialect by the parser; it is checked against MariaDB.

Examples#

Example:

SQL
DELETE FROM e WHERE id IN (SELECT id FROM x);
DELETE a, b FROM a JOIN b ON a.id = b.id WHERE a.id = 1;
DELETE FROM a USING a JOIN b ON a.id = b.id;
DELETE FROM t WHERE id IN (2, 3) RETURNING id, name;
DELETE FROM t ORDER BY id LIMIT 2 RETURNING id;

See also#