0

im doing a quite simple database in which i need to connect people as friends.

In my first column i have user_id and in the second one i have friend_id. What i want to achieve is to prevent a duplicate entry.

for example i want to have something like this:

user_id ; friend_id

1 ; 2

1 ; 3

1 ; 4

and so on

and prevent this:

1 ; 2

1 ; 3

1 ; 3

1 ; 4

1 ; 4

iv tried everything. If i set user_id as primary or unique, it wont allow me 2 entries with user_id = 1;

any help? thanks

Dan Stern
  • 2,155
  • 8
  • 26
  • 38

1 Answers1

1

As this answer states, a composite primary key such as (user_id, friend_id) won't allow you to use the index if you query only friend_id. You can alternatively create a UNIQUE key on (user_id, friend_id).

Community
  • 1
  • 1
Kermit
  • 33,827
  • 13
  • 85
  • 121