The DELETE statement (delete_statement) deletes rows in a table.
<delete_statement> ::=
DELETE [FROM] <table_name>
[<reference_name>]
[KEY <key_spec>,...]
[WHERE <search_condition>]
| DELETE [FROM] <table_name> [<reference_name>] WHERE CURRENT OF
<result_table_name>
The table name must identify an existing base table, view-table or a synonym.
The current user must have the DELETE privilege for the specified table. If the table name identifies a view table, even the owner of the view table may not have the DELETE privilege because the view table cannot be changed.
Table name identifies a view table: the rows of the tables on which the view tables are based are deleted.
Table name identifies a join view table: only the following rows are deleted:
· Rows in the key table of the join view table
· Rows in base tables on which the view table is based and that have a 1:1: relationship with the key table.
The following specifications determine the rows in the table that are deleted:
·
Optional sequence
of key specifications and optional search condition
Key specification and no search condition: a row with the specified key
values already exists. This row is deleted. No rows are deleted if a row with
the specified key values does not exist.
Key specification and a search condition: a row containing the
specified key values exists. The search condition is applied to this row. If
the search condition is
satisfied, then the row is deleted. No rows are deleted if a row with the
specified key values does not exist or if a search condition applied to a row
is not fulfilled.
No key specification and a search condition: the search condition is
applied to each row in the specified table. All rows for which the search condition is satisfied are
deleted.
· If CURRENT OF is used, that is, if the cursor position in the result table (result_table_name) is specified: no rows are deleted if the cursor is not positioned on a row in the result table.
· If none of the above specifications were made, all of the rows in the specified table are deleted.
· If no row is found that satisfies the conditions defined by the optional clauses, the following message appears: 100 row not found.
If CURRENT OF is specified, the table name in the FROM clause of the QUERY statement, with which the result table (result_table_name)was built, must be identical to the table name in the DELETE statement.
If CURRENT OF is specified and the cursor is positioned on a row in the result table, the corresponding row is deleted. The corresponding row is the row of the table specified in the FROM clause of the query statement, from which the result table row was formed. This procedure requires that the result table was specified with FOR UPDATE. Afterwards, the cursor is positioned behind the result table row. It is impossible to predict whether or not the updated values in the corresponding row are visible the next time the same row of the result table is accessed.
For each row deleted in the course of the delete statement which originates from a referenced table of at least one referential CONSTRAINT definition, one of the following actions is carried out - depending on the DELETE rule of the referential constraint definition:
· DELETE CASCADE: all matching rows in the corresponding foreign key table are deleted.
· DELETE RESTRICT: if there are matching rows in the corresponding foreign key table, the delete statement fails.
· DELETE SET NULL: the NULL value is assigned to the respective foreign key columns of all matching rows in the corresponding foreign key table.
· DELETE SET DEFAULT: the DEFAULT value that was set with a DEFAULT specification is assigned to the respective foreign key columns of all matching rows in the corresponding foreign key table.
If triggers that are to be executed after a DELETE statement were defined for base tables from which rows are to be deleted with the DELETE statement, they are executed accordingly. The DELETE statement will fail if one of these triggers fails.