Inserting Data with INSERT: Adding New Rows
The WHERE clause is the safety mechanism of DELETE: it tells the database which rows should be removed and which rows should remain.
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…
A Deletion Needs a Target
Deleting data is not the same as inserting or updating it. Inserting adds a new row, and updating modifies an existing value. DELETE removes rows, and recovering deleted data typically requires restoring it from a backup. That is why a deletion must identify its target carefully.
The WHERE clause is the safety mechanism of DELETE: it tells the database which rows should be removed and which rows should remain.
How WHERE Selects Rows
Imagine a Track table containing several songs. The WHERE clause examines each row independently. A row whose title matches the condition is selected for deletion; a row that does not match remains in the table.
For the condition title = 'My Way', the two rows titled 'My Way' match and are deleted. 'Bohemian Rhapsody' and 'Imagine' do not match, so they remain.
A Worked Track Deletion
Remove Tracks with a Specific Title
Remove every row from the Track table whose title is 'My Way'.
- 1Choose the table
The deletion operates on the Track table.
- 2Choose the condition
Use the condition title = 'My Way' so the database selects only rows whose title equals 'My Way'.
- 3Execute DELETE
The DELETE statement removes the matching rows. Rows with other titles are not selected.
- 4Check the result
Before executing DELETE, test the same WHERE condition with SELECT. This verifies which rows the condition will target.
- 5Finalize the change
Call
commit()after DELETE to finalize the deletion and ensure the change is permanently removed from the database.
The rows titled 'My Way' are removed. The rows titled 'Bohemian Rhapsody' and 'Imagine' remain.
Treat the sequence as two deliberate checks: first test the WHERE condition with SELECT, then execute DELETE with that same condition, and finally call commit().
The Risk of Missing WHERE
A DELETE statement without a WHERE clause has no condition to evaluate. Every row matches the implicit meaning of all rows, so the entire table becomes empty.
Common mistake Running DELETE without a WHERE clause.
What it looks likeDELETE FROM Track
Why it failsWithout a condition, every row in Track is deleted.
Do this insteadInclude a specific WHERE condition and test that condition with SELECT before executing DELETE.
Common mistake Skipping the verification step.
Why it failsA condition can target different rows from the ones you intended to remove.
Do this insteadRun SELECT with the intended WHERE condition first so you can verify the target rows.
Common mistake Assuming DELETE is finalized without commit().
Why it failsIn many database systems, DELETE marks rows for deletion within a transaction instead of immediately finalizing the change.
Do this insteadCall commit() after DELETE unless the database library or framework explicitly handles commits automatically.
From Pending to Permanent
In many database systems, executing DELETE creates a pending transaction change. Calling commit() tells the database to finalize the change and write it to disk, making the deletion permanent and visible to other users or programs accessing the database.
Deletion Checklist
- 1Identify the table containing the rows to remove.
- 2Write the specific condition that should identify those rows.
- 3Test the condition with SELECT.
- 4Confirm that the selected rows are exactly the rows you intend to delete.
- 5Execute DELETE with the WHERE condition.
- 6Call
commit()to finalize the deletion.
A Track table contains rows titled 'Bohemian Rhapsody', 'My Way', 'Imagine', and another 'My Way'. Describe which rows should remain after deleting rows whose title equals 'My Way', and state what you should do before executing DELETE.
Hints
- Evaluate the WHERE condition separately for each row.
- Use SELECT with the intended condition before DELETE.
- Remember to call
commit()after the deletion.
Safe Deletion Summary
- 1DELETE removes rows from a table.
- 2WHERE limits the deletion to rows that satisfy a specific condition.
- 3Omitting WHERE deletes every row in the table.
- 4Testing the condition with SELECT helps verify the intended targets.
- 5Calling
commit()finalizes the deletion.
At a glance
- Use DELETE to remove rows, and use WHERE to identify exactly which rows should be removed.
- Always test the WHERE condition with SELECT before executing DELETE.
- A DELETE statement without WHERE removes every row in the table.
- Call
commit()after DELETE to finalize the change.