Results 1 to 5 of 5

Thread: Deleting records that have relationships.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Posts
    27

    Deleting records that have relationships.

    Hello

    Can you tell me the best way to delete a record that is related to another table. For instance, I have a customer and invoice table (Customer ID is the Primary Key in Customer and the Foreign key in invoice).

    I wish to delete the Customer in the Customer table but not all of its related invoices in the Invoice table.

    I am using VB6 front end with an Access database.

    Thanks

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Deleting records that have relationships.

    How about simply flagging that customer as Not Active.

    If you delete the customer, to what are all of their invoices now related?

  3. #3
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Deleting records that have relationships.

    As a principle, I never want to delete records unless I really have to. As an alternative I always add a Deleted flag (a bit or boolean) to each table. Then when I need to 'delete' a record, I just set that flag to TRUE.
    Then, all queries to the tables include a line in the WHERE clause that only returns records where Deleted=FALSE.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

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

    Re: Deleting records that have relationships.

    Hack and DKenny are aboslutely correct. If you have the tables related using a FK relationship (Parent-Child) then you can not have a child with out a parent (the Old relationial rules rear there head here). You can mark a customer as deleted using a feild as DKenny suggests, my only point to that would be not to use a Boolean (Yes/No) data type (if you are using MS Access). If you move to larger type RDBMS they don't all support True/False datatypes (Oracle as an example), I normmaly use a Text type of 1 charactor (stores a null (nothing) or a Y for deleted).
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Posts
    27

    Re: Deleting records that have relationships.

    Thanks Hack, DKenny and Mazz1.

    Your comments are very helpful.

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