Click to See Complete Forum and Search --> : Unique Pairs


Sid3335
10-13-2006, 09:29 AM
Hi Guys:

Is it possible to create a pair of unique fields in mysql.

for example if i have a table that holds a user_id and a product_id, i want to dissallow duplicate entires of both fields.

so
1,1
1,2
1,3

will be fine but i don't want to allow 1,1 twice in combination.

chazzy
10-13-2006, 01:08 PM
so the two together form the primary key on the table?

Sid3335
10-13-2006, 04:17 PM
yeah i guess so, but as a pair not just 2 seperate primary keys.

i'm sure i read somwhere that its possible, but it may have been using a different type of database.

i think the term i read about (or possibly dreamed) was unique matching pairs or similar. but i have been unable to find the article again.

NogDog
10-13-2006, 04:26 PM
I believe it would be something like this:

CREATE TABLE table_name (
user_id INT,
product_id INT,
<other column defintions>,
PRIMARY KEY (user_id, product_id)
);

Sid3335
10-14-2006, 04:13 AM
Thanks guys, worked no problem.