The UPDATE statement in SQL Server is used to modify existing data in a table.
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
- Explanation:
- table_name: The table where the data is to be updated.
- SET: Specifies the columns to update and their new values.
- WHERE: Specifies the condition to identify the rows to be updated (optional but recommended to avoid updating all rows).
Example:
- Update a single column:
UPDATE employees
SET age = 31
WHERE id = 1;
- Update multiple columns:
UPDATE employees
SET name = 'John Smith', age = 32
WHERE id = 1;
Key Points:
- WHERE clause is crucial to limit which rows are updated. Without it, all rows in the table will be updated.
- The SET clause can update multiple columns in a single statement.
- Ensure data integrity by checking conditions before updating to avoid accidental changes to all records.