MySQL FAQs
FAQs Categories
Client Server Commands
Database Structure
Table Types or Storage Engines
Indexes
SQL Statements
Table Joins
Funtions and Operators
Tricky Select Queries
Speed Up Queries
Data Back Up
General Questions
Errors
N.M.P.L. Company www.NearMePayday.Loan || 3-month (90 days)
Powered by MySQL
 
Home / Table Joins / Question No: 12

How to delete or update records of multiple tables in one go?

How to delete or update records of multiple tables based on whether they match or don't match records with each other.

Answer No: 12

To perform a single-table DELETE, you refer only to the columns of one table and thus need not qualify the column names with the table name. For example, to delete all records in a table t that have id values greater than 100, you'd write a statement like this:
DELETE FROM t WHERE id > 100;
But what if you want to delete records based not on properties inherent in the records themselves, but rather on their relationship to records in another table? Suppose that you want to delete from t those records with id values that are found in another table t2?

To write a multiple-table DELETE, name all the tables in a FROM clause and specify the conditions used to match up records in the tables in the WHERE clause. The following statement deletes records from table t1 where there is a matching id value in table t2:
DELETE t1 FROM t1, t2 WHERE t1.id = t2.id;
Notice that the FROM clause names all the tables involved in the operation, just as when writing a join. In addition, if a column name appears in more than one of the tables, it becomes ambiguous and must be qualified with a table name something like t1.id, t2.id. This too is similar to writing a join.

The syntax also allows for deleting records from multiple tables at once. To delete rows from both tables where there are matching id values, name them both after the DELETE keyword like:
DELETE t1, t2 FROM t1, t2 WHERE t1.id = t2.id;
What if you want to delete non-matching records? A multiple-table DELETE can use any kind of join that you can write in a SELECT, so employ the same strategy that you'd use when writing a SELECT that identifies the non-matching records. That is, use a LEFT JOIN or RIGHT JOIN. For example, to identify records in t1 that have no match in t2, you'd write a SELECT like this:
SELECT t1.* FROM t1 LEFT JOIN t2 ON t1.id = t2.id 
WHERE t2.id IS NULL;
The analogous DELETE statement to find and remove those records from t1 uses a LEFT JOIN as well:
DELETE t1 FROM t1 LEFT JOIN t2 ON t1.id = t2.id 
WHERE t2.id IS NULL;
MySQL supports a second multiple-table DELETE syntax. With this syntax, use a FROM clause to indicate which tables records are to be deleted from and a USING clause to list the tables that determine which records to delete. The preceding multiple-table DELETE statements can be rewritten using this syntax as follows:
DELETE FROM t1 USING t1, t2 WHERE t1.id = t2.id;

DELETE FROM t1, t2 USING t1, t2 WHERE t1.id = t2.id;

DELETE FROM t1 USING t1 LEFT JOIN t2 ON t1.id = t2.id
WHERE t2.id IS NULL;
Multiple-table UPDATE Statements
The principles involved in writing multiple-table UPDATE statements are quite similar to those used for DELETE: Name all the tables that participate in the operation and qualify column references as necessary. Suppose that the quiz you gave on May 5, 2005 contained a question that everyone got wrong, and then you discover that the reason for this is that your answer key was incorrect. As a result, you must add a point to everyone's score. Without multiple-table UPDATE capability, you might accomplish this using two statements. First, look up the event id corresponding to the quiz for the given date:
SELECT @id := event_id FROM grade_event 
WHERE date = '2005-05-05' AND category = 'Q';
Then use the id value to identify the relevant score records:
UPDATE score SET score = score + 1 WHERE event_id = @id;
With a multiple table UPDATE, you can do the same thing with a single statement:
UPDATE score, grade_event SET score.score = score.score + 1 
WHERE score.event_id = grade_event.event_id AND
grade_event.date = '2005-05-05' AND grade_event.category = 'Q';
You can not only identify records to update based on the contents of another table, you can copy column values from one table to another. The following statement copies t1.a to t2.a for records that have a matching id column value:
UPDATE t1, t2 SET t2.a = t1.a WHERE t2.id = t1.id;
If you want to perform multiple-table deletes or updates for InnoDB tables, you need not use the syntax just described. Instead set up a foreign key relationship between tables that includes an ON DELETE CASCADE or ON UPDATE CASCADE constraint.

Related MySQL FAQs to the Above FAQ

What-is-difference-between-ON-Clause-and-WHERE-Clause What is difference between ON Clause and WHERE Clause?

How-to-get-record-from-first-table-joining-with-second-table-without-any-active-record-between-them How to get record from first table joining with second table without any active record between them?

About FAQs: Recently Added FAQs

About MySQL FAQs: Site Map | Bookmark Us | Recommend this Site to Your Friend | Contact Us

Useful Links: Wikipedia.org | Oracle.com | w3schools.com | www.php.net | Github.com

© 2021  www.mysqlfaqs.net
All rights reserved.