Introduction
MySQL is a popular open-source relational database management system that allows users to store, organize, and retrieve data efficiently. It offers a wide range of features and functionalities, including the ability to modify existing records in a database table.
Updating Rows in MySQL
Updating rows in MySQL is a common task that can be accomplished using the UPDATE statement. This statement allows users to modify one or more records in a table based on specific conditions. The syntax for the UPDATE statement is as follows:
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
Example
Suppose we have a table named 'employees' that contains information about employees in a company. We want to change the salary of an employee named 'John Smith' from $50,000 to $60,000. We can do so using the following SQL statement:
UPDATE employees SET salary = 60000 WHERE name = 'John Smith';
This statement updates the 'salary' column of the 'employees' table for the record where the 'name' column equals 'John Smith'.
Updating Multiple Rows
In some cases, we may need to update multiple rows in a database table at once. This can be done by adding a WHERE clause that specifies the conditions for selecting the records to be updated. For example, the following statement updates the salaries of all employees who work in the 'Sales' department:
UPDATE employees SET salary = salary * 1.1 WHERE department = 'Sales';
This statement multiplies the 'salary' column by 1.1 for all records in the 'employees' table where the 'department' column equals 'Sales', effectively giving them all a 10% raise.
Conclusion
Updating rows in a MySQL database table is a useful way to modify existing data. By using the UPDATE statement, users can modify one or more records in a table based on specific conditions. It is also possible to update multiple rows at once by using a WHERE clause that specifies the conditions for selecting the records to be updated.