Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7542957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:18:13+00:00 2026-05-30T08:18:13+00:00

I have a table ParentTable in which I have parentId as primary key. Now

  • 0

I have a table ParentTable in which I have parentId as primary key.

Now I have three child tables ChildTable1, ChildTable2 and ChildTable3.

  • in ChildTable1, primary key is ch1Id and foreign key is fkparentId (pk from ParentTable)

  • in ChildTable2, primary key is ch2Id and foreign keys are fkparentId and fkch1Id (primary key from ChildTable1)

  • in ChildTable3, primary key is ch3Id and foreign keys are fkparentId, fkch1Id and fkch2Id (primary key from ChildTable2).

And this goes on..

I want to write a query to delete these tables hierarchially in the following manner.

First delete ChildTable3 > then delete ChildTable2 > then delete ChildTable1 and finally delete ParentTable.

I have a vague idea of doing this like

delete from ChildTable3
where fkch2Id = ch2.ch2Id
and fkch1Id = ch1.ch1Id
and fkparentId = p.parentId

then proceeding to ChildTable2 for deletion and so on. Hope I made it clear.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:18:15+00:00Added an answer on May 30, 2026 at 8:18 am

    You can do same thing without any query by setting cascade on delete on foreign key definitin.

    if you don’t set foreign key definiton on db you can delete records on fkparentId like this.

    declare @parentId int = 3
    
    delete from ChildTable3 where fkparentId = @parentId;
    delete from ChildTable2 where fkparentId = @parentId;
    delete from ChildTable1 where fkparentId = @parentId;
    delete from ParentTable where parentId = @parentId;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple parent/child tables. Contract is the parent table. Each contract can
I have table with a unique auto-incremental primary key. Over time, entries may be
I have two parent tables, BusinessGroup and SocialGroup, and one child table, Members. A
I have a Parent/Child Table relationship with the ChildTable linking back to the ParentTable
I have a table Parent and a table Child. Child contains a foreign-key to
I have 2 tables (say parent and child table and 1 to many relation),
I have 3 tables ParentTable ChildTable (with column ParentId refering to ParentTable ) Recording
I have a parent table called 'Website' which holds records about websites. I have
If I have a parent table and a child table where there can be
I have few tables in the database with parent table say P is having

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.