Update the rows of the table.
[ WITH [ RECURSIVE ] with_query [, ...] ]
UPDATE [ONLY] table [[AS] alias]
SET {column = {expression | DEFAULT} |
(column [, ...]) = ({expression | DEFAULT} [, ...])} [, ...]
[FROM fromlist]
[WHERE condition| WHERE CURRENT OF cursor_name ]
UPDATE changes the value of the specified column in all rows that meet the criteria. Simply mention the column you want to modify in the SET clause; a column that is not explicitly modified will retain its previous value.
By default, UPDATE will update rows in the specified table and all its subtables. If you only want to update the specific table mentioned, you must use the ONLY clause.
There are two ways to modify a table using the information contained in other tables in the database: use subselect, or specify another table in the FROM clause. Which technology is more suitable depends on the specific situation.
If the WHERE CURRENT OF clause is specified, the updated row is the latest row obtained from the specified cursor.
The WHERE CURRENT OF clause is not supported in the copy table.
You must have UPDATE privileges on the table or at least on the columns you want to update. You must also have SELECT privileges to read any columns in expression or condition.
Note: By default, the YMatrix database acquires the EXCLUSIVE lock of the table for the UPDATE operation of the heap table. After the global deadlock detector is enabled, the lock mode of the UPDATE operation on the heap table is ROW EXCLUSIVE.
After successful completion, the UPDATE command will return the command mark in the following format:
UPDATE count
where count is the number of updated rows. If count is 0, there are no rows that meet the criteria (this is not considered an error).
with_query
ONLY
table
alias
column
Expression
DEFAULT
fromlist
condition
cursor_name
output_expression
output_name
SET is not allowed on the YMatrix distribution key column of the table.
When an FROM clause exists, it is essentially connecting the target table to the table mentioned in the from list, and each output row of the join represents the update operation of the target table. When using FROM, make sure that the connection produces at most one output line for each line to be modified. In other words, the target row should not be joined to a row in another table. If so, then only one of the join rows will be used to update the target row, but it will be difficult to predict which row will be used.
Because of this uncertainty, it is safer to refer to other tables only within the subselect, although it is generally harder to read and slower than using a join.
Direct execution of UPDATE and DELETE commands on specific partitions (subtables) of partitioned tables is not supported. Instead, execute these commands on the root partition table (the table created using the CREATE TABLE command).
Change the kind column in table films from Drama to Dramatic:
UPDATE films SET kind = 'Dramatic' WHERE kind = 'Drama';
Adjust the temperature entry and reset the precipitation in a row of the table weather to the default value:
UPDATE weather SET temp_lo = temp_lo+1, temp_hi =
temp_lo+15, prcp = DEFAULT
WHERE city = 'San Francisco' AND date = '2016-07-03';
Use the alternative column list syntax to make the same update:
UPDATE weather SET (temp_lo, temp_hi, prcp) = (temp_lo+1,
temp_lo+15, DEFAULT)
WHERE city = 'San Francisco' AND date = '2016-07-03';
Use the FROM clause syntax to increase the sales number of salespeople who manage Acme Corporation accounts (assuming that both connected tables are distributed based on id columns in the YMatrix database):
UPDATE employees SET sales_count = sales_count + 1 FROM
accounts
WHERE accounts.name = 'Acme Corporation'
AND employees.id = accounts.id;
Use subselects in the WHERE clause to perform the same operation:
UPDATE employees SET sales_count = sales_count + 1 WHERE id =
(SELECT id FROM accounts WHERE name = 'Acme Corporation');
Try inserting new stock items and stock quantity. If the material already exists, update the inventory quantity of existing materials. To do this without failing the entire transaction, use a savepoint.
BEGIN;
-- Other operations
SAVEPOINT sp1;
INSERT INTO wines VALUES('Chateau Lafite 2003', '24');
-- Assume the above fails because of a unique key violation,
-- so now we issue these commands:
ROLLBACK TO sp1;
UPDATE wines SET stock = stock + 24 WHERE winename = 'Chateau
Lafite 2003';
-- Continue with other operations, and eventually
COMMIT;
This command complies with SQL standards, but the FROM clause is a YMatrix database extension.
According to the criteria, the column list syntax should allow the allocation of column lists from a single row value expression (such as sub-select):
UPDATE accounts SET (contact_last_name, contact_first_name) =
(SELECT last_name, first_name FROM salesmen
WHERE salesmen.id = accounts.sales_id);
Not currently implemented - the source must be a list of independent expressions.
Some other database systems provide FROM options, and the target table should be listed again in the FROM. That's not how the YMatrix database interprets FROMs. Be careful when migrating applications with this extension.