Home Tutorials SQL Tutorial Updating Data
Updating Data

Updating Data


Updating Data

Definition: The UPDATE statement is used to modify existing records in a table. It allows you to change specific values for rows that have already been inserted into your database.

Learning Outcome: Mastering data modification is a core requirement for any SQL beginner. In real-world applications, data is rarely static; users change their addresses, status flags are toggled, and information is corrected as needed.


Syntax

The UPDATE statement requires the table name, the SET keyword to specify which columns to change, and—most importantly—a WHERE clause to target specific records.

UPDATE table_name
SET column1 = value1, column2 = value2
WHERE condition;

Example: Modifying a Specific Record

To update the city for a specific student (in this case, the student with ID #2), you would use the following query:

UPDATE students
SET city = 'Hubballi'
WHERE id = 2;

Explanation

  • SET: This tells the database exactly which field to change and what the new value should be.
  • WHERE id = 2: This is the most critical part of the query. It ensures that only the student with ID 2 is affected.
  • The Result: The record for student #2 is modified, while all other students in the table remain unchanged.

Key Notes

  • The Danger of Missing WHERE: Be extremely careful—if you omit the WHERE clause, the database will update every single row in the table. For example, UPDATE students SET city = 'Hubballi' would change everyone's city to Hubballi.
  • Updating Multiple Columns: You can update several fields at once by separating them with commas: SET city = 'Hubballi', age = 21.
  • Verification: It is standard practice to run a SELECT query with your WHERE condition first to verify you are targeting the correct rows before executing the UPDATE.

🏋️ Test Yourself With Exercises

Take our quiz on Updating Data to test your knowledge.

Browse Quizzes »