SELECT: Retrieving Data from Tables
UPDATE modifies existing records in a table using the SET keyword to specify columns and new values.
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…
Changing Data Already Stored
Data in a database can change after a row has been created. A customer may update an email address, a product price may change, or a song's play count may increase. UPDATE is the SQL statement used to modify values in columns of records that already exist in a table. INSERT adds new rows; UPDATE changes values in rows that are already present.
The central question when using UPDATE is not only what value should change, but also which rows should receive that change.
Reading an UPDATE Statement
An UPDATE statement has three important parts. UPDATE identifies the table whose existing records may change. SET lists the column assignments, pairing each column with its new value. WHERE filters the rows that are eligible for the change. The WHERE clause is optional in the statement's structure, but omitting it means that every row in the table is modified.
This statement targets the Track table, assigns the value 16 to the plays column, and limits the update to rows whose title equals My Way. If several rows have that title, all matching rows are updated. If no row matches, the statement runs without changing any rows.
Selecting Rows with WHERE
WHERE determines which existing records are changed and which remain untouched. In the Track example, title = 'My Way' is the condition. Rows with another title do not receive the new plays value. A condition may match one row, many rows, or no rows, so its scope must be considered before the statement runs.
UPDATE Track
SET plays = 16
WHERE title = 'My Way';- 1The database considers rows in Track.
- 2It checks which rows satisfy the WHERE condition.
- 3For each matching row, it changes plays to 16.
- 4Rows that do not satisfy the condition remain unchanged.
- UPDATE Track selects the table containing the existing records.
- SET plays = 16 assigns the new value to the plays column.
- WHERE title = 'My Way' limits the change to matching rows.
One Row or Every Row
| Statement form | Rows affected | Safety implication |
|---|---|---|
| UPDATE with WHERE | Rows matching the condition | Only the selected rows are modified |
| UPDATE without WHERE | Every row in the table | All rows receive the change |
The presence or absence of WHERE controls the scope of an UPDATE.
This statement changes every order whose status is pending to processed. If 100 orders match the condition, all 100 are updated by this one statement. That is useful when the same change is intended for a group, but it also makes an overly broad condition consequential.
The second statement has no WHERE clause. Therefore, it modifies the status value in every row of Order, not only rows that were previously pending. The difference between these two statements is a single clause, but the number of affected records can be dramatically different.
Changing Several Columns
SET can contain more than one column assignment. Separate the assignments with commas. This allows one UPDATE statement to change several values in the same row or rows. For example, a customer may update both an email address and a phone number in one operation.
The WHERE condition identifies customer 5. Both assignments in SET apply to that customer's record. The order of the assignments does not matter; SQL processes them together.
Verify Before You Modify
The SELECT is used as a check: it shows which rows satisfy the condition. The UPDATE then uses that same condition to modify those rows. This is especially important because a condition may match multiple records, and an UPDATE can change all of them at once.
Common UPDATE Mistakes
Common mistake Leaving out the WHERE clause when only selected rows should change
What it looks likeUPDATE Track SET plays = 16;Why it failsWithout WHERE, every row in Track is modified.
Do this insteadAdd and verify a condition that identifies the intended rows.
Common mistake Using a condition that matches more rows than intended
What it looks likeWHERE title = 'My Way'Why it failsIf several rows have that title, all of them are updated.
Do this insteadUse a condition that identifies the intended records, and test it with SELECT first.
Common mistake Changing only one column when several related values need updating
What it looks likeUpdating the email but not the phone number for a customer who changed both
Why it failsThe record remains partly outdated.
Do this insteadPlace both column assignments in SET, separated by commas.
Common mistake Assuming that no matching row is an error
What it looks likeA WHERE condition matches no records.
Why it failsThe statement can run without error while changing nothing.
Do this insteadCheck the condition and confirm that the intended record exists.
Practice the Target
Write an UPDATE statement that changes the email and phone columns for the customer whose customer_id is 5. Then describe what could happen if you removed the WHERE clause.
Hints
- Begin with UPDATE and the Customer table.
- Put both column assignments in SET and separate them with a comma.
- Use
customer_id= 5 as the WHERE condition. - Without WHERE, every row in Customer would be modified.
Before executing an update like this, use a SELECT with WHERE customer_id = 5 to verify that the returned record is the customer you intend to modify.
Essential Takeaways
- 1UPDATE modifies values in existing records; it does not add new rows.
- 2SET names the columns and supplies their new values.
- 3A WHERE clause filters the rows that receive the update.
- 4Without WHERE, every row in the table is modified.
- 5Use SELECT with the same WHERE clause before UPDATE to verify the intended target rows.
At a glance
- UPDATE changes values in records that already exist in a table.
- SET specifies one or more column and value assignments.
- WHERE determines which rows are changed; without it, all rows are changed.
- One UPDATE can affect many matching rows or no rows at all.
- Verify the target rows with SELECT before running UPDATE.