Updating Data
Introduction to SQL
Database
SQL Basics
Creating a Database
Using a Database
Creating a Table
Common Data Types
Inserting Data
Selecting Data
WHERE Clause
Comparison Operators
AND, OR, and NOT
ORDER BY Clause
LIMIT Clause
DISTINCT
LIKE Operator
IN, BETWEEN, and IS NULL
Updating Data
Deleting Data
ALTER TABLE
DROP TABLE
PRIMARY KEY and NOT NULL
FOREIGN KEY and Relationships
Aggregate Functions
GROUP BY Clause
HAVING Clause
JOIN Basics
INNER JOIN
LEFT JOIN
Aliases
Subqueries
Indexes
User Permissions and Security
Common Mistakes Beginners Make
Practice Ideas
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
WHEREclause, 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
SELECTquery with yourWHEREcondition first to verify you are targeting the correct rows before executing theUPDATE.
🏋️ Test Yourself With Exercises
Take our quiz on Updating Data to test your knowledge.
Browse Quizzes »