I have a table with four columns... A B C D
I want the B,D combination to be unique while inserting the records. For example, if there was a record already, then I don't want to insert that. Is this a database issue or do I have to do some SELECT in php to see if I get any records and then insert?
Also, how would I use the distinct for multiple columns? At present, if I say SELECT DISTINCT(A), it gives me all records not repeating in column A but I want to do this on multiple columns... Is there a way for this?
To insert unique records, do I have to do something in PHP
Moderator: General Moderators
You mean I should add the unique attribute to B and D? Like this perhaps?
But I thought it'll add B and D as unique and has nothing to do with maintaining unique combination of B and D... Can you advice me further please?
Code: Select all
ALTER TABLE table_name ADD UNIQUE (B,D);Maybe I'm not thinking straight but even if it did add them separately it wouldn't matter.
Assume B is 1 and D is 2. If you insert that into the db that exact combination can never happen again because B can never be 1 and D can never be 2. Now D can be 1 and B 2, but that's not the same combination.
Assume B is 1 and D is 2. If you insert that into the db that exact combination can never happen again because B can never be 1 and D can never be 2. Now D can be 1 and B 2, but that's not the same combination.
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
You need a unique KEY - not a unique column. The syntax is:
Does that work?
Code: Select all
CREATE TABLE .... UNIQUE KEY `keyname` (`B`,`D`)