tags:

views:

32

answers:

3

I was wondering what else should I add to my friends table how can i stop a user from adding the same friend twice as well as when the user is online? As well as any suggestions on how i can make my friends table better?

Here is my MySQL table.

CREATE TABLE friends (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
user_id INT UNSIGNED NOT NULL,
friend_id INT UNSIGNED NOT NULL,
PRIMARY KEY (id)
);
+2  A: 

You don't need an id on a pivot table and could define your primary key like this PRIMARY KEY(user_id, friend_id), this would prevent from having duplicates.

Serty Oan
What if one user just wants to add the friend but the other does not?
alpha
@alpha: The relationship is only that `id` considers `user_id` a friend. In reverse, the most you can imply is that `user_id` is an acquaintance of `id` - not a friend, unless a matching for with the values reversed exists.
OMG Ponies
So you need to add one more field, `friendship_accepted` which could be a boolean. Set to 0 when created, and to 1 when the friend accepted
Serty Oan
@OMG Ponies then how should friends be accepted?
alpha
A: 

IMO there's no need for id attribute.
You can add timestamp, which can be useful sometimes.
Create key for both user_id and friend_id, and they will be unique, which prevents you from creating this tuple twice.

Wojtek
Some ORMs have issues with tables that have composite keys
OMG Ponies
That very much depends on ORM used - here we just design a relation.
Wojtek
A: 

well if the deal it's about the table design you could make the combination of the user_id and the friend_id as an unique key, or maybe to make all those three(3) fields as primary keys,,, not so good practice but works.

The other thing would be for you to controll this by using PHP or the connection language you alreade chose.

Let me know if this helps.

Jean Paul