djones Posted August 2, 2010 Share Posted August 2, 2010 I'm trying to insert unique data based on 2 fields. The product_id and the value_id combined should be unique. I'm not sure how to do this. I have: ID(primary key) -- not shown below PRODUCT_ID(unique key) VALUE_ID(unique key) VARIATION_ID I need product_id and value_id to be unique together. When I insert this data it only insert one row and I'm assuming it's because It's just looking at each unique fields by itself thus stopping at the next '37' insert. For example 37 and 11 should not insert twice, 37 and 3 ...etc INSERT INTO table (product_id, value_id, variation_id) VALUES ('37', '11', '4') INSERT INTO table (product_id, value_id, variation_id) VALUES ('37', '3', '2') INSERT INTO table (product_id, value_id, variation_id) VALUES ('37', '10', '4') INSERT INTO table (product_id, value_id, variation_id) VALUES ('37', '3', '2') INSERT INTO table (product_id, value_id, variation_id) VALUES ('37', '9', '4') INSERT INTO table (product_id, value_id, variation_id) VALUES ('37', '5', '2') INSERT INTO table (product_id, value_id, variation_id) VALUES ('37', '11', '4') INSERT INTO table (product_id, value_id, variation_id) VALUES ('37', '5', '2') INSERT INTO table (product_id, value_id, variation_id) VALUES ('37', '10', '4') INSERT INTO table (product_id, value_id, variation_id) VALUES ('37', '5', '2') INSERT INTO table (product_id, value_id, variation_id) VALUES ('37', '9', '4') Quote Link to comment https://forums.phpfreaks.com/topic/209607-help-with-insert-ignore-into/ Share on other sites More sharing options...
Pikachu2000 Posted August 2, 2010 Share Posted August 2, 2010 You can't set product_id and value_id as unique indices if you want to insert the same value into that field in multiple records. Quote Link to comment https://forums.phpfreaks.com/topic/209607-help-with-insert-ignore-into/#findComment-1094265 Share on other sites More sharing options...
djones Posted August 2, 2010 Author Share Posted August 2, 2010 In Oracle (*ducks*) I can do this. I was hoping for a way in MySQL. So is this not possible? I can work around this, but this would save me several lines of PHP code. Quote Link to comment https://forums.phpfreaks.com/topic/209607-help-with-insert-ignore-into/#findComment-1094269 Share on other sites More sharing options...
djones Posted August 2, 2010 Author Share Posted August 2, 2010 I forgot the IGNORE in the INSERTs above. They all include that... Quote Link to comment https://forums.phpfreaks.com/topic/209607-help-with-insert-ignore-into/#findComment-1094272 Share on other sites More sharing options...
Pikachu2000 Posted August 2, 2010 Share Posted August 2, 2010 If you use the IGNORE keyword' date=' errors that occur while executing the INSERT statement are treated as warnings instead. For example, without IGNORE, a row that duplicates an existing UNIQUE index or PRIMARY KEY value in the table causes a duplicate-key error and the statement is aborted. With IGNORE, the row still is not inserted, but no error is issued.[/quote'] Quote Link to comment https://forums.phpfreaks.com/topic/209607-help-with-insert-ignore-into/#findComment-1094277 Share on other sites More sharing options...
PFMaBiSmAd Posted August 2, 2010 Share Posted August 2, 2010 Define your table with the following - UNIQUE INDEX `Index_name`(`product_id`, `value_id`) Quote Link to comment https://forums.phpfreaks.com/topic/209607-help-with-insert-ignore-into/#findComment-1094281 Share on other sites More sharing options...
djones Posted August 2, 2010 Author Share Posted August 2, 2010 Wow, I'm made that real hard for myself. That's it. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/209607-help-with-insert-ignore-into/#findComment-1094284 Share on other sites More sharing options...
Pikachu2000 Posted August 2, 2010 Share Posted August 2, 2010 So an index spanning the 2 fields will allow that? I wish I'd known that about 3 months ago . . . Quote Link to comment https://forums.phpfreaks.com/topic/209607-help-with-insert-ignore-into/#findComment-1094286 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.