Forum Moderators: coopster

Message Too Old, No Replies

How to Update an Association Table in MySQL?

         

bajingan

11:05 pm on Dec 12, 2005 (gmt 0)

10+ Year Member



OK.. I have 3 tables: table_items, table_categories and table_assoc_items_categories.

table_items
- item_id
- item_name

table_categories
- category_id
- category_name

table_assoc_items_categories
- item_id
- category_id

Each item can have multiple categories. That's why I have the association table.

My question is how do you update the association table when user makes changes?

Right now, I would DELETE all rows where category_id = X, and then INSERT item_ids selected by the user and category_id into the table.

Is this the right way of doing it? It seems too extreme to have to DELETE and INSERT. Can I do it with UPDATE?

Thanks.

dmmh

8:33 am on Dec 13, 2005 (gmt 0)

10+ Year Member



sure you can :)

UPDATE table SET field='$val' WHERE id='$id';

bajingan

9:57 am on Dec 15, 2005 (gmt 0)

10+ Year Member



Hi... thanks for your response. I'm still learning MySQL and I didn't even know how to ask the question right.

I know you can update the values using the SQL you gave me, but let's say you are looking at this entries form:

------------------------------
Item ID: itemA

Select all applicable categories for this item:

[] Category1
[] Category2
[] Category3
[] Category4
[] Category5

[Submit]
------------------------------

If you selected, say, Category2 and Category4 for this item for the very FIRST time, I could use an INSERT INTO to insert the values into the association table.

So the table would look like this:

table_assoc_items_categories
item_id ¦ category_id
-----------------------
itemA ¦ Category2
itemA ¦ Category4

On the next day, you want to change the category. Instead of Category2, you want Category3. You also want to add Category5. So you go to the entries form, uncheck Category2 and check Category3 and Category5 and hit Submit.

How do you do that in SQL the easiest way?

Because the entries form data is sent by the browser indicating what was checked, not what was unchecked, you only see Category3 and Category5 that you selected.

So my question is, how can I put these new Category3 and Category5 into the table?

If I used "UPDATE assoc_table SET category = 'Category3' WHERE item = 'itemA'", I would change every category with item = 'itemA' to Category3, not just one. This is certainly wrong.

The UPDATE call also assumes that there's a record in the table already. What if the table is still empty? So you have to make another query for each record to find out.

If I used INSERT, what if the same values already exist in the table? Then you have to make another query to check. Also what would happen with the category that you unchecked?

So my current solution is that I delete all records with item = 'itemA', and then do INSERT for all categories selected.

But I don't know if this is the right away because deleting records seems too scary for me. is there another simple way that doesn't require DELETE?

I hope I asked the question right this time :)

jatar_k

3:59 pm on Dec 15, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> So my current solution is that I delete all records with item = 'itemA', and then do INSERT for all categories selected.

that is what I would do, was thinking that would be my response as I was reading through.

a simpler way? not really, it is a lot more work any other way, you would need to check each one, count number of entries already, see if that number matches the entries you have, then update/insert accordingly for each change.

dump all for that item
add all new ones

I agree that deleting is scary but you are only deleting associations for a single product at a time. The worst scenario (assuming uniqueness of ids and soundeness of queries) is that a single product becomes unassociated with any cat.

bajingan

10:14 pm on Dec 15, 2005 (gmt 0)

10+ Year Member



Hi .... thanks for your response. Now I know I'm doing it right. I'm currently learning MySQL from a book and I often don't know if what I do is right or wrong.

Thanks again for your assurance.

jatar_k

10:21 pm on Dec 15, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



my pleasure, feel free to post anytime you aren't sure :)

coopster

2:05 pm on Dec 16, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



To make it *less* scary you could use transactions [dev.mysql.com]. Basically, either ALL of the statements in a transaction get processed or NONE of them do. MySQL has an article on them here:
[dev.mysql.com...]