The DELETE statement is used to delete existing records in a table.
Warning: Be careful when deleting records in a table! Notice the
WHEREclause in theDELETEstatement. TheWHEREclause specifies which record(s) should be deleted. If you omit theWHEREclause, all records in the table will be deleted!
DELETE FROM table_name
WHERE condition;
Below is a selection from the Customers table:
Customers Table:
| CustomerID | CustomerName | Country |
|---|---|---|
| 1 | Alfreds Futterkiste | Germany |
| 2 | Ana Trujillo Emparedados | Mexico |
| 3 | Antonio Moreno Taquería | Mexico |
The following SQL statement deletes the customer "Alfreds Futterkiste" from the Customers table:
DELETE FROM Customers WHERE CustomerName = 'Alfreds Futterkiste';
It is possible to delete all rows in a table without deleting the table itself. This means that the table structure, attributes, and indexes will be intact.
The following SQL statement deletes all rows in the Customers table, without deleting the table:
-- Be very careful with this command! DELETE FROM Customers;
A faster alternative for deleting all data is the TRUNCATE TABLE command, though it works slightly differently and cannot be rolled back in some database systems.