Reference articles on history, science, culture and more
Encyclopedia

Delete (SQL)

SQL statement

In the database structured query language (SQL), the DELETE statement is used to remove one or more rows from a table. A subset may be defined for deletion using a condition, otherwise all records are removed. Some database management systems (DBMSs), like MySQL, allow deletion of rows from multiple tables with one DELETE statement (this is sometimes called multi-table DELETE).

It is not possible to delete individual values using DELETE, instead UPDATE should be used to set the value to NULL. Backups should always be created before using DELETE.

01Examples

Delete rows from table pies where column flavor equals Lemon Meringue:

DELETE FROM pies WHERE flavor='Lemon Meringue';

Delete rows in trees, if the value of height is smaller than 80.

DELETE FROM trees WHERE height < 80;

Delete all rows from mytable:

DELETE FROM mytable;

Delete rows from mytable using a subquery in the where condition:

DELETE FROM mytable WHERE id IN ( SELECT id FROM mytable2 );

Delete rows from mytable using a list of values:

DELETE FROM mytable WHERE id IN ( value1, value2, value3, value4, value5 );

03Features

  • It is a DML (Data Manipulation Language) command, therefore the following commands are used for this command: COMMIT and ROLLBACK
  • Deletes all or some records from the table, you can limit the records to be deleted by using the WHERE clause
  • Does not free the space occupied by the data in the table (in the TABLESPACE)
  • Does not reset the SEQUENCE value assigned to the table
  • DELETE works much slower than TRUNCATE
  • You can undo the operation of removing records by using the ROLLBACK command
  • DELETE requires a shared table lock
  • Triggers fire
  • DELETE can be used in the case of: database link
  • DELETE returns the number of records deleted
  • Transaction log - DELETE needs to read records, check constraints, update block, update indexes, and generate redo / undo. All of this takes time, hence it takes time much longer than with TRUNCATE
  • reduces performance during execution - each record in the table is locked for deletion
  • DELETE uses more transaction space than the TRUNCATE statement
  • DELETE can be used with indexed views
  • DELETE generates a small amount of redo and a large amount of undo
  • DELETE operation does not make unusable indexes usable again
Watch videos about Delete (SQL)Explainers and documentaries on YouTube (opens in a new tab)

Sources and credits

This article is adapted from the Wikipedia article Delete (SQL), written by its contributors and licensed under CC BY-SA 4.0. Fathomly has changed the layout, removed citation markers, navigation and maintenance notices, and adjusted punctuation. This adapted version is shared under the same license. For references, see the original article.

Fathomly is not affiliated with or endorsed by the Wikimedia Foundation. Spotted a problem? Tell us.