cipher
cipher

Reputation: 2484

Complying with Database Normalization

Database normalization is getting on my nerves. Suppose I have a scenario to find out mutual friends. And I am using three fields in my database

conn_id,user_id,friend_id

user_id represent a normal id of users, and friend id represent the same, i am just trying to relate each users with friends.

I now enter a user id, and enter friends id, one record each

For Example:

conn_id | user_id |friend_id

1 - us1 - us2

2 - us1 - us3

3 - us1 - us5

4 - us3 - us1

5 - us3 - us6

........................................and so on

How does it fail to comply with database normalization?

EDIT (clarification from comments):

someone got me confused saying that i have one record with user1 -- user2 . And another record with user2 -- user1 : That does not violate normalization, does it?

Upvotes: 1

Views: 82

Answers (1)

FoolishSeth
FoolishSeth

Reputation: 4021

A common normalization violation is the tacking on of a sequence number for an id when rows are already uniquely identified by some column or combination of columns therein. Of course people do this all the time. There are reasons to keep your schemas normalized and plenty of reasons not to do so as well.

It looks like that's the case here. If it is not meaningful to have duplicate entries of (user_id, friend_id), then that combination of columns can be used as a primary key. Unless the sequence number is actually meaningful in its own right it is likely extraneous from a normalization perspective.

UPDATE

Another consideration you bring up below is the possibility of duplicate records e.g. (u1 -> u2) and (u2 -> u1). This comes down to a question of whether or not friendship is commutative.

If user2 is user1's friend, then is user1 also necessarily user2's friend? If friendship is commutative then you'd have duplicate records. If this is something like a social app I would assume that's NOT the case and those would not be duplicate records at all: they represent two completely independent relationships.

Upvotes: 1

Related Questions