Delete joined data from multiple tables

Source: Internet
Author: User
Tags mysql tutorial
Delete joined data from multiple tables
[14:08:00 | by: recalling the death of the year of Hangzhou]
 

Ref: http://blog.csdn.net/duming115/archive/2008/04/15/2293714.aspx

The content below 2008.04.15 is only for MySQL Databases

1. the SQL statement deletes two or more associated data.

In the past, only simple operations were performed on the data in the database. Generally, only the data in a table is deleted (because the association between the table and the table is poor and there is no foreign key constraint), just like this.

Delete
From tablea
Where
1 = 1
And a. ID = 1;

After the delete operation, you do not need to specify the table name until the data in the two tables are recently deleted. Table A and table B and the content in Table B are affiliated with table, that is to say, if a record in Table A is deleted, the record in Table B is also deleted, because if the record in Table B is not deleted, junk data is formed,

An item in Table A is Itemid, and a column in table B is also Itemid. It is directed to Table A in the form of a foreign key.

When I write it like this, an error is reported:

Delete
From
A,
B
Where
1 = 1
And a. Itemid = B. Itemid
And a. ID <5

Prompt that the statement after where has a problem (of course the problem is not the where statement). When the table after from involves multiple, this statement will change a bit.

This way
Delete
A, B
From
A A, B
Where
1 = 1
And a. Itemid = B. Itemid
And a. ID <5

Or
Delete
From
A, B
Using a A, B
Where
1 = 1
And a. Itemid = B. Itemid
And a. ID <5

The table names in from do not have sequential requirements. The following is a description in <SAMs-mysql tutorial (2003). chm>:

Delete Syntax: Delete [low_priority] [quick] From table_name
[Where where_definition]
[Order by...]
[Limit rows]

Or

Delete [low_priority] [quick] table_name [. *] [, table_name [. *]...]
From table-References
[Where where_definition]

Or

Delete [low_priority] [quick]
From table_name [. *] [, table_name [. *]...]
Using table-References
[Where where_definition]

The sample code is as follows:

1. delete data from a table
Delete from department where name = 'asset management ';

2. delete data from two tables
Delete employee, employeeskills
From employee, employeeskills, Department
Where employee. employeeid = employeeskills. employeeid
And employee. interval mentid = Department. interval mentid
And department. Name = 'finance ';

3. Delete the data in the two tables and use the using syntax.
Delete from employee, employeeskills
Using Employee, employeeskills, Department
Where employee. employeeid = employeeskills. employeeid
And employee. interval mentid = Department. interval mentid
And department. Name = 'finance ';

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.