Querying Data with SELECT: Retrieving Information
DELETE removes rows from a table; the WHERE clause specifies which rows to delete.
Deletion Requires Deliberate Targeting
Deleting data is different from inserting or updating it. Inserting adds a row, and updating modifies an existing value. DELETE removes rows from a table, so the operation demands careful targeting. In most practical situations, recovering a deleted row requires restoring it from a backup.
The WHERE clause is the main safety mechanism in a DELETE statement. It acts as a filter that identifies which rows should be removed and which should remain.
Filtering Rows with WHERE
A DELETE statement removes rows from a table. Adding a WHERE clause gives the database a condition to evaluate for each row. Rows that satisfy the condition are selected for deletion; rows that do not satisfy it remain in the table.
Tracing a Matching Condition
Removing tracks titled My Way
Consider a Track table containing Bohemian Rhapsody, My Way, Imagine, and a second My Way entry. What remains after applying DELETE with the condition title = 'My Way'?
Check the first row: For Bohemian Rhapsody, title = 'My Way' is false, so the row remains.
Check the matching rows: For each My Way entry, title = 'My Way' is true, so both matching rows are selected for deletion.
Check the remaining nonmatching row: For Imagine, the condition is false, so that row remains.
After the statement executes, only Bohemian Rhapsody and Imagine remain in the table.
The condition is evaluated independently for each row. This means a single DELETE statement can remove more than one row when multiple rows satisfy the same WHERE condition.
The Empty WHERE Clause Trap
When DELETE FROM Track is executed without a WHERE clause, there is no condition to evaluate. Every row matches the implicit meaning of the statement, so every row is deleted and the table becomes empty.
Checking Before Deleting
Before executing DELETE, test the WHERE condition with SELECT. This lets you verify which rows the condition identifies before you remove them. The check is especially important because the WHERE clause determines the complete set of rows affected by the deletion.
- Use SELECT to test the condition before deletion.
- Verify that the rows identified are exactly the rows intended for removal.
- Execute DELETE with the specific WHERE condition.
- Call commit() to finalize the pending deletion.
Finalizing the Transaction
In many database systems, DELETE does not immediately make the removal permanent. Instead, the rows are marked for deletion within a transaction. Calling commit() tells the database to finalize pending changes and write them to disk, making the deletion permanent and visible to other users or programs accessing the database.
Common Deletion Mistakes
Omitting the WHERE clause
Without a condition, every row in Track is eligible for deletion, leaving the table empty.
Fix:
Include a specific WHERE condition that identifies the intended rows.Skipping the SELECT check
You may not notice that the condition identifies more rows than intended until after DELETE executes.
Fix:
Test the WHERE condition with SELECT and verify the target rows before deleting.Forgetting to call commit()
The deletion may remain a pending transaction rather than becoming a finalized database change.
Fix:
Call commit() after executing DELETE unless the database library or framework explicitly handles committing automatically.Assuming a matching title identifies only one row
Every row whose title equals My Way matches the condition, so multiple rows can be deleted.
Fix:
Use SELECT first to check how many rows satisfy the condition.
Practice the Safe Sequence
A Track table contains several songs. You intend to remove only the rows whose title is My Way. Describe the sequence you should follow before the deletion becomes a permanent database change.
Hints
- Begin by checking the target rows with SELECT.
- Use a WHERE condition that identifies the intended title.
- After DELETE, call commit() to finalize the change.
What do you think happens?
What happens if DELETE FROM Track is executed without a WHERE clause?
Reveal answer
Answer: Every row in Track is deleted.
Without a WHERE clause, the database has no condition to evaluate, so every row matches the statement.
Key Takeaways
- DELETE removes rows from a table.
- WHERE filters the rows so that only matching records are selected for deletion.
- Leaving out WHERE causes every row in the table to be deleted.
- Use SELECT before DELETE to verify the rows targeted by the condition.
- Call commit() after DELETE to finalize the deletion and save the database change.
Key Takeaways
- A DELETE statement removes rows, while its WHERE clause determines which rows match.
- The WHERE condition is evaluated for each row, so one DELETE can remove multiple matching rows.
- A DELETE statement without WHERE deletes every row in the table.
- Testing the condition with SELECT is a practical safeguard before deletion.
- Calling commit() finalizes the pending deletion and makes the change permanent.