Skip to content

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.

UPDATE table_source [ , … ]
SET [ table_alias. ] column_name = expression [ , … ]
[ WHERE condition ]
[ WITH OWNERACCESS OPTION ]

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.

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”.

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.

Which rows to change. Without a WHERE, every row changes. The condition is any expression a SELECT’s WHERE takes, subqueries included.

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.

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 NULL column set to Null fails.
  • A duplicate key in a primary key or unique index fails.
  • A table-level CHECK constraint 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.

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 = 3

Update through a join, changing only the products:

UPDATE Products INNER JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID
SET Products.Discontinued = True
WHERE Suppliers.Country = 'Norway'

The same kind of update with a comma list and aliases:

UPDATE Products AS p, Categories AS c
SET p.ReorderLevel = 50
WHERE p.CategoryID = c.CategoryID AND c.CategoryName = 'Seafood'

This fails, because Northwind has no customer ZZZZZ:

UPDATE Orders SET CustomerID = 'ZZZZZ' WHERE OrderID = 10248

Cascade 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'.