
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Perform Multi-table delete in MySQL
For this, you can use DELETE command. Let us first create a table −
mysql> create table DemoTable1 ( Id int, Name varchar(20) ); Query OK, 0 rows affected (0.63 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1 values(1,'Chris'); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable1 values(2,'David'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1 values(3,'Bob'); Query OK, 1 row affected (0.20 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable1;
This will produce the following output −
+------+-------+ | Id | Name | +------+-------+ | 1 | Chris | | 2 | David | | 3 | Bob | +------+-------+ 3 rows in set (0.00 sec)
Here is the query to create second table −
mysql> create table DemoTable2 ( ClientId int, ClientName varchar(20) ); Query OK, 0 rows affected (1.19 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable2 values(101,'Mike'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable2 values(110,'Bob'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable2 values(120,'Adam'); Query OK, 1 row affected (0.60 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable2;
This will produce the following output −
+----------+------------+ | ClientId | ClientName | +----------+------------+ | 101 | Mike | | 110 | Bob | | 120 | Adam | +----------+------------+ 3 rows in set (0.00 sec)
Here is the query for multi-table delete −
mysql> delete from DemoTable1 where Name='Bob'; Query OK, 1 row affected (0.23 sec) mysql> delete from DemoTable2 where ClientName='Bob'; Query OK, 1 row affected (0.27 sec)
Let us check the first table records −
mysql> select *from DemoTable1;
This will produce the following output −
+------+-------+ | Id | Name | +------+-------+ | 1 | Chris | | 2 | David | +------+-------+ 2 rows in set (0.00 sec)
Let us check the second table records −
mysql> select *from DemoTable2;
This will produce the following output −
+----------+------------+ | ClientId | ClientName | +----------+------------+ | 101 | Mike | | 120 | Adam | +----------+------------+ 2 rows in set (0.00 sec)
- Related Articles
- Perform MySQL delete under safe mode?
- MySQL SELECT from a subquery and then perform DELETE?
- Delete all records from a table in MySQL?
- How can I delete MySQL temporary table?
- Delete multiple entries from a MySQL table
- Delete only specific rows in a table with MySQL
- MySQL query to perform delete operation where id is the biggest?
- Delete all the records from a MySQL table?
- How to delete a column from a table in MySQL?
- Can we perform MySQL UPDATE and change nothing in a table?
- How to delete all the duplicate records in a MySQL table?
- MySQL: delete all rows containing string “foo” in sample table “bar”?
- Delete records from a MySQL table with IN() in a single query
- How can we delete multiple rows from a MySQL table?
- How can we delete all rows from a MySQL table?

Advertisements