Skip to main content

Multi-Table UPDATE and DELETE Statements

MySQL allows the use of join syntax in UPDATE and DELETE statements to enable updates or deletes that involve multiple tables. They can be useful for storage engines where transactions and foreign keys are not supported, and to make sure that the tables are consistent. These statements can be used to perform the
following operations:

• Update rows in one table by transferring information from another table
• Update rows in one table, determining which rows to update by referring to another table
• Update rows in multiple tables with a single statement
• Delete rows from one table, determining which rows to delete by referring to another table
• Delete rows from multiple tables with a single statement

Some of the principles involved in writing joins in SELECT statements also apply to multiple-table UPDATE
and DELETE statements. This section provides a brief overview of their syntax.

A multiple-table UPDATE is an extension of a single-table statement:

• Following the UPDATE keyword, name the tables involved in the operation, separated by commas.
(all the tables used in the query must be named, even if all of them are not being updated.)
• In the WHERE clause, describe the conditions that determine how to match records in the tables.
• In the SET clause, assign values to the columns to be updated. These assignments can refer to columns
from any of the joined tables.

For example, this statement identifies matching records in two tables based on id values, and then copies the
name column from t2 to t1:

UPDATE t1, t2 SET t1.name = t2.name WHERE t1.id = t2.id;

Multiple-table DELETE statements can be written in two formats. The following example demonstrates one
syntax, for a query that deletes rows from a table t1 where the id values match those in a table t2:

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

The second syntax is slightly different:

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

To delete the matching records from both tables, the statements are:

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

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

Comments

Popular posts from this blog

PHP INTRODUCTION

                     PHP  (recursive acronym for  PHP: Hypertext Preprocessor ) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML. PHP stands for  P HP:  H ypertext  P reprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.) PHP is an open source software PHP is free to download and use Why PHP? PHP runs on different platforms (Windows, Linux, Unix, etc.) PHP is compatible with almost all servers used today (Apache, IIS, etc.) PHP is FREE to download from the official PHP resource:  www.php.net PHP is easy to learn and runs efficiently on the server side What can PHP do? Anything. PHP is mainly focused on server-side scripting, so you can...

Storage Engine Breakdown

The following diagram represents a simplified view of the MySQL server and its interaction with the  storage engines. The following properties are storage engine dependant: • Storage Medium – Each table uses its own method of storing the data it contains. • Transactional Capabilities – Certain storage engines handle transactional processing which ensures that integrity of a database is maintained during the processing of multiple SQL statements.

Managing Database Tables

1. Login in to the mysql client using the login name and password provided by your instructor. 2. View the existing databases that the MySQL Server is maintaining. SHOW DATABASES; 3. Utilize the photo_album database. USE photo_album; 4. Create the first table called customers that will store the information related to the customers who have purchased access to the online photo album CREATE TABLE customers ( email_customer VARCHAR(50) NOT NULL DEFAULT '', f_name VARCHAR(20) NOT NULL DEFAULT '', l_name VARCHAR(30) NOT NULL DEFAULT '', addr1 VARCHAR(30) NOT NULL DEFAULT '', addr2 VARCHAR(30) NULL, addr_city VARCHAR(25) NOT NULL DEFAULT '', addr_state CHAR(3) NOT NULL DEFAULT '', addr_zip CHAR(10) NOT NULL DEFAULT '', primary_phone CHAR(12) NOT NULL DEFAULT '', secondary_phone CHAR(12) NULL, term_date DATE NOT NULL DEFAULT 0, active_customer ENUM('True', 'False') NOT NULL DEFAULT ...