SQL Update Query Example | SQL Update Statement Tutorial
SQL Update Query Example | SQL Update Statement Tutorial is today’s topic. The SQL UPDATE statement is used to modify the existing records in a table. You need to be very careful when updating records in a table. SQL WHERE clause in the UPDATE statement specifies which record(s) that should be updated. If you omit the WHERE clause completely, then all records in the table will be updated!
SQL Update Query Example
You need to specify which record needs to be updated via WHERE clause, otherwise all the rows would be affected. We can update the single column as well as multiple columns using the UPDATE statement as per our requirement.
SQL Update Syntax
The syntax is following.
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
In the above query, the SET statement is used to set the new values to a particular column and the WHERE clause condition is used to select the rows for which the columns needed to be updated.
See the following example.
I have the following table called Apps.
select * from Apps
The output is following.
So, we have a table with six rows. Now, we will write an update query to modify one or multiple rows.
Update one row in SQL
Now, let’s write an update query that can affect only one row.
UPDATE Apps SET CreatorName = 'Tony Stark', AppName= 'IronSpider' WHERE AppID = 1
Here, we are updating the row whose AppID = 1. We are updating the CreatorName and AppName.
It will not return anything. So, if we need to check the output, write the following query.
select * from Apps
See the below output.
See, we have updated the row whose AppID = 1.
Update multiple rows in SQL
Let’s write a query where we update multiple rows.
UPDATE Apps SET CreatorName = 'CaptainAmerica', AppName= 'Shield' WHERE AppID IN('4', '5');
The above query will update the rows whose AppIDs are 4 and 5.
Now, check the output using the below query.
You can see that AppID 4 and 5 have updated values.
Update All records in SQL
We can write an SQL Update query in which we update all the rows if we do not specify the WHERE condition. See the below query.
UPDATE Apps SET CreatorName = 'Thor', AppName= 'Asguard'
It will update the CreatorName and AppName for all the six rows. Verify the output by the following query.
Conclusively, SQL Update Query Example | SQL Update Statement Tutorial is over.