Delete statement based on table joins : Delete « Insert Update Delete « MySQL Tutorial

MySQL Tutorial
1. Introduction
2. Select Query
3. Database
4. Table
5. Table Join
6. Subquery
7. Insert Update Delete
8. Logic Operator
9. View
10. Data Types
11. Procedure Function
12. Cursor
13. Trigger
14. Date Time Functions
15. Comparison Functions Operators
16. Aggregate Functions
17. Cast Functions Operators
18. Control Flow Functions
19. Encryption Compression Functions
20. Information Functions
21. Math Numeric Functions
22. Miscellaneous Functions
23. String Functions
24. Regular Expressions
25. Data Dictionary
26. MySQL Utilities
27. Privilege
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
MySQL Tutorial » Insert Update Delete » Delete 
7. 3. 4. Delete statement based on table joins
mysql>
mysql>
mysql> CREATE TABLE Books(
    ->    BookID SMALLINT NOT NULL PRIMARY KEY,
    ->    BookName VARCHAR(40NOT NULL,
    ->    InStock SMALLINT NOT NULL
    -> )
    -> ENGINE=INNODB;
Query OK, rows affected (0.05 sec)

mysql>
mysql> CREATE TABLE Orders(
    ->    OrderID SMALLINT NOT NULL PRIMARY KEY,
    ->    BookID SMALLINT NOT NULL,
    ->    Quantity TINYINT (40NOT NULL DEFAULT 1,
    ->    DateOrdered TIMESTAMP,
    ->    FOREIGN KEY (BookIDREFERENCES Books (BookID)
    -> )
    -> ENGINE=INNODB;
Query OK, rows affected (0.05 sec)

mysql>
mysql>
mysql> INSERT INTO Books VALUES (101'Java', 12),
    ->                          (102'PHP', 17),
    ->                          (103'MySQL', 23),
    ->                          (104'Perl', 32),
    ->                          (105'Pyton', 6),
    ->                          (106'www.java2s.com', 28);
Query OK, rows affected (0.03 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql>
mysql> INSERT INTO Orders VALUES (10011031'2001-11-12 12:30:00'),
    ->                           (10021011'2002-10-16 12:31:00'),
    ->                           (10031032'2003-02-11 12:34:00'),
    ->                           (10041043'2004-01-19 12:36:00'),
    ->                           (10051021'2005-12-17 12:41:00'),
    ->                           (10061032'2006-10-18 12:59:00'),
    ->                           (10071011'2004-11-21 13:01:00'),
    ->                           (10081031'2014-10-16 13:02:00'),
    ->                           (10091024'1994-09-02 13:22:00'),
    ->                           (10101012'1995-10-04 13:30:00'),
    ->                           (10111031'1996-08-12 13:32:00'),
    ->                           (10121051'2004-10-03 13:40:00'),
    ->                           (10131062'2002-05-12 13:44:00'),
    ->                           (10141031'2001-10-01 14:01:00'),
    ->                           (10151061'1997-05-05 14:05:00'),
    ->                           (10161042'1998-10-07 14:28:00'),
    ->                           (10171051'2004-03-12 14:31:00'),
    ->                           (10181021'2004-10-21 14:32:00'),
    ->                           (10191063'1991-01-30 14:49:00'),
    ->                           (10201031'1990-10-12 14:51:00');
Query OK, 20 rows affected (0.05 sec)
Records: 20  Duplicates: 0  Warnings: 0

mysql>
mysql> select from Books;
+--------+----------------+---------+
| BookID | BookName       | InStock |
+--------+----------------+---------+
|    101 | Java           |      12 |
|    102 | PHP            |      17 |
|    103 | MySQL          |      23 |
|    104 | Perl           |      32 |
|    105 | Pyton          |       |
|    106 | www.java2s.com |      28 |
+--------+----------------+---------+
rows in set (0.00 sec)

mysql>
mysql> select from Orders;
+---------+--------+----------+---------------------+
| OrderID | BookID | Quantity | DateOrdered         |
+---------+--------+----------+---------------------+
|    1001 |    103 |        2001-11-12 12:30:00 |
|    1002 |    101 |        2002-10-16 12:31:00 |
|    1003 |    103 |        2003-02-11 12:34:00 |
|    1004 |    104 |        2004-01-19 12:36:00 |
|    1005 |    102 |        2005-12-17 12:41:00 |
|    1006 |    103 |        2006-10-18 12:59:00 |
|    1007 |    101 |        2004-11-21 13:01:00 |
|    1008 |    103 |        2014-10-16 13:02:00 |
|    1009 |    102 |        1994-09-02 13:22:00 |
|    1010 |    101 |        1995-10-04 13:30:00 |
|    1011 |    103 |        1996-08-12 13:32:00 |
|    1012 |    105 |        2004-10-03 13:40:00 |
|    1013 |    106 |        2002-05-12 13:44:00 |
|    1014 |    103 |        2001-10-01 14:01:00 |
|    1015 |    106 |        1997-05-05 14:05:00 |
|    1016 |    104 |        1998-10-07 14:28:00 |
|    1017 |    105 |        2004-03-12 14:31:00 |
|    1018 |    102 |        2004-10-21 14:32:00 |
|    1019 |    106 |        1991-01-30 14:49:00 |
|    1020 |    103 |        1990-10-12 14:51:00 |
+---------+--------+----------+---------------------+
20 rows in set (0.00 sec)

mysql>
mysql>
mysql> DELETE FROM Orders
    -> USING Books, Orders
    -> WHERE Books.BookID=Orders.BookID
    ->    AND Books.BookName='MySQL';
Query OK, rows affected (0.01 sec)

mysql>
mysql> select from Books;
+--------+----------------+---------+
| BookID | BookName       | InStock |
+--------+----------------+---------+
|    101 | Java           |      12 |
|    102 | PHP            |      17 |
|    103 | MySQL          |      23 |
|    104 | Perl           |      32 |
|    105 | Pyton          |       |
|    106 | www.java2s.com |      28 |
+--------+----------------+---------+
rows in set (0.00 sec)

mysql>
mysql> select from Orders;
+---------+--------+----------+---------------------+
| OrderID | BookID | Quantity | DateOrdered         |
+---------+--------+----------+---------------------+
|    1002 |    101 |        2002-10-16 12:31:00 |
|    1004 |    104 |        2004-01-19 12:36:00 |
|    1005 |    102 |        2005-12-17 12:41:00 |
|    1007 |    101 |        2004-11-21 13:01:00 |
|    1009 |    102 |        1994-09-02 13:22:00 |
|    1010 |    101 |        1995-10-04 13:30:00 |
|    1012 |    105 |        2004-10-03 13:40:00 |
|    1013 |    106 |        2002-05-12 13:44:00 |
|    1015 |    106 |        1997-05-05 14:05:00 |
|    1016 |    104 |        1998-10-07 14:28:00 |
|    1017 |    105 |        2004-03-12 14:31:00 |
|    1018 |    102 |        2004-10-21 14:32:00 |
|    1019 |    106 |        1991-01-30 14:49:00 |
+---------+--------+----------+---------------------+
13 rows in set (0.00 sec)

mysql>
mysql>
mysql>
mysql> drop table Orders;
Query OK, rows affected (0.02 sec)

mysql> drop table Books;
Query OK, rows affected (0.05 sec)

mysql>
mysql>
7. 3. Delete
7. 3. 1. The DELETE statement
7. 3. 2. Using the LIMIT keyword to ensure only the number of rows you specify are deleted
7. 3. 3. Without the WHERE clause, all records from the database are deleted by default
7. 3. 4. Delete statement based on table joins
7. 3. 5. Delete statement based on table joins (tableName.*)
7. 3. 6. Delete on row with LOW_PRIORITY
w_w_w___.j_a___va_2___s___._c_om__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.