Jump to content

[SOLVED] INSERT......ON DUPLICATE question


asmith3006

Recommended Posts

Hi,

 

I'm having trouble getting my head around INSERT...... ON DUPLICATE at the moment.

 

I have three tables,

 

Product details:

id, description.....

 

Categories

type, value

 

Product Categories

product_id, category_value

 

Example data for the Categories table:

height, short

height, tall

colour, red

colour, blue.

 

What I want to do is have no more than one entry for each category 'type' for each product (i.e. a product can be tall and red but not tall and short).

 

I was hoping to do this with INSERT...ON DUPLICATE as this means when a user changes a product from red to blue I don't have to do "if colour exists delete colour insert new colour".

 

Thanks for any and all help!

Link to comment
Share on other sites

You should create a trigger BEFORE INSERT to Product Categories table. Inside you should check, if you should insert or update.

 

Alternatively, you could do a small change to your table design

 

Product Categories
product_id, category, category_value
1, 'height', 'tall'
1, 'color', 'red'
2, 'color', 'blue'

 

and create a unique key on (product_id, category)

then you could use INSERT ... ON DUPLICATE KEY UPDATE

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.