Jump to content

Delete (SQL)

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by 188.236.111.122 (talk) at 05:00, 16 December 2020 (Example with related tables). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In the database structured query language (SQL), the DELETE statement removes one or more records from a table. A subset may be defined for deletion using a condition, otherwise all records are removed.[1] 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).

Examples

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
      );

bcddjjjhesp

Deleting all rows from a table can be very time consuming. Some DBMS[clarification needed] offer a TRUNCATE TABLE command that works a lot quicker, as it only alters metadata and typically does not spend time enforcing constraints or firing triggers.

DELETE only deletes the rows. For deleting a table entirely the DROP command can be used.

References

  1. ^ "SQL Delete Statement". w3schools.com.