views:

39

answers:

2

Given 2 tables:

Person

PersonsFavoriteColors

A person can have one or more favorite colors. These colors are updated with a multi-select control (CheckBoxList, ListBox w/ multi-select enabled).

In the past, if I am updating the person's colors, I'd:

  1. Start Transaction
  2. Delete all color records for the person
  3. Insert records for each selected color
  4. Commit Transaction

Is this the standard and best practice for handling multi-select controls that add / update / delete records in "to-many" child tables?

Thanks!

+1  A: 

I normally wouldn't delete all the old colours but rather just the ones that were no longer favourites and then I'd only add the ones that were actually new.

ho1
Sure. If they had 10 selected (previously inserted) and then they remove 2 of those, I could delete those rows, but it seemed easier to do the wipe and restore method rather than a series of updates / deletes based on what's selected.How would you remove 2 of 10?
D-Sect
I assumed that the colours were just a simplified example, but if it is actually just such simple values as their favourite colour or similar, maybe it would be ok to do as you say. Regarding how to remove 2 of 10, I'd probably downloaded the colours to store them in a collection in memory and then used that to populate the List/Combo and then I'd just compare values between the two to see what should be removed/added.
ho1
Thank you. This was the method I'd used.
D-Sect
A: 

If you're not locked in to two tables, a relatively simple way to store values the way you've described is to use an array as the data type of favorite colors instead of a separate table.

http://www.postgresql.org/docs/8.0/interactive/arrays.html

If that is an acceptable option, your updating instructions would simply be:

  1. Start Transaction
  2. Update Row
  3. Commit Transaction
adam
This is an MS SQL 2K5 project, if that helps. I'm trying to stay away from a composite field that needs to be parsed / split - or something that uses bitwise comparators (red = 8, blue = 16, etc - ANDing to work with)
D-Sect