DELETE
Removes the rows of a table that meet a condition.
Access SQL. ACE accepts this statement as written here, joins and comma lists of tables included.
Syntax
Section titled “Syntax”DELETE [ * | table_name.* [ , … ] | column_name [ , … ] ] FROM table_source [ , … ] [ WHERE condition ] [ WITH OWNERACCESS OPTION ]Arguments
Section titled “Arguments”*
Delete rows of the one table in FROM. This is the same as naming no target.
table_name.*
Section titled “table_name.*”The table whose rows are deleted, when FROM names several tables. The other tables only decide which rows
match.
column_name
Section titled “column_name”A column, as the Access query designer writes the statement (DELETE Shippers.Phone FROM Shippers …). A column
names no part of the row: the whole row is deleted, exactly as with *. Over a join a column does not say which
table to delete from, so it is refused.
table_source
Section titled “table_source”The table to delete from, or a join of tables, written as in a FROM clause. A comma list of sources is accepted
too, as in a FROM clause, and the WHERE then relates them.
WHERE condition
Section titled “WHERE condition”Which rows to delete. Without a WHERE, every row of the table is deleted. 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”DELETE always removes whole rows. To clear a column’s value, use UPDATE and set it to Null.
When FROM names more than one table, the target must be table_name.*. With no target, with * or with a
column, the statement is ambiguous and fails, as it does in ACE, which asks you to specify the table containing
the records you want to delete.
The statement reports the number of rows it deleted, which SELECT @@ROWCOUNT also returns afterwards. See
System variables.
Referential integrity is enforced. Deleting a parent row that child rows refer to fails, unless the relationship has a delete rule:
ON DELETE CASCADEdeletes the child rows too.ON DELETE SET NULLsets the child rows’ foreign key to Null.
A cascaded delete applies the child table’s own relationships too, so it carries on down a chain. Rows deleted by a cascade are not counted in the statement’s row count.
Examples
Section titled “Examples”These run against the Northwind sample database, as in the playground.
Delete the order lines with a quantity under 3:
DELETE * FROM [Order Details] WHERE Quantity < 3Delete through a join. Only [Order Details] rows go; Products only decides which:
DELETE [Order Details].*FROM [Order Details] INNER JOIN Products ON [Order Details].ProductID = Products.ProductIDWHERE Products.ProductName = 'Chai'The same with a comma list:
DELETE [Order Details].*FROM [Order Details], ProductsWHERE [Order Details].ProductID = Products.ProductID AND Products.ProductName = 'Chang'This fails, because the join leaves it unclear which table to delete from:
DELETE FROM [Order Details] INNER JOIN Products ON [Order Details].ProductID = Products.ProductIDWHERE Products.ProductName = 'Chang'This fails too, because the customer has orders:
DELETE FROM Customers WHERE CustomerID = 'ALFKI'Cascade a delete to the child rows. These follow on from each other:
CREATE TABLE Depts (Code TEXT(10) PRIMARY KEY, Name TEXT(50))CREATE TABLE Staff (Id LONG PRIMARY KEY, Dept TEXT(10) REFERENCES Depts (Code) ON DELETE CASCADE)INSERT INTO Depts VALUES ('SALES', 'Sales'), ('OPS', 'Operations')INSERT INTO Staff VALUES (1, 'SALES'), (2, 'SALES'), (3, 'OPS')DELETE FROM Depts WHERE Code = 'SALES'Staff 1 and 2 are deleted with the department. The statement reports 1 row.