Solved

delete from two tables based on FK

Posted on 2010-11-15
11
452 Views
Last Modified: 2012-05-10
I have two tables, tblOne (firstid (PK), secondid (FK)
tblTwo (secondid (PK), name, address)

I'm looking to create a DELETE statement that will delete from tblOne where firstid = @firstid, but then subsequently DELETE the related record from tblTwo based on the tblOne.secondid value of the deleted record. Can this be done in one statement?
0
Comment
Question by:alright
  • 5
  • 4
  • 2
11 Comments
 
LVL 6

Expert Comment

by:AliSyed
ID: 34138775
You can set delete cascase and and it will be done automatically.
0
 
LVL 6

Expert Comment

by:AliSyed
ID: 34138792
DELETE CASCADE
look at this
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34138801
Not in one statement but can be done in the same batch in a transaction. Cascaded deletes in the other way, is not what you are after
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34138837
Is it always one to one? Otherwise the delete from tbl2 may fail due to other records using that id.
0
 

Author Comment

by:alright
ID: 34138857
Yes, always one to one
0
 

Author Comment

by:alright
ID: 34138865
You say that CASCADE delete goes the other way, ie del tblTwo record first and then the associated tblOne record? If so, I think that would be okay
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34138926
Then you can set up the foreign key to be the "master" of the relationship and cascade deletes, updates. Even then one statement may not be enough, if you have records in tblOne that have no secondid link.

alter table tblOne add constraint FK_onetwo foreign key
REFERENCES tblTwo(secondid) ON DELETE CASCADE ON UPDATE CASCADE;

Your statement:

Delete tblTwo where secondid in (select secondid from tblOne where <some condition>)

This deletes from tblTwo and will grab all the records from tblOne that link by FK, however, if there are records in tblOne that are not linked to tblTwo, you will need to fire a secondary delete

delete tblOne where <some condition>

So it may or may not be better than just managing the deletes in a batch transaction.
0
 

Author Comment

by:alright
ID: 34138929
BEGIN TRAN
SET @secondid = tblOne.secondid WHERE firstid = @firstid
DELETE FROM [tblOne] WHERE [firstid] = @firstid
DELETE FROM [tblTwo] WHERE [secondid] = @secondid
If @@error = 0
COMMIT TRAN

ELSE
ROLLBACK


Incorrect syntax near the keyword 'WHERE'
0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 125 total points
ID: 34138970
BEGIN TRAN
select @secondid = secondid from tblOne where firstid = @firstid
DELETE FROM tblTwo where secondid = @secondid
DELETE FROM [tblOne] WHERE [firstid] = @firstid
If @@error = 0
      COMMIT TRAN
ELSE
      ROLLBACK
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34138976
That is if you are always deleting just one record, I thought you wanted something more general like batch deletes.
0
 

Author Closing Comment

by:alright
ID: 34139040
Oh yes, always deleting just one record. Working well now, thank you sir
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
CSV How to add columns based on existing column(s)? 20 32
Very Large data in MYSQL 7 74
Perl Versus AWK? 7 52
sql server query 12 26
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question