Update Statement - SQL
In SQL, the UPDATE statement is used to modify existing records (rows) in a table. It allows you to change the values of one or more columns in one or more rows based on specified conditions.
Here is the basic syntax of the UPDATE statement:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
- table_name: The name of the table to be updated.
- SET: Keyword indicating the beginning of the list of columns and their corresponding new values to be updated.
- column1 = value1, column2 = value2, ...: Specifies the columns to be updated along with their new values.
- WHERE: Optional clause used to specify conditions that must be met for the rows to be updated. If omitted, all rows in the table will be updated.
- condition: The condition that must be met for the rows to be updated.
UPDATE employees
SET salary = 50000, department_id = 20
WHERE employee_id = 1001;
This SQL statement updates the salary and department_id columns for the employee with an employee_id of 1001 in the employees table.
If you want to update multiple rows based on a condition, you can use the WHERE clause to specify the condition that all updated rows must meet. Additionally, you can use various comparison operators and logical operators to construct more complex conditions.
It's important to be cautious when using the UPDATE statement, especially without a WHERE clause, as it can modify a large number of rows if not used carefully. Always double-check your conditions to ensure that only the intended rows are updated.