I am using two tables for maintaining Items-Groups relationship.
I have items table which will have two columns ItemName and Groups.
The other table is Group which has a column GroupName
User will add one or more groups to each item. The user will then query for no of items assigned to that group.
Currently I am appending the GroupName to Groups column and while querying for no of groups I use %Groups% in where query.
But if the user adds the same group twice, I need to check and remove.
Is there any better way to maintain these relationships?
Don’t try to store multiple items in a single column(*).
Instead, create 3 tables –
Items,GroupsandItemGroups.ItemGroupswill store 1 row for each group that an item belongs to, e.g.:Note that, by adding the primary key on
ItemID,GroupID, we’ve prevented duplicates from occurring, with no further work.(*) SQL has one data structure designed for containing multiple items. It doesn’t have arrays, lists, dictionaries, etc. It just has one structure. And it’s called a table. Things work well if you use this structure. Things don’t work nearly so well if you try to fake some other kind of structure using e.g. string values stored in a single column.