The UPDATE statement in MySQL is used to modify the data in one or more rows of a table. The basic syntax of the UPDATE statement is as follows:
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
table_name is the name of the table that we want to update.
column2, etc. are the names of the columns that we want to update.
value2, etc. are the new values that we want to set for the corresponding columns.
condition is a condition that determines which rows to update.
For example, let’s say we have a table named
employees with columns
salary. To update the salary of an employee with
id = 1 to
55000, we can use the following SQL query:
UPDATE employees SET salary = 55000 WHERE id = 1;
If we want to update multiple columns, we can specify them in the SET clause separated by commas. For example, to update the name and age of an employee with
id = 2 to
"Jane Smith" and
26, respectively, we can use the following SQL query:
UPDATE employees SET name = 'Jane Smith', age = 26 WHERE id = 2;
If we want to update multiple rows, we can specify the condition that selects those rows in the WHERE clause. For example, to update the salaries of all employees whose age is greater than or equal to
60000, we can use the following SQL query:
UPDATE employees SET salary = 60000 WHERE age >= 30;
Note that if we omit the WHERE clause, the UPDATE statement will update all rows in the table. This can be dangerous and should be avoided unless we really want to update all rows.