+ Reply to Thread
Results 1 to 10 of 10

Thread: Dealing with deleting in Parent/Child Tbale relationship

  1. #1
    Join Date
    Oct 2005
    Location
    Scotland
    Posts
    220
    Thanks
    6
    Thanked 12 Times in 8 Posts

    Default Dealing with deleting in Parent/Child Tbale relationship

    Just wondering if anyone had a good way of dealing with deleting a record in a parent child relationship in a database.

    For example say you had

    tblCars
    CarID
    Manufacturer
    Model
    ColourID

    tblCarColour
    ColourID
    ColourName
    RBG

    If say the manufacturer stopped making a certain colour so you want to remove the colour so you can't order the car in that colour now. How would you deal with that?

    If you deleted it obviously the historical data would be messed up as some orders would probably contain an order for a car in that colour.

    Just interested to see if anyone had a good way of dealing with this sort of problem.

  2. #2
    Join Date
    Apr 2005
    Location
    Haslemere, Surrey, UK
    Posts
    318
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default

    Quote Originally Posted by JohnnyW
    Just wondering if anyone had a good way of dealing with deleting a record in a parent child relationship in a database.

    For example say you had

    tblCars
    CarID
    Manufacturer
    Model
    ColourID

    tblCarColour
    ColourID
    ColourName
    RBG

    If say the manufacturer stopped making a certain colour so you want to remove the colour so you can't order the car in that colour now. How would you deal with that?

    If you deleted it obviously the historical data would be messed up as some orders would probably contain an order for a car in that colour.

    Just interested to see if anyone had a good way of dealing with this sort of problem.
    I would add a field to tblCarColour called Discontinued and set it as a boolean with a default of false.

    When the colour is discontinued just flip the flag to TRUE.

    HTH

    Harry
    Alastair Hamilton - WOWD



  3. #3
    Join Date
    Feb 2004
    Posts
    4,677
    Thanks
    2
    Thanked 107 Times in 98 Posts

    Default

    Quote Originally Posted by Wise Webs
    I would add a field to tblCarColour called Discontinued and set it as a boolean with a default of false.
    But then that would Discontinue the colour for all cars that use that particular colour?
    Warren Ashcroft
    Red Fox UK Limited - Pioneers in Internet Technology
    http://www.redfoxuk.com
    w.ashcroft [at] redfoxuk.com

    NOTE: Forum Private Messaging should not be used to contact staff with support queries.

  4. #4
    Join Date
    Oct 2005
    Location
    Scotland
    Posts
    220
    Thanks
    6
    Thanked 12 Times in 8 Posts

    Default

    Quote Originally Posted by Warren Ashcroft
    But then that would Discontinue the colour for all cars that use that particular colour?
    Sorry that was probably my fault, pretty crap example.

    At the moment I use the above solution of having an Active/Inactive boolean field but it just doesn't sit right with me, was just wondering if anyone else had a better way of doing it.

    The problem I have at the moment on a project is with a status field which uses a similar system at the moment to my example. Admin on the site want to be able to delete status's and create new ones etc but at any time a user can update their profile (in 5 years time for example) and if they had select the status which has then been deleted then it causes problems.

    Hope that made sense

  5. #5
    Join Date
    Apr 2005
    Location
    Haslemere, Surrey, UK
    Posts
    318
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default

    Quote Originally Posted by JohnnyW
    Sorry that was probably my fault, pretty crap example.

    At the moment I use the above solution of having an Active/Inactive boolean field but it just doesn't sit right with me, was just wondering if anyone else had a better way of doing it.

    The problem I have at the moment on a project is with a status field which uses a similar system at the moment to my example. Admin on the site want to be able to delete status's and create new ones etc but at any time a user can update their profile (in 5 years time for example) and if they had select the status which has then been deleted then it causes problems.

    Hope that made sense
    A boolean flag would sit fine in my view then. If a user has a profile with a status 'A' then admin could 'delete' the status (ie set the discontinued flag as 'true'). All users with a profile of this status would see their profile as Status 'A'. Any other user wouldnt be able to select status 'A'.

    HTH

    Harry
    Alastair Hamilton - WOWD



  6. #6
    Join Date
    Jan 2006
    Posts
    419
    Thanks
    2
    Thanked 16 Times in 16 Posts

    Default

    I totally agree with the solution, deleting data causes so many headaches - in reality you end up keeping it but marking it flagged as deleted.

    Quote Originally Posted by Wise Webs
    A boolean flag would sit fine in my view then. If a user has a profile with a status 'A' then admin could 'delete' the status (ie set the discontinued flag as 'true'). All users with a profile of this status would see their profile as Status 'A'. Any other user wouldnt be able to select status 'A'.

    HTH

    Harry

  7. #7
    Join Date
    Feb 2008
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jimlewis View Post
    I totally agree with the solution, deleting data causes so many headaches - in reality you end up keeping it but marking it flagged as deleted.

    It's quite handy to have a stored proc that properly deletes all the parent/child data, at least for dev purposes.

  8. #8
    Join Date
    Mar 2005
    Location
    Isle of Man
    Posts
    1,258
    Thanks
    3
    Thanked 23 Times in 23 Posts

    Default

    This is a many-to-many relationship, the way you would normally deal with something like this is to use three tables.

    tblCars
    CarID
    Manufacturer
    Model

    tblColours
    ColourID
    ColourName
    RBG

    tblCarColours
    CarID
    ColourID

    Then you just add or delete entries from the new tblCarColours table. If possible you would want to add a foreign key constraint to the car and colour id's in this table so that the related rows would be deleted if an entry is deleted from either of the other tables.

  9. #9
    Join Date
    Jun 2008
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I usually add these fields to all important tables, just out of habit now

    [CreatedDate] datetime,
    [LastModifiedDate] datetime,
    [Active] bit

    These have proved useful on so many occasions when the spec changes!

  10. #10
    Join Date
    Feb 2004
    Posts
    4,677
    Thanks
    2
    Thanked 107 Times in 98 Posts

    Default

    Quote Originally Posted by Gilgamesh View Post
    I usually add these fields to all important tables, just out of habit now

    [CreatedDate] datetime,
    [LastModifiedDate] datetime
    Ditto, would be great if SQL Server natively/internally supported these values for each record.
    Warren Ashcroft
    Red Fox UK Limited - Pioneers in Internet Technology
    http://www.redfoxuk.com
    w.ashcroft [at] redfoxuk.com

    NOTE: Forum Private Messaging should not be used to contact staff with support queries.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Posting Permissions

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