Loading
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.

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

idnamemarks
1Rahul85
2Anjali90
3Amit88


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_name
SET column1 = value1, column2 = value2
WHERE 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 students
SET marks = 92
WHERE id = 1;


Result

Only the student with id = 1 is updated.

idnamemarks
1Rahul92
2Anjali90
3Amit88


Updating Multiple Columns

UPDATE students
SET name = 'Amit Sharma', marks = 90
WHERE id = 3;


Updating All Rows (Without WHERE)

UPDATE students
SET 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