SQL Reference

DELETE

The DELETE statement deletes rows from a table or view. Deleting a row from a view deletes the row from the table on which the view is based.

There are two forms of this statement:

Invocation

A DELETE statement can be embedded in an application program or issued through the use of dynamic SQL statements. It is an executable statement that can be dynamically prepared.

Authorization

To execute either form of this statement, the privileges held by the authorization ID of the statement must include at least one of the following:

To execute a Searched DELETE statement, the privileges held by the authorization ID of the statement must also include at least one of the following for each table or view referenced by a subquery:

When the package is precompiled with SQL92 rules 90 and the searched form of a DELETE includes a reference to a column of the table or view in the search-condition, the privileges held by the authorization ID of the statement must also include at least one of the following:

When the specified table or view is preceded by the ONLY keyword, the privileges held by the authorization ID of the statement must also include the SELECT privilege for every subtable or subview of the specified table or view.

Group privileges are not checked for static DELETE statements.

Syntax

Searched DELETE:

>>-DELETE FROM----+-table-name-------------------+-------------->
                  +-view-name--------------------+
                  '-ONLY--(--+-table-name-+---)--'
                             '-view-name--'
 
>-----+---------------------------+----------------------------->
      | .-AS-.                    |
      '-+----+--correlation-name--'
 
>-----+--------------------------+-----------------------------><
      '-WHERE--search-condition--'
 

Positioned DELETE:

>>-DELETE FROM----+-table-name-------------------+-------------->
                  +-view-name--------------------+
                  '-ONLY--(--+-table-name-+---)--'
                             '-view-name--'
 
>----WHERE CURRENT OF--cursor-name-----------------------------><
 

Description

FROM table-name  or  view-name
Identifies the table or view from which rows are to be deleted. The name must identify a table or view that exists in the catalog, but it must not identify a catalog table, a catalog view, a summary table or a read-only view. (For an explanation of read-only views, see CREATE VIEW.)

If table-name is a typed table, rows of the table or any of its proper subtables may get deleted by the statement.

If view-name is a typed view, rows of the underlying table or underlying tables of the view's proper subviews may get deleted by the statement. If view-name is a regular view with an underlying table that is a typed table, rows of the typed table or any of its proper subtables may get deleted by the statement.

Only the columns of the specified table may be referenced in the WHERE clause. For a positioned DELETE, the associated cursor must also have specified the table or view in the FROM clause without using ONLY.

FROM ONLY (table-name)
Applicable to typed tables, the ONLY keyword specifies that the statement should apply only to data of the specified table and rows of proper subtables cannot be deleted by the statement. For a positioned DELETE, the associated cursor must also have specified the table in the FROM clause using ONLY. If table-name is not a typed table, the ONLY keyword has no effect on the statement.

FROM ONLY (view-name)
Applicable to typed views, the ONLY keyword specifies that the statement should apply only to data of the specified view and rows of proper subviews cannot be deleted by the statement. For a positioned DELETE, the associated cursor must also have specified the view in the FROM clause using ONLY. If view-name is not a typed view, the ONLY keyword has no effect on the statement.

correlation-name
May be used within the search-condition to designate the table or view. (For an explanation of correlation-name, see Language Elements.)

WHERE
Specifies a condition that selects the rows to be deleted. The clause can be omitted, a search condition specified, or a cursor named. If the clause is omitted, all rows of the table or view are deleted.

search-condition
Is any search condition as described in Search Conditions. Each column-name in the search condition, other than in a subquery must identify a column of the table or view.

The search-condition is applied to each row of the table or view and the deleted rows are those for which the result of the search-condition is true.

If the search condition contains a subquery, the subquery can be thought of as being executed each time the search condition is applied to a row, and the results used in applying the search condition. In actuality, a subquery with no correlated references is executed once, whereas a subquery with a correlated reference may have to be executed once for each row. If a subquery refers to the object table of a DELETE statement or a dependent table with a delete rule of CASCADE or SET NULL, the subquery is completely evaluated before any rows are deleted.

CURRENT OF cursor-name
Identifies a cursor that is defined in a DECLARE CURSOR statement of the program. The DECLARE CURSOR statement must precede the DELETE statement.

The table or view named must also be named in the FROM clause of the SELECT statement of the cursor, and the result table of the cursor must not be read-only. (For an explanation of read-only result tables, see DECLARE CURSOR.)

When the DELETE statement is executed, the cursor must be positioned on a row: that row is the one deleted. After the deletion, the cursor is positioned before the next row of its result table. If there is no next row, the cursor is positioned after the last row.

Rules

Notes

Examples

Example 1:  Delete department (DEPTNO) 'D11' from the DEPARTMENT table.

  DELETE FROM DEPARTMENT
  WHERE DEPTNO = 'D11'

Example 2:  Delete all the departments from the DEPARTMENT table (that is, empty the table).

  DELETE FROM DEPARTMENT


Footnotes:

90
The package used to process the statement is precompiled using option LANGLEVEL with value SQL92E or MIA.


[ Top of Page | Previous Page | Next Page ]