This is a detailed guide of the SQL Update Statement. Learn how you can update the database table records and can change the column values using the Update Query.
Use of SQL Update Statement
The SQL Update Statement is used whenever you want to change or modify the table records of your database. This way you can change any column value of any table row. The Update Statement is mostly used with SQL WHERE Clause to find out the right table cell to change the value. You can also update the value of multiple columns as well as multiple rows at the same time in a single Update Query.
The basic syntax for the SQL Update Query is written below. Here we’re updating the col_1 and col_2, respectively with values val_1 and val_2 in the
table_name where the condition is True.
SET col_1 = val_1, col_2 = val_2, ...
Note. It is not necessary to use the WHERE Clause but if you won’t use it will update the column values for the entire table rows.
SQL Update Query Examples
Here in this section, we’ve given three different examples for the SQL Update Query. Each example is different in its own kind and illustrates a unique use case.
For demonstration, we’ll use the demo database in which we have the standards table. The existing state of the table is shown in the screenshot given below.
There are 7 columns in the table and the last two columns have the NULL values set for all of the records.
- In the first query, we’ll update the values for two columns of a single table record.
- In the second query, we’ll update a single column for multiple rows.
- In the third and last example query, we’ll update the value for one null column for the entire table to illustrate the importance of the WHERE clause.
Note. You must put the strings in either single or double quotes but need not put integer values in any kind of quote.
Updating Single Record
UPDATE standards SET standard_full_name = 'Tenth', standard_short_name = '10th' WHERE ID = 10;
The above query updates the column values for the column
standard_short_name for the table row with ID 10.
Updating Multiple Records
UPDATE standards SET standard_description = 'Junior' WHERE ID <= 4;
The above query updated the column standard_description values for all the table records with ID either four or less than four. There are four table records with IDs 1, 2, 3 and 4 that are updated after the execution of this query.
Updating All Table Records
UPDATE standards SET standard_comments = 'Happy Students!';
This example illustrates why you should always use the Update Query with WHERE clause and shows that you may end up in updating the entire table’s column values if you won’t use it. In the above query, the
standard_comments column is updated for the entire table.
The following screenshot shows the table state after the execution of all the queries specified in the above examples.
I hope you find this guide useful. If so, do share it with others who are willing to learn SQL. If you have any questions related to this article, feel free to ask them in the comments section.
Helpful Associated Guides.
Also, don’t forget to Subscribe to WTMatter!