Archive for August 14th, 2013
Storing a Friendship: SQL Server Indexed Views, Part 2
You're building a social network and want to store who is friends with whom. OK. Let's assume you want Facebook-style friends: by agreeing to be friends with someone you allow them to be friends with you too. This is called a symmetric relationship and if your social model sticks to that kind of relationship, your application should enforce its symmetry.
How shall we store friends in a database table? There are several options.
Initiator first
A table with one record per friendship and two fields for each of the friends. The person who requested the friendship is stored in the first field: