📜  sql update \ - SQL (1)

📅  最后修改于: 2023-12-03 15:20:15.874000             🧑  作者: Mango

SQL Update

SQL Update statement is used to modify or update existing records in a database table. It is part of Data Manipulation Language (DML). The update statement can modify one or more columns in a table, and it can update one or many rows depending on the Where clause.

Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Parameters
  • table_name: The name of the table to update.
  • column1, column2, ...: The columns to be modified.
  • value1, value2, ...: The new values to be assigned to each column.
  • condition: The conditions that must be met for the rows to be updated.
Example

Suppose we have a table called employees with the following data:

| id | name | age | department | salary | |----|------|-----|------------|--------| | 1 | John | 30 | HR | 50000 | | 2 | Jane | 25 | IT | 60000 | | 3 | Mark | 35 | Finance | 70000 | | 4 | Mary | 40 | HR | 55000 |

Update a Single Column
UPDATE employees
SET salary = 65000
WHERE name = 'John';

This query updates the salary of John to 65000.

Update Multiple Columns
UPDATE employees
SET age = 27, department = 'Marketing', salary = 56000
WHERE id = 2;

This query updates the age, department, and salary of the employee with id 2.

Update Multiple Rows
UPDATE employees
SET salary = 60000
WHERE department = 'HR';

This query updates the salary of all employees in the HR department to 60000.

Conclusion

In conclusion, the SQL Update statement is used to modify existing data in a database table. It is a powerful tool that can update one or more columns, and one or many rows at a time. It is an essential part of SQL DML and can be used to keep data up to date and accurate.