Lesson 29 of 60 – UPDATE
48%

MySQL UPDATE

The UPDATE statement is used to modify existing records in a MySQL table. You can update one column, multiple columns, or selected rows using a WHERE condition.

Note: Always use a WHERE condition when you only want to update specific records. Without WHERE, all rows in the table may be updated.

1. What is UPDATE?

The UPDATE statement changes existing data in a table.

UPDATE students
SET fee = 15000
WHERE id = 1;

This changes the fee of the student whose ID is 1.

2. Basic UPDATE Syntax

The basic syntax is:

UPDATE table_name
SET column_name = value
WHERE condition;

The SET clause specifies the new value, while WHERE identifies the rows to modify.

3. Updating One Column

You can update a single column.

UPDATE students
SET fee = 20000
WHERE id = 5;

Only the fee of student ID 5 is changed.

4. Updating Multiple Columns

You can update more than one column in the same statement.

UPDATE students
SET fee = 18000,
    course = 'Python'
WHERE id = 5;

Both fee and course are changed for the selected student.

5. UPDATE with WHERE

WHERE is used to identify specific records.

UPDATE students
SET fee = 15000
WHERE course = 'Python';

This updates all students whose course is Python.

6. UPDATE Without WHERE

If you omit WHERE, every row may be updated.

UPDATE students
SET fee = 10000;

This sets the fee of all students to 10000.

Warning: Avoid UPDATE without WHERE unless you intentionally want to modify every row.

7. UPDATE Using ID

Using a unique ID is one of the safest ways to update a single record.

UPDATE students
SET name = 'Amit Kumar'
WHERE id = 10;

Only the student with ID 10 is updated.

8. UPDATE Text Values

Text values should be placed inside quotes.

UPDATE students
SET course = 'Java'
WHERE id = 3;

The course of student ID 3 becomes Java.

9. UPDATE Numeric Values

Numeric values are normally written without quotes.

UPDATE students
SET age = 25
WHERE id = 3;

The student's age is changed to 25.

10. UPDATE Decimal Values

Decimal columns can also be updated.

UPDATE students
SET fee = 18500.50
WHERE id = 3;

The fee is changed to 18500.50.

11. UPDATE with Comparison

Comparison operators can be used in the WHERE condition.

UPDATE students
SET fee = fee + 1000
WHERE age > 20;

This increases the fee for students older than 20.

12. UPDATE with AND

Multiple conditions can be combined using AND.

UPDATE students
SET fee = fee + 1000
WHERE course = 'Python'
AND age >= 18;

Only Python students aged 18 or above are updated.

13. UPDATE with OR

OR can be used when either condition should match.

UPDATE students
SET fee = fee + 500
WHERE course = 'Python'
OR course = 'Java';

This updates students from Python or Java.

14. UPDATE with IN

IN is useful when several values should be updated.

UPDATE students
SET fee = fee + 1000
WHERE course IN ('Python', 'Java', 'PHP');

This increases the fee for students in the listed courses.

15. UPDATE with LIKE

LIKE can be used when records need to be selected based on a text pattern.

UPDATE students
SET course = 'Python'
WHERE name LIKE 'A%';

This changes the course for students whose names start with A.

16. UPDATE with BETWEEN

BETWEEN can be used to update records within a range.

UPDATE students
SET fee = fee + 500
WHERE age BETWEEN 18 AND 25;

This increases the fee for students between the specified ages.

17. UPDATE NULL Values

IS NULL can be used to find records with missing values.

UPDATE students
SET email = 'notprovided@example.com'
WHERE email IS NULL;

This replaces NULL email values with the specified value.

18. Set a Column to NULL

A nullable column can be changed to NULL.

UPDATE students
SET mobile = NULL
WHERE id = 7;

The mobile value of student ID 7 becomes NULL.

19. UPDATE Using an Expression

You can calculate a new value from the existing value.

UPDATE students
SET fee = fee + 1000
WHERE course = 'Python';

The existing fee is increased by 1000 instead of replacing it with a fixed value.

20. UPDATE with Multiple Conditions

UPDATE students
SET fee = fee + 2000
WHERE course IN ('Python', 'Java')
AND age >= 20
AND fee < 20000;

This updates only records that satisfy all the specified conditions.

21. UPDATE with LIMIT

MySQL allows LIMIT to restrict the number of rows affected by an UPDATE.

UPDATE students
SET fee = fee + 500
WHERE course = 'Python'
LIMIT 5;

This updates up to five matching records.

Tip: Be careful when using LIMIT without a clearly defined ordering or unique condition.

22. UPDATE Multiple Rows

An UPDATE statement can change many rows when multiple rows satisfy the WHERE condition.

UPDATE students
SET course = 'Python'
WHERE course = 'Python Full Stack';

Every matching record is updated.

23. UPDATE and Auto Increment ID

An AUTO_INCREMENT primary key normally does not need to be manually changed.

UPDATE students
SET name = 'Rahul Kumar'
WHERE id = 15;

Here, the ID is only used to identify the record.

24. UPDATE with Table Alias

Aliases can be useful in more advanced UPDATE queries.

UPDATE students AS s
SET s.fee = s.fee + 500
WHERE s.course = 'Python';

The alias s represents the students table.

25. UPDATE from Another Table

MySQL can update records using information from another table with a JOIN.

UPDATE students AS s
JOIN courses AS c
ON s.course = c.course_name
SET s.fee = c.fee
WHERE c.course_name = 'Python';

This can update student fees using values from the courses table.

26. Common UPDATE Mistakes

  • Forgetting the WHERE clause
  • Using the wrong WHERE condition
  • Updating more rows than intended
  • Forgetting quotes around text values
  • Using the wrong column name
  • Changing a primary key unnecessarily
  • Not checking the records before updating

27. Safe UPDATE Workflow

A safe UPDATE workflow is:

  1. Write the WHERE condition.
  2. Test the condition with SELECT.
  3. Check the records returned.
  4. Write the UPDATE statement.
  5. Execute the UPDATE.
  6. Run SELECT again to verify the result.
SELECT *
FROM students
WHERE id = 5;

UPDATE students
SET fee = 18000
WHERE id = 5;

SELECT *
FROM students
WHERE id = 5;

28. UPDATE Student Fee

Suppose the fee of a Python student needs to be increased by ₹1,000.

UPDATE students
SET fee = fee + 1000
WHERE course = 'Python';

This increases the existing fee rather than replacing it with a fixed amount.

29. Practical Student UPDATE

Suppose we want to update the course and fee of a particular student.

UPDATE students
SET course = 'Python',
    fee = 20000
WHERE id = 10;

Only student ID 10 is modified.

30. Complete UPDATE Example

CREATE TABLE students (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100),
    course VARCHAR(100),
    age INT,
    email VARCHAR(150),
    fee DECIMAL(10,2)
);

INSERT INTO students
(name, course, age, email, fee)
VALUES
('Amit', 'Python', 22, 'amit@gmail.com', 15000.00),
('Priya', 'Java', 21, 'priya@gmail.com', 18000.00),
('Rahul', 'PHP', 24, 'rahul@gmail.com', 12000.00),
('Neha', 'Python', 20, NULL, 15000.00);

SELECT *
FROM students
WHERE course = 'Python';

UPDATE students
SET fee = fee + 1000
WHERE course = 'Python';

SELECT *
FROM students
WHERE course = 'Python';

First, the Python students are displayed. Then their fees are increased by 1000. Finally, SELECT verifies the updated records.

📌 Key Points

  • UPDATE is used to modify existing records.
  • The SET clause specifies the new values.
  • WHERE identifies the records to update.
  • Without WHERE, all rows may be updated.
  • You can update one or multiple columns.
  • UPDATE can use AND, OR, IN, LIKE, BETWEEN, and IS NULL.
  • You can calculate new values using existing column values.
  • MySQL supports LIMIT with UPDATE.
  • Always test the WHERE condition using SELECT before updating important data.
  • After UPDATE, use SELECT to verify the changes.

🧠 Quick Quiz

Question: Which clause is normally used to specify which records should be modified by an UPDATE statement?