The SQL UPDATE statement is used to modify the existing records in a table. You can update one or more columns in one or more rows based on a specified condition.
Basic Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
- table_name: The name of the table where the records need to be updated.
- column1, column2, ...: The columns to update.
- value1, value2, ...: The new values that will replace the existing values in those columns.
- WHERE condition: This specifies which rows should be updated. If omitted, all rows in the table will be updated.
Example 1: Update a single column
UPDATE employees
SET salary = 60000
WHERE employee_id = 101;
This will update the salary column of the employee with employee_id 101 to 60000.
Example 2: Update multiple columns
UPDATE employees
SET salary = 70000, department = 'Marketing'
WHERE employee_id = 102;
This will update both the salary and department columns for the employee with employee_id 102.
Example 3: Update all rows (no WHERE clause)
UPDATE employees
SET department = 'HR';
This will set the department of all employees to 'HR'.
Important Notes:
- Be careful when using the
UPDATEstatement without aWHEREclause as it will update all rows in the table. - To ensure you are updating the correct rows, always use the
WHEREclause to specify the condition.
If you need help with any specific UPDATE query, feel free to ask!
No comments:
Post a Comment