Search code examples
database-designentity-relationship

Entity Relationship User and Contacts


I am trying to create database where users has contacts. I designed the tables below and I am confused about the part how the relationship should be because it looks like it should be one to many , but its also many to many relationship.

In database each user has to have contacts table that contacts are other user in database. But contacts has to belong the each user.

Whats the relationship in this case?

enter image description here


Solution

  • What's the relationship in this case?

    Many to Many.

    In your Contacts table, you have an owner_id and a contact_id. You could just as easily call these columns user_1_id and user_2_id.

    Using your column names, a contact_id can have many owner_id's, and an owner_id can have many contact_id's.

    The clustering (primary) index on the Contacts table is (owner_id, contact_id). You also need to define one unique index on the Contacts table, (contact_id, owner_id).