The DELETE statement serves to remove pre-existing records from a table.
DELETE FROM table_name WHERE condition; |
Important: Exercise caution when deleting records from a table! Pay attention to the WHERE clause in the DELETE statement. The WHERE clause determines which record(s) to delete. If you omit the WHERE clause, all records in the table will be deleted! |
The following excerpt is taken from the Customers table utilized in the illustrations:
CustomerID |
CustomerName |
ContactName |
Address |
City |
PostalCode |
Country |
1 |
Alfreds Futterkiste |
Maria Anders |
Obere Str. 57 |
Berlin |
12209 |
Germany |
2 |
Ana Trujillo Emparedados y helados |
Ana Trujillo |
Avda. de la Constitución 2222 |
México D.F. |
05021 |
Mexico |
3 |
Antonio Moreno Taquería |
Antonio Moreno |
Mataderos 2312 |
México D.F. |
05023 |
Mexico |
4 |
Around the Horn |
Thomas Hardy |
120 Hanover Sq. |
London |
WA1 1DP |
UK |
5 |
Berglunds snabbköp |
Christina Berglund |
Berguvsvägen 8 |
Luleå |
S-958 22 |
Sweden |
This SQL statement removes the customer “Alfreds Futterkiste” from the “Customers” table:
DELETE FROM Customers WHERE CustomerName=‘Alfreds Futterkiste’; |
Now, the “Customers” table will appear as follows:
CustomerID |
CustomerName |
ContactName |
Address |
City |
PostalCode |
Country |
2 |
Ana Trujillo Emparedados y helados |
Ana Trujillo |
Avda. de la Constitución 2222 |
México D.F. |
05021 |
Mexico |
3 |
Antonio Moreno Taquería |
Antonio Moreno |
Mataderos 2312 |
México D.F. |
05023 |
Mexico |
4 |
Around the Horn |
Thomas Hardy |
120 Hanover Sq. |
London |
WA1 1DP |
UK |
5 |
Berglunds snabbköp |
Christina Berglund |
Berguvsvägen 8 |
Luleå |
S-958 22 |
Sweden |
You can delete all rows in a table while preserving its structure, attributes, and indexes, thereby ensuring that the table remains intact.
DELETE FROM table_name; |
This SQL statement clears all rows from the “Customers” table, while leaving the table itself intact:
DELETE FROM Customers; |
To entirely remove the table, utilize the DROP TABLE statement:
Delete the Customers table.
DROP TABLE Customers; |