Updating Data with UPDATE: Modifying Existing Rows
A DELETE statement removes rows, while its WHERE clause identifies which rows are eligible for removal.
Try it: INSERT, UPDATE and DELETE
How INSERT builds a new row from its column list (omitted columns get the generated id, their DEFAULT or NULL), how UPDATE … SET and DELETE test WHERE on every row and change only the matches — or every row when WHERE is missing — and how cur.rowcount and commit() report and keep the change.
How it works
- The statement is sent with ? placeholders and a tuple of values: cur.execute(sql, params).
- INSERT: the listed columns get the values in order; an unlisted INTEGER PRIMARY KEY gets max(id) + 1, an unlisted column gets its DEFAULT (or NULL). A listed id that already exists raises IntegrityError.
- UPDATE / DELETE: WHERE is evaluated on each row; only rows where it is TRUE match (NULL does not). No WHERE means every row matches. The same WHERE in a SELECT shows the target rows first.
- UPDATE applies every SET assignment to each matched row, reading the row's old values; DELETE removes the matched rows. Matching nothing is not an error.
- cur.rowcount is the number of rows inserted, changed or removed.
- The change is pending until conn.commit(); conn.close() without commit() rolls it back, so a new connection still sees the old table.
Default run (11 steps): UPDATE track SET plays = ? WHERE title = ? with parameters (16, 'My Way'). The track table starts with 5 rows. … conn.commit() makes the change permanent. A new connection now sees 5 rows.
Simplified: A toy relational engine in the browser, not SQLite itself: statements are built from the controls (no SQL text is parsed or executed) and tables have a few rows. Results were checked against Python's sqlite3 (SQLite 3.49). One table track(id INTEGER PRIMARY KEY, title TEXT, plays INTEGER DEFAULT 0) of up to 8 rows, one WHERE comparison, text limited to printable ASCII. The generated id is max(id) + 1, SQLite's rule for an INTEGER PRIMARY KEY without AUTOINCREMENT.
Loading the simulation…
Deletion Requires Precision
The source topic names UPDATE, but the operation covered here is DELETE. UPDATE changes a value in an existing row. DELETE removes rows from a table entirely. Because deletion is irreversible in most practical situations, the condition that identifies the rows to remove must be checked carefully.
A DELETE statement removes rows, while its WHERE clause identifies which rows are eligible for removal.
How WHERE Selects Rows
The WHERE clause acts as a gatekeeper. The database evaluates its condition for each row independently. Rows that satisfy the condition are selected for deletion. Rows that do not satisfy it remain in the table.
In the Track example, the condition is title = 'My Way'. The two rows titled My Way match the condition and are removed. Bohemian Rhapsody and Imagine do not match, so they remain.
A Targeted DELETE Operation
Removing matching tracks
Remove every row from the Track table whose title equals 'My Way'.
- 1Identify the table
The operation applies to the Track table.
- 2Specify the filter
The WHERE condition title = 'My Way' tells the database which rows qualify.
- 3Evaluate each row
Each row is checked independently. Matching rows are selected for deletion; nonmatching rows are left untouched.
- 4Finalize the change
After executing DELETE, call
commit()to finalize the pending deletion.
The rows titled My Way are deleted. Bohemian Rhapsody and Imagine remain in the Track table after the change is committed.
Before executing DELETE, test the same WHERE condition with SELECT. This verifies that the condition identifies exactly the rows you intend to remove.
The Empty-Condition Trap
A DELETE statement without a WHERE clause has no condition to evaluate. Every row matches the implicit condition of all rows, so the entire table is deleted and becomes empty.
Common mistake Executing DELETE without a WHERE clause
What it looks likeDELETE FROM Track
Why it failsWith no condition to evaluate, every row matches and the entire table is deleted.
Do this insteadInclude a specific WHERE condition and test that condition with SELECT before executing DELETE.
Common mistake Skipping the verification step
What it looks likeRunning DELETE immediately after writing a condition
Why it failsA condition can target more rows than intended if it has not been checked.
Do this insteadUse SELECT with the same WHERE condition first, then execute DELETE only after confirming the matching rows.
Common mistake Assuming deletion is equivalent to updating a value
What it looks likeTreating removal of a row as an ordinary data edit
Why it failsUpdating modifies an existing value, while deletion removes the row and is irreversible in most practical situations.
Do this insteadReview the target condition carefully and recognize that recovery typically requires a backup.
Committing the Transaction
In many database systems, DELETE does not immediately remove data permanently. Instead, the rows are marked for deletion within a transaction. Calling commit() tells the database to finalize pending changes and write them to disk. This makes the deletion permanent and visible to other users or programs accessing the database.
The practical sequence is: execute DELETE with a carefully checked WHERE clause, then immediately call commit(). Some libraries or frameworks commit automatically, but in most educational and professional contexts you are responsible for calling commit() explicitly.
Safe Deletion Checklist
- 1Identify the table containing the rows to remove.
- 2Write a specific WHERE condition that describes the intended rows.
- 3Test that condition with SELECT and inspect the matching rows.
- 4Execute DELETE using the verified condition.
- 5Call
commit()to finalize the deletion.
Check Your Reasoning
A Track table contains Bohemian Rhapsody, two rows titled My Way, and Imagine. You want to remove only the rows titled My Way. Describe what you should verify before DELETE and what operation finalizes the change.
Hints
- The verification should use SELECT with the same condition intended for DELETE.
- The condition should match both My Way rows and not the other tracks.
- The finalization operation is
commit().
What do you think happens?
What happens if DELETE is executed on the Track table without a WHERE clause?
Reveal answer
Every row is deleted.
Without a WHERE clause, the database has no condition to evaluate, so every row matches and the table becomes empty.
Key Takeaways
- 1DELETE removes rows from a table; UPDATE changes values in existing rows.
- 2A WHERE clause filters the rows that DELETE can remove.
- 3Omitting WHERE causes every row in the table to be deleted.
- 4Test the WHERE condition with SELECT before executing DELETE.
- 5Call
commit()to finalize the deletion and make the change permanent.
At a glance
- DELETE removes complete rows, unlike UPDATE, which modifies existing values.
- WHERE is the safety filter that identifies which rows should be deleted.
- A DELETE without WHERE removes every row in the table.
- Use SELECT to verify the target rows before deletion.
- Use
commit()to finalize the deletion.