tags:

views:

50

answers:

2

Hey guys, how the heck do I go about doing this.

I have an address book I'm making and I'm trying to figure out how to tackle my groups. I let the users rename their groups at will. But then this complicates my life ;0

I have 2 tables. groups and contacts the groups has the group names per user. the contacts has a group column that says what group that contact entry belongs to.

How can I rename a group from the groups table and have the new name reflect in the contacts table so everything matches up?

+5  A: 

I would suggest changing your model.
Use IDs as primary keys and an intersection-table to assign your contacts to groups:

Groups
id
name

Contacts
id
name

Group_Contacts
group_id    -> Groups.id
contact_id  -> Contacts.id

Now you can change the group-name whenever you want, without updating the contacts.


EDIT: If you only want to get the contacts of a certain group, use a SELECT like this one:

Select c.name
From groups g
Join group_contacts gc On ( gc.group_id = g.id )
Join contacts c On ( c.id = gc.contact_id )
Where g.name = 'Your group name'
Peter Lang
hmm I see, can you give me a quick php/sql scenario on how I could start my set up pls ;)
s2xi
@s2xi: This would be difficult without knowing what exactly your problems are. Please try to set it up yourself, and feel free to ask another question once you are stuck...
Peter Lang
well ok, perhaps I just need a hint/push in the right direction. When I query the database in order to get the group names I would query the groups table correct? in order to get a list of contacts i would query the contacts table correct? if I wanted to ONLY get the contacts that belong to a specific group what would I query?
s2xi
@s2xi: See my update for that query.
Peter Lang
where are you specifying c.name
s2xi
`c` is my alias for the table `contacts`, and `name` is a column within that table. Replace it by whatever your column-name is.
Peter Lang
;-) After looking at it I finally understood that thank you. One quick question. I tried to say WHERE `user_id` = 2 (where 2 is the specific user logged in at the moment through a SESSION variable) but i get the error "Column `user_id` in where clause is ambiguous. What does this mean? I understand the meaning of ambiguous... I think, it means redundant sorta of right?
s2xi
You need to specify your table-name (or the alias if you have one in your query): `WHERE u.user_id = 2`.
Peter Lang
ah got it. i wasn't specifying which table for the query to look in!
s2xi
+1  A: 

You also could use a before update trigger which first renames all entries in the contacts table to match the new name and then let the update go on

ITroubs