SQL UPDATE statement allows you to modify data in a database table. With SQL UPDATE statement, you can modify data of the whole table or subset of data based on condition in WHERE clause. Here is the syntax of SQL UPDATE statement.
UPDATE table_name
SET column1 = value1,
column2 = value2
WHERE condition
First you specify a database table where you want to update data after UPDATE keyword. You can update data in one column or more. If you update data in more than one column, the columns has to be separated by commas.
It is not required to provide the data directly to the SQL UPDATE statement. You can also use data which you retrieve from another table and then use it for SQL UDPATE statement as well. The data you can get from a SELECT statement but be sure that the select query must return a single value which is compatible type with the column you want to update.
The WHERE clause is optional, if you omit the WHERE clause all the records of the database table will be updated.
Let’s take a look at several examples of using SQL UPDATE.
Suppose one of employee in the company get married and need to change her last name, so you have to make the change by using the SQL UPDATE statement. Here is the query to do so:
UPDATE employees
SET lastname = 'Phan'
WHERE employeeID = 3.
Suppose her employee ID is 3.
Another example is one of employee of the company change the address so you want to update address information including address, city, region and postal code. In this case you can use SQL UPDATE to change this data.
UPDATE employees
SET address = '1300 Carter St',
city = 'San Jose',
postalcode = 95125,
region = 'CA'
WHERE employeeID = 3
In this tutorial, you've learn how to use SQL UPDATE statement to update data in a database table.