Careeroza
DELETE
The SQL DELETE Statement
DELETE removes existing records.
DELETE FROM table_name WHERE condition;Note: If you omit WHERE, all records are deleted. The table itself (structure, columns, indexes) stays.
Delete Rows
Example
Delete products that are out of stock:
DELETE FROM Products
WHERE Stock = 0;Running SELECT ProductID, ProductName, Stock FROM Products; afterwards shows:
Result: 5 rows
| ProductID | ProductName | Stock |
|---|---|---|
| 1 | Basmati Rice 5kg | 120 |
| 2 | Green Tea 100 bags | 300 |
| 3 | Olive Oil 1L | 80 |
| 5 | Espresso Beans 1kg | 45 |
| 6 | Almonds 500g | NULL |
Delete All Records
DELETE FROM Customers;This removes every row but keeps the table. To remove the table completely, use DROP TABLE.
DELETE vs TRUNCATE vs DROP
DELETE | TRUNCATE | DROP | |
|---|---|---|---|
| Type | DML | DDL | DDL |
| Removes | chosen rows (WHERE) | all rows | the whole table |
| Table structure kept? | yes | yes | no |
| Can roll back? | yes | depends on database | depends on database |
| Fires triggers? | yes | no | no |
| Speed on big tables | slow (row by row, logged) | fast | fast |
| Resets identity/auto-increment? | no | yes (most databases) | n/a |