The UPDATE statement in SQL is used to modify the existing records in a table. You can update one or more columns of a table based on a specific condition, which is provided using the WHERE clause. If no WHERE clause is included, all records in the table will be updated.
Syntax
Explanation:
table_name: The name of the table where you want to make updates.column1,column2, etc.: The columns you want to update.value1,value2, etc.: The new values you want to assign to the columns.WHERE condition: This is a filter to specify which rows should be updated. If you omit this, all rows in the table will be updated.
Examples:
1. Simple Update
To update a single column in a table:
This updates the salary of the employee with employee_id 101 to 55,000.
2. Updating Multiple Columns
To update multiple columns in a table:
This sets the salary to 60,000 and the department to 'Marketing' for the employee with employee_id 102.
3. Update Without a WHERE Clause
If you omit the WHERE clause, all rows in the table are updated:
This will set the status column to 'Inactive' for all employees in the employees table.
4. Using Conditions
You can also use conditions for more complex updates. For example, updating based on a range of values:
This increases the price of all products in the 'Electronics' category by 10%, but only for products priced under 500.
Important Notes:
- Always use the
WHEREclause to specify which rows you want to update. Otherwise, all rows in the table will be modified. - Make sure the values you're updating are compatible with the column data types.
- You can use subqueries or joins in the
WHEREclause or even in theSETclause for more advanced use cases.
No comments:
Post a Comment