PDA

View Full Version : Stopping duplicate entries


PeterPan
9-23-06, 11:55 AM
Hi, i have a table.. with 7-8 fields...

Now, 3 of my fields, I want to be unique, however no INDIVIDUAL field
will be unique...

IE I can have values

Smith, 7, 8, (& extra fields here...)
Smith, 7, 5, (& extra fields here...)
Smith, 6, 5, (& extra fields here...)
Smith, 6, 3, (& extra fields here...)
John, 6, 5, (& extra fields here...)

Ie the 3 fields can have duplicate entries - two '5's' in the 3rd column, etc..
multiple Smiths etc...

HOWEVER, I do not want two lots of

Smith, 6, 5, (& extra fields here...)
Smith, 6, 5, (& extra fields here...)

QUESTION

Is there a way we can use the INSERT command, - But compare the 3
fields, & see if there is already a table entry with that
combination...

and only insert a new row, if there is no match for ALL 3 of the first
3 fields....

Or - Will I need to do a SELECT command - & then loop through the
array & see if it exists already...


Does this make sense ?

PS - I am already doing a SELECT - & adding toa n array - & looping through them...

According to my PHP code, & the scrteduled tasks logs (& onscreen printout..) - The values are NOT being added..

But - When I go into phpmyadmin screen - I do see dupes...

any advise ?

PeterPan
9-23-06, 12:20 PM
Never mind

Ive fixed it....

Changed the field from TINYINT to INT..

The values all ended / stopped at 127...

Yuri
9-24-06, 07:12 AM
You can create a table with a primary key composed by several fields (the 3 one you are interested by). Example:
CREATE TABLE `test` (
`Name` VARCHAR( 10 ) NOT NULL ,
`ID2` TINYINT NOT NULL ,
`ID3` TINYINT NOT NULL ,
`test` VARCHAR( 30 ) NOT NULL ,
PRIMARY KEY ( `Name` , `ID2` , `ID3` )
) ENGINE = MYISAM ;

So, when you perform an INSERT, MySQL will check if the primary key is unique (if the combination of field 1, 2 and 3 is unique).