Results 1 to 8 of 8

Thread: delete Parent - Child Records in Vb 2008

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    68

    delete Parent - Child Records in Vb 2008

    I am using vb2088 Express edition and I am trying to figure out how to delete a parent record from one table with child records from another table. Right now if I try to execute a delete of the parent records an error occur saying that it's violate the foreign key constraints. Can anyone give me a hand with this?

  2. #2
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    Re: delete Parent - Child Records in Vb 2008

    Right,

    The way you can resolve your error is by deleting all records simultaneusly and that can be accomplished using T-SQL.

    You didnt indicate which database you are using. If its Access I dont think this can be done but if its SQL Server it can.

    Have a look at this site it specifies exactly your problem

    Hope it helps
    ------------------------------------------------------------------------
    If an answer to your question has been helpful, then please, Rate it!

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: delete Parent - Child Records in Vb 2008

    You have two choices:

    1. Configure the foreign key relationship to cascade deletions.
    2. Manually delete all child records before deleting a parent.

    The first option is easier. Once it's done you just go ahead and delete the parent record and all the child records will be automatically deleted. Alternatively you can do as angelica suggests.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: delete Parent - Child Records in Vb 2008

    Pesonnaly as a DBA I always agrue against the first optiom. I always recommend doing the deletes required by code.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: delete Parent - Child Records in Vb 2008

    Quote Originally Posted by GaryMazzone
    Pesonnaly as a DBA I always agrue against the first optiom. I always recommend doing the deletes required by code.
    I certainly have heard that sentiment and, in fact, nearly mentioned it in my previous post. I'd be interested to hear the specific rationale behind it. I figure that if you're just going to write the code to delete all the child records with a specific parent ID anyway then you may as well just let the relationship do it for you. That said, there are plenty of things that I do a particular way, and recommend to others to do the same way, because it's "the right way" to do it. This may well be one of those, or there may be implications I haven't considered. I'd be interested to hear an explanation from someone who knows.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: delete Parent - Child Records in Vb 2008

    I have worked many positions where delete of data was almost always forbiden (gov (military) conctract, financial, Nuclear power plants). If a delete is to be performed on parent data we always require a reponse in the affirmitave that the delete is required. If the answer is yes we would code the delete of all required tables in stored procs or in the code itself. If someone did a delete on the database the keys will keep the delete from happening by accident.

    In fact more often then not the delete was not a phsyical delete but a locgical delete only (the record had a field to mark it as logically deleted).
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    68

    Re: delete Parent - Child Records in Vb 2008

    Thank you everyone for your input. I am using a Ms SQL DB. I am just writing a simple POS system. jmcilhinney, for option 1 "Configure the foreign key relationship to cascade deletions" can you tell me how I do that is there a sample code I can look at? Thank you again.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: delete Parent - Child Records in Vb 2008

    Quote Originally Posted by ronan9715
    Thank you everyone for your input. I am using a Ms SQL DB. I am just writing a simple POS system. jmcilhinney, for option 1 "Configure the foreign key relationship to cascade deletions" can you tell me how I do that is there a sample code I can look at? Thank you again.
    If you do want to take that route then you'd normally do it in SQL Server Management Studio. You could execute the appropriate SQL code but I'd generally do it visually. Open the table in the designer, open the relations dialogue, select the relation of interest and configure it to cascade deletes.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width