I have a table User with a column id. I want the user to have relationships with other users, so I create a table Relationships with columns user_id_1 and user_id_2.
The questions is how to constrain the table so that
1) there are no entries where user_id_1 equals user_id_2.
For example, entry (1, 1) is bad, because it indicates a relationship to oneself.
2) if there is an entry (user_id_1, user_id_2), the entry (user_id_2, user_id_1) is not allowed.
For example, having entries (1, 2) and (2, 1) is bad, because it indicates the same relationship.
I am using MySQL, although I think this is a general design issue. Thanks!
You can write a
INSERTandUPDATEtriggers on the join table that check these conditions.