Delete
To delete (remove) data from a table, the DELETE
statement is used. DELETE
can be used in two ways:
To delete specific rows from a table
To delete all rows from a table
If the WHERE
clause were omitted, this statement would have deleted every customer in the table!
The DELETE
statement deletes rows from tables, even all rows from tables. But DELETE
never deletes the table itself.
Multi-Table Deletes
For example, to delete rows from bothT1
andT2
tables that meet a specified condition, you use the following statement:
Notice that you put table names
T1
andT2
between theDELETE
andFROM
keywords. If you omitT1
table, theDELETE
statement only deletes rows inT2
table. Similarly, if you omitT2
table, theDELETE
statement will delete only rows inT1
table.The expression
T1.key = T2.key
specifies the condition for matching rows betweenT1
andT2
tables that will be deleted.The condition in the
WHERE
clause determine rows in theT1
andT2
that will be deleted.
In the following code, we INNER JOINs three tables.
These two syntax return the same results. These statements use all three tables when searching for rows to delete, but delete matching rows only from tablest1
andt2
.
For the first multiple-table syntax, only matching rows from the tables listed before the FROM
clause are deleted. For the second multiple-table syntax, only matching rows from the tables listed in the FROM
clause (before theUSING
clause) are deleted. The effect is that you can delete rows from many tables at the same time and have additional tables that are used only for searching.
Last updated