Concepts / DELETE: Removing Records from a Table

DELETE: Removing Records from a Table

UPDATE modifies existing records in a table using the SET keyword to specify columns and new values.

  • Programming

Why Existing Records Change

Database records often need correction or revision. A customer may change an email address, a product price may increase, or a song's play count may go up. UPDATE is the SQL statement used to modify values in columns of records that already exist in a table.

UPDATE modifies existing records in a table. The SET clause identifies the columns and replacement values, while an optional WHERE clause determines which rows are changed.

How WHERE Selects Rows

The WHERE clause filters the table before the update is applied. The database checks each row against the condition. Rows that match are changed; rows that do not match remain untouched. If several rows satisfy the condition, one UPDATE statement changes all of them. If no row satisfies it, the statement changes nothing.

evaluatematchesdoes not matchTrack tableexisting rowstitle = 'My Way'check each rowMatching rowsplays becomes 16Other rowsremain untouched
How does the database determine which rows will be changed by an UPDATE statement?
sql

This statement targets rows in Track whose title is exactly My Way and changes their plays value to 16. If multiple rows have that title, all matching rows are updated. If no row has that title, no rows change.

Mapping Columns with SET

The SET clause describes the replacement values. Each assignment names a column, uses an equals sign, and gives the new value. Multiple assignments are separated by commas, so one UPDATE can change several columns in the same row or rows.

namesusesassignsthenassignsthen filtersUPDATEchoose tableCustomertableSETbegin assignmentsemailnew email value,separate assignmentsphonenew phone valueWHEREfilter rows
How does each column named in SET map to its new value?
sql

Here, customer_id = 5 identifies the target customer. Both email and phone are changed in the same operation. The assignments in SET are separated by commas, and their order does not matter because SQL processes them together.

Selected Rows and Every Row

A WHERE clause limits the update to rows that satisfy its condition. Omitting WHERE removes that filter. In that case, every row in the table is modified. This is the critical difference between a targeted update and an all-rows update.

targeted updatenot matchedall rows updatedpending ordersbeforeprocessed ordersWHERE status = 'pending'other ordersbeforeother ordersuntouchedall rowsbeforenew values in allrowsno WHERE clause
What changes in the table when an UPDATE includes a WHERE clause versus when it omits one?
sql

This statement changes every pending order to processed. If 100 orders are pending, all 100 are updated by the one statement. The statement is efficient because it handles matching rows together, but the condition must be checked carefully.

Checking Before Changing

Testing the Target Rows

You want to update every pending order to processed.

Write a SELECT with the intended filter: Use the same WHERE condition that you plan to use with UPDATE so you can inspect the rows that would be affected.

Inspect the returned rows: Confirm that the result contains exactly the records you intend to change. Unexpected rows indicate that the condition needs revision.

Run the UPDATE only after verification: Use the verified condition in the UPDATE statement to change the matching rows.

The SELECT acts as a check on the WHERE clause before the data is modified.

sql

Run the SELECT first and inspect its rows. If it returns exactly the records you expect, reuse the same WHERE clause in the UPDATE. If it returns unexpected rows, revise the condition and test again before making any change.

Mistakes to Avoid

  • Omitting the WHERE clause when only selected rows should change

    Without WHERE, every row in Customer is modified.

    Fix: Add a condition that identifies the intended row or rows, then verify that condition with SELECT.

  • Using a condition that matches more rows than intended

    Every row matching the condition is updated, not just the row you had in mind.

    Fix: Choose and verify a WHERE condition that targets the intended records.

  • Skipping the verification step

    You lose the opportunity to inspect the rows before changing them.

    Fix: Run SELECT with the exact same WHERE clause first.

Practice the Decision

EASY

Write an UPDATE statement that changes the phone column for the customer whose customer_id is 5. Then describe how you would verify the target row before running the UPDATE.

Hints
  • Name the Customer table after UPDATE.
  • Use SET to assign a new value to phone.
  • Use WHERE customer_id = 5 to identify the intended customer.
  • Test the same WHERE condition with SELECT first.

What do you think happens?

Suppose a table contains 100 pending orders. How many rows will this statement update: UPDATE Order SET status = 'processed' WHERE status = 'pending';

  • One row
  • Only the first matching row
  • All 100 pending rows
  • No rows
Reveal answer

Answer: All 100 pending rows

A single UPDATE changes every row that matches its WHERE condition.

Key Takeaways

  1. UPDATE modifies values in records that already exist.
  2. SET names the columns and replacement values.
  3. WHERE determines which rows are changed; matching multiple rows updates all of them.
  4. Without WHERE, every row in the table is modified.
  5. Verify the exact WHERE clause with SELECT before running UPDATE.

Key Takeaways

  • UPDATE changes existing records rather than adding new rows.
  • SET specifies one or more column assignments.
  • WHERE filters the rows that receive the update.
  • A missing WHERE clause causes every row in the table to be modified.
  • Use SELECT with the same WHERE clause to verify the target rows before updating.