REQUEST COMPLIMENTARY SQLS*PLUS LICENCE
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_id | f_name | l_name | fav_website |
---|---|---|---|
4000 | Justin | Bieber | google.com |
5000 | Selena | Gomez | bing.com |
6000 | Mila | Kunis | yahoo.com |
7000 | Tom | Cruise | oracle.com |
8000 | Johnny | Depp | NULL |
9000 | Russell | Crowe | google.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_id | f_name | l_name | fav_website |
---|---|---|---|
4000 | Justin | Bieber | google.com |
5000 | Selena | Gomez | bing.com |
6000 | Mila | Kunis | yahoo.com |
7000 | Tom | Cruise | oracle.com |
8000 | Joseph | Depp | NULL |
9000 | Russell | Crowe | google.com |
suppl_id | suppl_name | city_id | state_id |
---|---|---|---|
100 | Yandex | Moscow | Russian |
200 | Lansing | Michigan | |
300 | Oracle | Redwood City | California |
400 | Bing | Redmond | Washington |
500 | Yahoo | Sunnyvale | Washington |
600 | DuckDuckGo | Paoli | Pennsylvania |
700 | Qwant | Paris | France |
800 | Menlo Park | California | |
900 | Electronic Arts | San Francisco | California |
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_id | suppl_name | city_id | state_id |
---|---|---|---|
100 | Yandex | Moscow | Russian |
150 | Apple | Cupertino | California |
300 | Oracle | Redwood City | California |
400 | Bing | Redmond | Washington |
500 | Yahoo | Sunnyvale | Washington |
600 | DuckDuckGo | Paoli | Pennsylvania |
700 | Qwant | Paris | France |
800 | Menlo Park | California | |
900 | Electronic Arts | San Francisco | California |
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_id | prod_name | cat_id |
---|---|---|
1 | Pear | 50 |
2 | Banana | 50 |
3 | Orange | 50 |
4 | Apple | 50 |
5 | Bread | 75 |
6 | Sliced Ham | 25 |
7 | Kleenex | NULL |
And the summary_data table with the following data:
prod_id | current_cat |
---|---|
1 | 10 |
2 | 10 |
3 | 10 |
4 | 10 |
5 | 10 |
8 | 10 |
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_id | current_cat |
---|---|
1 | 50 |
2 | 50 |
3 | 50 |
4 | 50 |
5 | 75 |
8 | 10 |
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
MORE NEWS
PreambleNoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...
PreambleMongoDB 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...
PreambleMS 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...
PreambleAtom 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...
PreambleMongoDB 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...
PreambleSQLShell 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...
PreambleWriting 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...
PreambleOracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...
PreambleIBM 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....
PreambleIf 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...
PreambleStarting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...