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.
// General syntax for updating records in MySQL
UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition;
Update a specific record by applying a condition using the WHERE clause.
// Update salary for a specific employee
UPDATE employees SET salary = 60000 WHERE employee_id = 5;
You can update more than one column in a single query.
// Update salary and department together
UPDATE employees SET salary = 65000, department = 'HR' WHERE employee_id = 5;
This query updates all matching rows using a condition.
// Increase salary for all marketing employees
UPDATE employees SET salary = 70000 WHERE department = 'Marketing';
The selected rows are modified instantly in the database. The number of affected rows depends on the condition used in the WHERE clause.
This PHP snippet demonstrates executing an UPDATE query using MySQLi.
// 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);
WHERE clause to avoid accidental full-table updatesSELECT query first to verify affected rowsActive for all employees joined last year