SQL UPDATE
The UPDATE command in SQL is used to modify existing records in a database table.
Whenever you need to change stored data, such as correcting a name, updating marks, or changing a status, the UPDATE command is used.
Example: Students Table
Syntax
Updating a Single Column
Result
Only the student with id = 1 is updated.
Updating Multiple Columns
Updating All Rows (Without WHERE)
Important:
The UPDATE command changes existing data permanently.
Understanding UPDATE
Consider a table as a register where data is already written.
- INSERT adds new entries
- UPDATE corrects or modifies existing entries
Using UPDATE is similar to erasing a value in a register and writing a new one at the same place.
Example: Students Table
| id | name | marks |
|---|---|---|
| 1 | Rahul | 85 |
| 2 | Anjali | 90 |
| 3 | Amit | 88 |
Why Use the UPDATE Command?
The UPDATE command is used to
- Correct incorrect data
- Change values after re-evaluation
- Update records based on conditions
- Maintain accurate and up-to-date data
Syntax
UPDATE table_nameSET column1 = value1, column2 = value2WHERE condition;- UPDATE specifies the table to modify
- SET defines new values for columns
- WHERE filters which rows will be updated
Updating a Single Column
UPDATE studentsSET marks = 92WHERE id = 1;Result
Only the student with id = 1 is updated.
| id | name | marks |
|---|---|---|
| 1 | Rahul | 92 |
| 2 | Anjali | 90 |
| 3 | Amit | 88 |
Updating Multiple Columns
UPDATE studentsSET name = 'Amit Sharma', marks = 90WHERE id = 3;Updating All Rows (Without WHERE)
UPDATE studentsSET marks = 100;This will update all rows in the table.
Always use WHERE unless intentionally updating every record.
Important Rules for Best Practices
- Always use WHERE to avoid accidental mass updates
- Text values must be enclosed in single quotes
- Use conditions carefully to target correct rows
- Test updates using SELECT before executing UPDATE
Two Minute Drill
- UPDATE modifies existing records in a table
- SET assigns new values to columns
- WHERE controls which rows are updated
- Without WHERE, all rows ae affected
- UPDATE changes data permanently
- Always verify conditions before execution