← Back to Chapters

MySQL UPDATE Statement

?️ MySQL UPDATE Statement

? Quick Overview

The UPDATE statement in MySQL is used to modify existing records in a table. It allows changing one or more column values based on a condition.

? Key Concepts

  • UPDATE modifies existing rows
  • SET assigns new values
  • WHERE filters rows to update
  • Can update single or multiple columns

? Syntax / Theory

? View Code Example
// General syntax for updating records in MySQL
UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition;

? Code Example 1: Basic UPDATE

Update a specific record by applying a condition using the WHERE clause.

? View Code Example
// Update salary for a specific employee
UPDATE employees SET salary = 60000 WHERE employee_id = 5;

? Code Example 2: Update Multiple Columns

You can update more than one column in a single query.

? View Code Example
// Update salary and department together
UPDATE employees SET salary = 65000, department = 'HR' WHERE employee_id = 5;

? Code Example 3: Conditional UPDATE

This query updates all matching rows using a condition.

? View Code Example
// Increase salary for all marketing employees
UPDATE employees SET salary = 70000 WHERE department = 'Marketing';

? Live Output / Explanation

What Happens?

The selected rows are modified instantly in the database. The number of affected rows depends on the condition used in the WHERE clause.

⚙️ Interactive Example (PHP + MySQL)

This PHP snippet demonstrates executing an UPDATE query using MySQLi.

? View Code Example
// Update employee status using PHP and MySQLi
$conn = mysqli_connect("localhost","root","","company");
$sql = "UPDATE employees SET status='Active' WHERE experience > 1";
mysqli_query($conn,$sql);

? Use Cases

  • Updating user profiles
  • Changing order or payment status
  • Modifying salaries or roles
  • Bulk updates during migrations

✅ Tips & Best Practices

  • Always use a WHERE clause to avoid accidental full-table updates
  • Run a SELECT query first to verify affected rows
  • Use transactions for critical updates
  • Keep database backups before large updates

? Try It Yourself

  • Update salary for employees with more than 5 years experience
  • Change department for employees from a specific city
  • Set status to Active for all employees joined last year