Suppose we have this database design
Users table:
- id
- name
Friends table:
- id
- user_id
- friend_user_id
Let's say USER 1
befriended USER 2
, this is what my application logic will execute:
INSERT INTO friends (user_id, friend_user_id) VALUES (1, 2);
INSERT INTO friends (user_id, friend_user_id) VALUES (2, 1);
For the retrieval of the USER 1
friends, we simply query this:
SELECT * FROM friends WHERE user_id = 1
Is it a bad idea to make redundant rows when friending users?
What I am trying to avoid here is not to store the friendship in a single row and keep guessing in every query which column the user is in:
SELECT * FROM friends WHERE user_id = 1 OR friend_user_id = 1
The latter made my application logic very complicated in some cases.
friends
table probably doesn't need anid
column