SQL delete simultaneously deletes records associated with multiple tables

Source: Internet
Author: User

SQL delete simultaneously deletes records associated with multiple tables

Sqlserver supports cascade update and deletion.
Oracle only supports cascading Deletion


Delete a row that contains the primary key value. This value is referenced by a foreign key column in an existing row of another table. In cascading deletion, all rows whose foreign key value references the deleted primary key value are also deleted.

For example:

Create database temp
Go
Use temp
Go

Create table UserInfo
(
UserId int identity (1, 1) primary key,
UserName varchar (20), -- User Name
Password varchar (20) not null -- password
)

Create table UserDetails
(
Id int identity (1, 1) primary key,
Name varchar (50) not null, -- real name
UserId int,
Foreign key (userId) references UserInfo (UserId) on delete cascade
)

Insert UserInfo values ('ly ', 'jeff ')
Insert UserInfo values ('wzq', 'wzqwzq ')
Insert UserInfo values ('lg ', 'lglg ')
 
Insert UserDetails values ('Li si', 1)
Insert UserDetails values ('wang wu', 2)
Insert UserDetails values ('Liu liu', 3)

 


Alter table name
Add constraint foreign key name
Foreign key (field name) references main table name (field name)
On delete cascade

Syntax:
Foreign Key
(Column [,... n])
References referenced_table_name [(ref_column [,... n])]
[On delete cascade]
[On update cascade]
Note:
Column: column name
Referenced_table_name: name of the primary key table referenced by the foreign key
Ref_name: primary key column of the Table to be referenced by the foreign key
On delete: deletes a cascading.
On update: update Cascade

000
(Please comment on the article.) Delete From UserInfo Where UserId = 1 to Delete the UserId = 1 in the UserInfo and UserDetails tables.


Let's take a look at how to delete multiple tables in the MySql database tutorial,

Sometimes we can use delete directly to delete


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 ';

Related Article

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.