6.5. DELETE

Used for

Deleting rows from a table or view

Available in

DSQL, ESQL, PSQL

Syntax

  1. DELETE
  2. FROM target [[AS] alias]
  3. [WHERE {<search-conditions> | CURRENT OF cursorname}]
  4. [PLAN <plan_items>]
  5. [ORDER BY <sort_items>]
  6. [ROWS m [TO n]]
  7. [RETURNING <returning_list> [INTO <variables>]]
  8. <returning_list> ::= <ret_value> [, <ret_value> ...]
  9. <ret_value> ::= colname | <value>
  10. <variables> ::= [:]varname [, [:]varname ...]
Table 75. Arguments for the DELETE Statement Parameters
ArgumentDescription

target

The name of the table or view from which the records are to be deleted

alias

Alias for the target table or view

search-conditions

Search condition limiting the set of rows being targeted for deletion

cursorname

The name of the cursor in which current record is positioned for deletion

plan_items

Query plan clause

sort_items

ORDER BY clause

m, n

Integer expressions for limiting the number of rows being deleted

ret_value

An expression to be returned in the RETURNING clause

value

An expression whose value is used for returning

varname

Name of a PSQL variable

Description

DELETE removes rows from a database table or from one or more of the tables that underlie a view. WHERE and ROWS clauses can limit the number of rows deleted. If neither WHERE nor ROWS is present, DELETE removes all the rows in the relation.

6.5.1. Aliases

If an alias is specified for the target table or view, it must be used to qualify all field name references in the DELETE statement.

Examples

Supported usage:

  1. delete from Cities where name starting 'Alex';
  2. delete from Cities where Cities.name starting 'Alex';
  3. delete from Cities C where name starting 'Alex';
  4. delete from Cities C where C.name starting 'Alex';

Not possible:

  1. delete from Cities C where Cities.name starting 'Alex';

6.5.2. WHERE

The WHERE clause sets the conditions that limit the set of records for a searched delete.

In PSQL, if a named cursor is being used for deleting a set, using the WHERE CURRENT OF clause, the action is limited to the row where the cursor is currently positioned. This is a positioned delete.

The WHERE CURRENT OF clause is available only in PSQL and ESQL, since there is no statement for creating and manipulating an explicit cursor in DSQL. Searched deletes are also available in PSQL, of course.

Examples

  1. DELETE FROM People
  2. WHERE firstname <> 'Boris' AND lastname <> 'Johnson';
  3. DELETE FROM employee e
  4. WHERE NOT EXISTS(
  5. SELECT *
  6. FROM employee_project ep
  7. WHERE e.emp_no = ep.emp_no);
  8. DELETE FROM Cities
  9. WHERE CURRENT OF Cur_Cities; -- ESQL and PSQL only

6.5.3. PLAN

A PLAN clause allows the user to optimize the operation manually.

Example

  1. DELETE FROM Submissions
  2. WHERE date_entered < '1-Jan-2002'
  3. PLAN (Submissions INDEX ix_subm_date);

6.5.4. ORDER BY and ROWS

The ORDER BY clause orders the set before the actual deletion takes place. It only makes sense in combination with ROWS, but is also valid without it.

The ROWS clause limits the number of rows being deleted. Integer literals or any integer expressions can be used for the arguments m and n.

If ROWS has one argument, m, the rows to be deleted will be limited to the first m rows.

Points to note

  • If m > the number of rows being processed, the entire set of rows is deleted

  • If m = 0, no rows are deleted

  • If m < 0, an error occurs and the deletion fails

If two arguments are used, m and n, ROWS limits the rows being deleted to rows from m to n inclusively. Both arguments are integers and start from 1.

Points to note

  • If m > the number of rows being processed, no rows are deleted

  • If m > 0 and <= the number of rows in the set and n is outside these values, rows from m to the end of the set are deleted

  • If m < 1 or n < 1, an error occurs and the deletion fails

  • If n = m - 1, no rows are deleted

  • If n < m -1, an error occurs and the deletion fails

Examples

Deleting the oldest purchase:

  1. DELETE FROM Purchases
  2. ORDER BY date ROWS 1;

Deleting the highest custno(s):

  1. DELETE FROM Sales
  2. ORDER BY custno DESC ROWS 1 to 10;

Deleting all sales, ORDER BY clause pointless:

  1. DELETE FROM Sales
  2. ORDER BY custno DESC;

Deleting one record starting from the end, i.e. from Z…​:

  1. DELETE FROM popgroups
  2. ORDER BY name DESC ROWS 1;

Deleting the five oldest groups:

  1. DELETE FROM popgroups
  2. ORDER BY formed ROWS 5;

No sorting (ORDER BY) is specified so 8 found records, starting from the fifth one, will be deleted:

  1. DELETE FROM popgroups
  2. ROWS 5 TO 12;

6.5.5. RETURNING

A DELETE statement removing at most one row may optionally include a RETURNING clause in order to return values from the deleted row. The clause, if present, need not contain all the relation’s columns and may also contain other columns or expressions.

Notes
  • In DSQL, a statement with RETURNING always returns a singleton, never a mult-row set. If no records are deleted, the returned columns contain NULL. This behaviour may change in future Firebird versions

  • The INTO clause is available only in PSQL

    • If the row is not deleted, nothing is returned and the target variables keep their values

Examples

  1. DELETE FROM Scholars
  2. WHERE firstname = 'Henry' and lastname = 'Higgins'
  3. RETURNING lastname, fullname, id;
  4. DELETE FROM Dumbbells
  5. ORDER BY iq DESC
  6. ROWS 1
  7. RETURNING lastname, iq into :lname, :iq;
The “Unstable Cursor” Problem

Because of the way the execution of data-changing DML is implemented in Firebird, up to and including this version, the sets targeted for deletion sometimes produce unexpected results. For more information, refer to The Unstable Cursor Problem in the UPDATE section.