Dear readers of our blog, we'd like to recommend you to visit the main page of our website, where you can learn about our product SQLS*Plus and its advantages.
 
SQLS*Plus - best SQL Server command line reporting and automation tool! SQLS*Plus is several orders of magnitude better than SQL Server sqlcmd and osql command line tools.
 

REQUEST COMPLIMENTARY SQLS*PLUS LICENCE

Enteros UpBeat offers a patented database performance management SaaS platform. It proactively identifies root causes of complex revenue-impacting database performance issues across a growing number of RDBMS, NoSQL, and deep/machine learning database platforms. We support Oracle, SQL Server, IBM DB2, MongoDB, Casandra, MySQL, Amazon Aurora, and other database systems.

SQL UPDATE

8 June 2020

SQL UPDATE

UPDATE command – makes changes to an already existing record or to a set of records in an SQL table. Changes the existing values in the table or in the main view table.

UPDATE command. Syntax command

UPDATE command. Basic keywords and parameters of the UPDATE command

  • schema – authorization identifier, usually matching the name of some user
  • table view – name of the SQL table in which the data is changed; if a view is defined, the data is changed in the main SQL table of the view
  • subquery_1 – a subquery that the server handles in the same way as a view
  • solumn – a column of an SQL table or SQL view whose value is changed; if a table column from a SET proposal is omitted, the column value remains unchanged
  • expr – new value assigned to the corresponding column; this expression may contain main variables and optional indicator variables
  • subquery_2 – new value assigned to the corresponding column
  • subquery_3 – new value assigned to the corresponding column

WHERE – determines the range of rows to be modified by those for which a certain condition is TRUE; if this phrase is omitted, all rows in the table or view are modified.

When issuing a UPDATE statement, any UPDATE trigger defined on the table is included.

Subqueries. If the SET offer contains a subquery, it returns exactly one line for each line being modified. Each value resulting from the subquery is assigned to the corresponding list columns in parentheses.

If the subquery does not return any rows, the column is assigned NULL. Subqueries can select data from a modifiable table. The SET offer can combine expressions and subqueries.

UPDATE command. Example 1
Change for all buyers of the rating by the value of 200:

UPDATE Customs SET rating = 200;

UPDATE command. Example 2
Substitution of the column value in all rows of the table is rarely used as a rule. Therefore, in the UPDATE command, as in the DELETE command, a predicate can be used. To perform the specified substitution of the rating column values, all buyers that are served by the Giovanni seller (snum = 1003) should be entered:

UPDATE Customs SET rating = 200 WHERE snum = 1001;

UPDATE command. Example 3
In the SET offer you can specify any number of values for the columns separated by commas:

UPDATE emp SET job = 'MANAGER', sal = sal + 1000, deptno = 20 WHERE ename = 'JONES';

UPDATE command. Example 4
In the SET sentence you can specify the value of NULL without using any special syntax (such as IS NULL). Thus, if you want to set all ratings of buyers from London (city = ‘London’) equal to NULL-value, you must enter:

UPDATE Customs SET rating_id = NULL WHERE city_id = 'London';

UPDATE command. Example 5
Explains how to use the following UPDATE command syntactic constructs:

  • Both SET sentence forms together in the same statement.
  • Subquery.
  • A WHERE sentence that restricts the range of lines to be modified.

UPDATE emp a SET deptno =
(SELECT deptno FROM dept WHERE loc = 'BOSTON'), (sal, comm) = (SELECT 1.1*AVG(sal), 1.5*AVG(comm) FROM emp b WHERE a.deptno = b.deptno) WHERE deptno IN (SELECT deptno FROM dept WHERE loc = 'DALLAS' OR loc = 'DETROIT');

The above statement UPDATE performs the following operations:

  • Modifies only those employees who work in Dallas or Detroit
  • Sets the deptno column for employees from Boston.
  • Sets the salary of each employee at 1.1 times the average salary of the entire department.
  • Sets the commission of each employee at 1.5 times the average commission of the whole department.

Example – update one column

Let’s look at an example that shows how to use the UPDATE SQL statement to update a single column in a table.
In this example of UPDATE we have a table with the following data:

custom_idf_namel_namefav_website
4000JustinBiebergoogle.com
5000SelenaGomezbing.com
6000 MilaKunisyahoo.com
7000TomCruiseoracle.com
8000JohnnyDeppNULL
9000RussellCrowegoogle.com

 

Now let’s demonstrate how UPDATE works by updating one column in the customers table. Enter the following UPDATE command.

UPDATE customs
SET f_name = 'Joseph'
WHERE custom_id = 8000;

1 record will be updated. Select the data from the table again:

SELECT *
FROM customs;

Here are the results you should get.

In this example, UPDATE for the first_name field is set to ‘Joseph’ in the customers table where customer_id is 8000.

Example – update several columns

Let’s look at the UPDATE example, which shows how to update more than one column in a table.
Tip:. When you update multiple columns in the UPDATE expression, you need to separate the column / value pairs in the SET sentence with commas.

In this UPDATE example, we have a table with the following data:

custom_idf_namel_namefav_website
4000JustinBiebergoogle.com
5000SelenaGomezbing.com
6000 MilaKunisyahoo.com
7000TomCruiseoracle.com
8000JosephDeppNULL
9000RussellCrowegoogle.com

 

suppl_idsuppl_namecity_idstate_id
100YandexMoscowRussian
200GoogleLansingMichigan
300OracleRedwood CityCalifornia
400BingRedmondWashington
500YahooSunnyvaleWashington
600DuckDuckGoPaoliPennsylvania
700QwantParisFrance
800FacebookMenlo ParkCalifornia
900Electronic ArtsSan FranciscoCalifornia

 

Now let’s demonstrate how to use the UPDATE operator to update more than one column value at a time. Enter the following UPDATE command.

UPDATE suppls
SET suppl_id = 150,
suppl_name = 'Apple',
city_id = 'Cupertino'
state_id = 'California'
WHERE suppl_name = 'Google';

1 record will be updated. Select the data from the supplier table again:

SELECT *
FROM suppls;

Here are the results you should get.

suppl_idsuppl_namecity_idstate_id
100YandexMoscowRussian
150AppleCupertinoCalifornia
300OracleRedwood CityCalifornia
400BingRedmondWashington
500YahooSunnyvaleWashington
600DuckDuckGoPaoliPennsylvania
700QwantParisFrance
800FacebookMenlo ParkCalifornia
900Electronic ArtsSan FranciscoCalifornia

 

In this example, UPDATE will update supplier_id to 150, supplier_name to ‘Apple’, city to ‘Cupertino’ and state to ‘California’, where supplier_name is ‘Google’.

Example – table update with data from another table

Let’s look at the UPDATE example, which shows how to update a table with data from another table.
In this UPDATE example, we have a product table with the following data:

prod_idprod_namecat_id
1Pear50
2Banana50
3Orange50
4Apple50
5Bread75
6Sliced Ham25
7KleenexNULL

 

And the summary_data table with the following data:

prod_idcurrent_cat
110
210
310
410
510
810

 

Now let’s update the summary_data table with values from the products table. Enter the following UPDATE command:

UPDATE summary_data
SET current_category = (SELECT category_id
FROM products
WHERE products.product_id = summary_data.product_id)
WHERE EXISTS (SELECT category_id
FROM products
WHERE products.product_id = summary_data.product_id);

Five records will be updated. Again select data from the summary_data table:

SELECT *
FROM summary_data;

Here are the results you should get.

prod_idcurrent_cat
150
250
350
450
575
810

 

In this example, the current_category field in the summary_data table will be updated with category_id from the products table where the product_id values match. The first 5 records in the summary_data table were updated.

Tip: Note that our UPDATE operator has included the EXISTS condition in the WHERE sentence to make sure that the product_id and summary_data tables match the product_id before updating the record.

Had we not included the EXISTS condition, the UPDATE query would have updated the current_category field to NULL on row 6 of the summary_data table (since the products table has no record where product_id = 8).

The SQL UPDATE Statement

 
Tags: , ,

MORE NEWS

 

Preamble​​NoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...

Preamble​​MongoDB Conditional operators specify a condition to which the value of the document field shall correspond.Comparison Query Operators $eq...

5 Database management trends impacting database administrationIn the realm of database management systems, moreover half (52%) of your competitors feel...

The data type is defined as the type of data that any column or variable can store in MS SQL Server. What is the data type? When you create any table or...

Preamble​​MS SQL Server is a client-server architecture. MS SQL Server process starts with the client application sending a query.SQL Server accepts,...

First the basics: what is the master/slave?One database server (“master”) responds and can do anything. A lot of other database servers store copies of all...

Preamble​​Atom Hopper (based on Apache Abdera) for those who may not know is an open-source project sponsored by Rackspace. Today we will figure out how to...

Preamble​​MongoDB recently introduced its new aggregation structure. This structure provides a simpler solution for calculating aggregated values rather...

FlexibilityOne of the most advertised features of MongoDB is its flexibility.  Flexibility, however, is a double-edged sword. More flexibility means more...

Preamble​​SQLShell is a cross-platform command-line tool for SQL, similar to psql for PostgreSQL or MySQL command-line tool for MySQL.Why use it?If you...

Preamble​​Writing an application on top of the framework on top of the driver on top of the database is a bit like a game on the phone: you say “insert...

Preamble​​Oracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...

Preamble​​IBM pureXML, a proprietary XML database built on a relational mechanism (designed for puns) that offers both relational ( SQL / XML ) and...

  What is PostgreSQL array? In PostgreSQL we can define a column as an array of valid data types. The data type can be built-in, custom or enumerated....

Preamble​​If you are a Linux sysadmin or developer, there comes a time when you need to manage an Oracle database that can work in your environment.In this...

Preamble​​Starting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...