Unique index across multiple fields
I recently came across this question:
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:
I could then alter my insert statement to include ON DUPLICATE KEY.
alter table tablename add unique index(field2, field3);
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):
Can anyone verify my understanding of this, or perhaps point out flaws in my thinking.
"INSERT INTO tablename (field1, field2, field3, field4) VALUES ('$_POST[field1]','$_POST[field2]','$_POST[field3]','$_POST[field4]') ON DUPLICATE KEY DO NOTHING";