Delete
To delete (remove) data from a table, the DELETE statement is used. DELETEcan be used in two ways:
To delete specific rows from a table
To delete all rows from a table
DELETE FROM table_name
WHERE condition;DELETE FROM Customers
WHERE cust_id = "1000000006";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:
DELETE T1, T2
FROM T1 INNER JOIN T2 ON T1.key = T2.key
WHERE condition;Notice that you put table names
T1andT2between theDELETEandFROMkeywords. If you omitT1table, theDELETEstatement only deletes rows inT2table. Similarly, if you omitT2table, theDELETEstatement will delete only rows inT1table.The expression
T1.key = T2.keyspecifies the condition for matching rows betweenT1andT2tables that will be deleted.The condition in the
WHEREclause determine rows in theT1andT2that 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 tablest1andt2.
For the first multiple-table syntax, only matching rows from the tables listed before the FROMclause are deleted. For the second multiple-table syntax, only matching rows from the tables listed in the FROMclause (before theUSINGclause) 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
Was this helpful?