How to use the SQL Drop, Truncate and Delete Statements
The SQL DELETE
statement is used to delete records from a table whereas the DROP
statement is used to delete a table or a database.
The TRUNCATE TABLE
statement can also be used to delete records from a table.
Deleting Records From a Table
The DELETE
statement in SQL is used to delete records from a database table. We can remove all records from a table or delete specific records using the WHERE clause.
DELETE FROM table_name
WHERE condition;
SQL DELETE Statement Examples
Let’s assume we have a table called “Employees” with the following records:
+------------+-----------+----------+------------+
| EmployeeID | FirstName | LastName | Department |
+------------+-----------+----------+------------+
| 1 | Mark | Otto | Finance |
| 2 | Jacob | Thornton | IT |
| 3 | Su | Bird | Marketing |
| 4 | Sam | Burger | IT |
+------------+-----------+----------+------------+
Delete a Single Record
The following code removes “Jacob Thornton” from the “Employees” table:
DELETE FROM Employees
WHERE FirstName = 'Jacob'
AND LastName = 'Thornton';
Delete All Records From a Table
The following code deletes all records from the “Employees” table:
DELETE * FROM Employees;
SQL TRUNCATE TABLE Statement
The TRUNCATE TABLE
statement can also be used to delete records from a table, but not the table instead.
Example:
TRUNCATE TABLE table_name;
SQL Drop Statement
The DROP
statement in SQL is used to delete a table or a database.
How to Drop a Table in SQL
The following code drops a table called “Employees”:
DROP TABLE "Employees";
How to Drop a Database in SQL
The following code drops a database called “EmployeesDB”:
DROP DATABASE "EmployeesDB";