How to Delete Records From Two Tables That Have Referential Integrity

I have two tables, tableA and tableB, with data in them. I need to know how to delete all of the records from these two tables with a query that has to take into account a column that is within tableA and tableB called tableA.record = tableB.record where tableA has a one to many relationship with TableB. How would one go about writing a Mircosoft SQL Server 2005 query to delete all of the data that is within both of the tables at the same time, taking into account the referential integrity?
thenthorn1010Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ephraim WangoyaCommented:


You cant delete records from two tables in parallel, one has to go first

Delete the records from the referenced table first
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
itsdivyaksCommented:
if the tables have properly set foreign key relationship, then when you click on the foreign key relationship under the keys for the table and you can set the property

DELETE RULE to CASCASE under "Insert and Update Specification" so that when you delete a parent record, then the child records get deleted.

so you can just write the query to delete the parent records and this in turn deletes child records automatically.
0
thenthorn1010Author Commented:
Thanks for the idea. I was trying the SQL "Delete tableA, tableB" and I was receiving SQL errors. I took your comment and created the two lines of SQL code:

"Delete tableA
Delete tableB"

This solved the problem. Thanks again for the help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.