Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-03-2013, 07:15 PM   PM User | #1
mharrison
New Coder

 
Join Date: Dec 2012
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
mharrison is an unknown quantity at this point
Unique index across multiple fields

I recently came across this question:
http://stackoverflow.com/questions/6...lumns-in-mysql

I just want to make sure I am understanding the answer correctly. (table names and fields have been made generic to protect the innocent)

If I were to issue the command:
Code:
alter table tablename add unique index(field2, field3);
I could then alter my insert statement to include ON DUPLICATE KEY.
From my searching about the ON DUPLICATE KEY clause, I'm finding a lot of options and I'm not quite sure how I would do this. To put it simply, I'm inserting data across 4 fields (field1,field2, field3, field4). I specify the unique index across field2 and field3 so when I insert, if the data being inserted is the same as an existing record on the field2 and field3 columns, I want nothing to be done (ie, don't insert a new record and don't update an existing record).

It looks like my insert command would have to look something like this (note that I insert records via a php form):
PHP Code:
"INSERT INTO tablename (field1, field2, field3, field4) VALUES ('$_POST[field1]','$_POST[field2]','$_POST[field3]','$_POST[field4]') ON DUPLICATE KEY DO NOTHING"
Can anyone verify my understanding of this, or perhaps point out flaws in my thinking.
mharrison is offline   Reply With Quote
Old 03-03-2013, 08:21 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Simpler than that:

INSERT IGNORE ...

That's all you need. Don't need the ON DUPLICATE KEY stuff if you just want to ignore the duplication error.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
mharrison (03-04-2013)
Old 03-04-2013, 04:17 PM   PM User | #3
mharrison
New Coder

 
Join Date: Dec 2012
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
mharrison is an unknown quantity at this point
Quote:
Originally Posted by Old Pedant View Post
Simpler than that:

INSERT IGNORE ...

That's all you need. Don't need the ON DUPLICATE KEY stuff if you just want to ignore the duplication error.

Once again, thank you. Followup question. Is there a way to have the query let me know which records it did not insert due to being duplicates. Not really important, just more curiosity on my part.
mharrison is offline   Reply With Quote
Old 03-04-2013, 04:50 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Yes. When an insert succeeds, the number of rows affected is 1. When it doesn't that number is 0.

http://php.net/manual/en/function.my...ected-rows.php
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 03-05-2013, 01:14 AM   PM User | #5
mharrison
New Coder

 
Join Date: Dec 2012
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
mharrison is an unknown quantity at this point
Just had a thought...if I am adding a unique index to a column...am I going to run into problems if the data in that column is identical....for example...I could have multiple entries where the value is "Happy"....I don't care about it being unique in that column, I care about the uniqueness of a new record between the two columns when looked at together. Not sure if I am explaining that correctly...makes sense in my head but then again, I am running on 3 hours of sleep.
mharrison is offline   Reply With Quote
Old 03-05-2013, 03:25 AM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Try it. You'll be pleasantly surprised. Or maybe not surprised,but still happy.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 03-06-2013, 02:49 AM   PM User | #7
mharrison
New Coder

 
Join Date: Dec 2012
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
mharrison is an unknown quantity at this point
Quote:
Originally Posted by Old Pedant View Post
Try it. You'll be pleasantly surprised. Or maybe not surprised,but still happy.
Well, I was surprised...not in the happy way.

Ended up getting: #1071 - Specified key was too long; max key length is 1000 bytes.

In looking at my records, there is no way I can modify them at all in order to generate a unique index from them.
mharrison is offline   Reply With Quote
Old 03-06-2013, 06:02 AM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Ahhh...bummer. You know, keys *can* be built from partial fields. Could you guaranteed a unique key in that way?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 03-06-2013, 08:33 AM   PM User | #9
Arcticwarrio
Regular Coder

 
Arcticwarrio's Avatar
 
Join Date: May 2012
Location: UK
Posts: 587
Thanks: 15
Thanked 65 Times in 65 Posts
Arcticwarrio is on a distinguished road
cant you just add a column that is auto increment?
__________________
There are 10 types of people on CodingForums,
Those who understand Binary and those who dont.
Arcticwarrio is offline   Reply With Quote
Old 03-06-2013, 03:58 PM   PM User | #10
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
Originally Posted by Arcticwarrio View Post
cant you just add a column that is auto increment?
How will that help him detect that the 4 fields he NEEDS to be unique actually are unique? All that would do is ensure that he doesn't duplicate the auto increment column. Kind of useless for his purposes.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:25 PM.


Advertisement
Log in to turn off these ads.