The UPDATE
statement in SQL is used to modify existing records in a database table. It allows you to update one or more records in a table based on a specified condition using the WHERE
clause.
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Explanation:
WHERE
clause) to specify which record(s) should be updated. Without the WHERE
clause, all records in the table will be updated!Let’s assume we have a table named Employees
. Here’s an example of how to update a record:
UPDATE Employees
SET Department = 'Marketing'
WHERE ID = 1;
This query updates the department of the employee with ID 1 to 'Finance'.
To update multiple columns in a table, you can simply separate each column and its new value with a comma:
UPDATE Employees
SET Name = 'Khan Shaista', Department = 'IT'
WHERE ID = 1;
This updates both the name and department of the employee with ID 1.
You can update multiple records at once by using a condition that matches more than one row:
UPDATE Employees
SET Department = 'HR'
WHERE Department = 'Admin';
This query updates the department of all employees in the 'Admin' department to 'HR'.
SQL UPDATE
is used across various relational database systems, such as: