sql_delete
SQL DELETE Statement
Section titled “SQL DELETE Statement”The DELETE statement in SQL is used to remove existing records (rows) from a table. It’s a powerful command, so it must be used with caution.
The SQL DELETE Statement Explained
Section titled “The SQL DELETE Statement Explained”The DELETE statement removes one or more rows from a table based on a specified condition. If no condition is provided, all rows in the table will be deleted.
SQL DELETE Syntax
Section titled “SQL DELETE Syntax”DELETE FROM table_nameWHERE condition;table_name: The name of the table from which to delete rows.WHERE condition: An optional clause that specifies which rows to delete. If omitted, all rows are deleted.
Demo Database
Section titled “Demo Database”Let’s use a conceptual “Customers” table for our examples.
Initial “Customers” table data:
CustomerID | CustomerName | ContactName | City | Country---------- | ----------------------------------- | --------------- | ----------- | -------1 | Alfreds Futterkiste | Maria Anders | Berlin | Germany2 | Ana Trujillo Emparedados y helados | Ana Trujillo | México D.F. | Mexico3 | Antonio Moreno Taquería | Antonio Moreno | México D.F. | Mexico4 | Around the Horn | Thomas Hardy | London | UK5 | Berglunds snabbköp | Christina Berglund | Luleå | SwedenSQL DELETE Example
Section titled “SQL DELETE Example”Suppose we want to delete the customer “Alfreds Futterkiste” from the “Customers” table. It’s best practice to use a unique identifier like CustomerID if available.
Using CustomerID (recommended):
DELETE FROM CustomersWHERE CustomerID = 1;Using CustomerName (if CustomerID is unknown, but be cautious of duplicates):
DELETE FROM CustomersWHERE CustomerName = 'Alfreds Futterkiste';After executing one of these statements, the “Customers” table will look like this (assuming CustomerID 1 was ‘Alfreds Futterkiste’):
CustomerID | CustomerName | ContactName | City | Country---------- | ----------------------------------- | --------------- | ----------- | -------2 | Ana Trujillo Emparedados y helados | Ana Trujillo | México D.F. | Mexico3 | Antonio Moreno Taquería | Antonio Moreno | México D.F. | Mexico4 | Around the Horn | Thomas Hardy | London | UK5 | Berglunds snabbköp | Christina Berglund | Luleå | SwedenDeleting All Rows from a Table
Section titled “Deleting All Rows from a Table”To delete all rows in a table without deleting the table itself (i.e., the table structure, columns, indexes, and constraints remain), you can use DELETE without a WHERE clause:
DELETE FROM table_name;Extreme caution is advised with this command.
Alternative: TRUNCATE TABLE
Section titled “Alternative: TRUNCATE TABLE”Many database systems offer the TRUNCATE TABLE command as a faster way to delete all rows from a table.
TRUNCATE TABLE table_name;Key differences between DELETE FROM table_name; and TRUNCATE TABLE table_name;:
- Speed:
TRUNCATEis generally much faster as it deallocates data pages directly and logs less information. - Triggers:
DELETEfires row-levelDELETEtriggers.TRUNCATEusually does not (behavior can vary by RDBMS). - Transaction Log:
DELETElogs each row deletion, which can fill up the transaction log.TRUNCATElogs the deallocation of pages, using fewer resources. - Auto-increment:
TRUNCATEtypically resets auto-incrementing identity columns back to their seed value.DELETEdoes not. - Rollback:
DELETEis a DML command and can be rolled back within a transaction.TRUNCATEis often considered a DDL command (or DML with DDL-like characteristics) and its rollback behavior can vary; some RDBMS don’t allow rollback or handle it differently. - WHERE clause:
TRUNCATEcannot have aWHEREclause; it always removes all rows.
Choose TRUNCATE when you need to quickly empty a table and the side effects (like not firing triggers or resetting identity) are acceptable or desired. For conditional deletion or when row-level actions are needed, use DELETE.
Best Practices:
Section titled “Best Practices:”- Always use a
WHEREclause unless you intend to delete all rows. - Prefer using primary keys or unique identifiers in your
WHEREclause for precise deletion. - Test your
WHEREcondition with aSELECTstatement first to ensure you are targeting the correct rows. - Ensure you have recent database backups before performing large-scale delete operations.
- Consider using transactions if you need to perform multiple related
DELETEoperations as an atomic unit.