UPDATE
Changes column values in the rows of a table, or of tables joined together, that meet a condition.
Access SQL. ACE accepts this statement as written here, joins and comma lists of tables included.
Syntax
Section titled “Syntax”UPDATE table_source [ , … ] SET [ table_alias. ] column_name = expression [ , … ] [ WHERE condition ] [ WITH OWNERACCESS OPTION ]Arguments
Section titled “Arguments”table_source
Section titled “table_source”The table to update, with an optional alias, or a join of tables, written as in a FROM clause:
Products INNER JOIN Suppliers ON …. A comma list of sources is accepted too, as in a FROM clause, and the
WHERE then relates them.
[ table_alias. ] column_name
Section titled “[ table_alias. ] column_name”The column to change. When the statement names several tables, qualify the column with its table or alias to say which table’s rows change. Each column may be set only once in a statement; setting one twice fails with “Duplicate output destination”.
expression
Section titled “expression”The new value. It may refer to the row’s columns, from any of the joined tables. Every SET reads the row as it
was before the statement changed it, so SET A = B, B = A swaps the two values.
WHERE condition
Section titled “WHERE condition”Which rows to change. Without a WHERE, every row changes. The condition is any expression a SELECT’s WHERE
takes, subqueries included.
WITH OWNERACCESS OPTION
Section titled “WITH OWNERACCESS OPTION”Runs the statement with its owner’s permissions. LibRed has no users to act for, so it changes nothing, but a stored query keeps it.
Remarks
Section titled “Remarks”The statement reports the number of rows it changed, which SELECT @@ROWCOUNT also returns afterwards. See
System variables.
Over a join, a target row that several joined rows match is updated once for each match, and each update reads
what the one before it left. SET a.X = a.X + 1 over two matches adds 2, as it does in ACE.
A SET on the optional side of an outer join, for a joined row with no match on that side, adds a new row to that
table holding the SET values and the table’s defaults, as ACE does.
The new values are checked as an INSERT’s are, and a row that fails a check leaves the statement with no
effect:
- A
NOT NULLcolumn set to Null fails. - A duplicate key in a primary key or unique index fails.
- A table-level
CHECKconstraint is enforced. - Referential integrity: changing a foreign key to a value with no parent row fails. Changing a parent’s key
while child rows refer to it fails, unless the relationship has
ON UPDATE CASCADE, which changes the child rows’ keys to match. A cascaded change applies the child table’s own relationships too, so it carries on down a chain.
ON UPDATE SET NULL is not supported yet.
Examples
Section titled “Examples”These run against the Northwind sample database, as in the playground.
Raise two prices by 10%:
UPDATE Products SET UnitPrice = UnitPrice * 1.1 WHERE ProductID IN (1, 2)Set two columns, one from its current value:
UPDATE Shippers SET Phone = '(503) 555-0000', CompanyName = CompanyName & ' Ltd'WHERE ShipperID = 3Update through a join, changing only the products:
UPDATE Products INNER JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierIDSET Products.Discontinued = TrueWHERE Suppliers.Country = 'Norway'The same kind of update with a comma list and aliases:
UPDATE Products AS p, Categories AS cSET p.ReorderLevel = 50WHERE p.CategoryID = c.CategoryID AND c.CategoryName = 'Seafood'This fails, because Northwind has no customer ZZZZZ:
UPDATE Orders SET CustomerID = 'ZZZZZ' WHERE OrderID = 10248Cascade a key change to the child rows. These follow on from each other:
CREATE TABLE Teams (Code TEXT(10) PRIMARY KEY, Name TEXT(50))CREATE TABLE Players (Id LONG PRIMARY KEY, Team TEXT(10) REFERENCES Teams (Code) ON UPDATE CASCADE ON DELETE CASCADE)INSERT INTO Teams VALUES ('RED', 'Red team'), ('BLU', 'Blue team')INSERT INTO Players VALUES (1, 'RED'), (2, 'RED'), (3, 'BLU')UPDATE Teams SET Code = 'CRIMSON' WHERE Code = 'RED'Players 1 and 2 now have Team = 'CRIMSON'.