Skip to content

Instantly share code, notes, and snippets.

@freitzzz
Last active August 27, 2018 22:24
Show Gist options
  • Save freitzzz/29e250b8c7856de3c8c51fcf133d0eaa to your computer and use it in GitHub Desktop.
Save freitzzz/29e250b8c7856de3c8c51fcf133d0eaa to your computer and use it in GitHub Desktop.
Examples for each database relationship type

A simple Gist for exampling database relationship types

One-to-One

  • One-to-One relationship type is the easiest to understand, yet probably the rarest to use in database design

  • One-to-One implies that one entity can only exists if another entity exists and vice-versa. One good example for this is the typical citizen card.

    • One Citizen has one SSN which is linked to his card, and his SSN is only linked to his card and not to any other citizen

One-to-Many

  • One-to-Many relationships are more common that One-To-One relationships, yet they often tend to be badly used due to being missunderstood

  • One-to-Many implies that one entity holds a set of entities, which these entities only exist in that entity aggregating them. We can example it with museums.

    • One Museum is currently showing a set of paintings, and these paiting are only being shown on that museum and not in any other place.
    • If Museum A is currently in hold of Paiting A, then Museum B who is opened at the same time as Museum A can't be in hold of Painting A (Unless we are talking about clones, but you get it)

Many-to-One

  • As One-to-Many, Many-to-One relationships follow the same rules, but on a inverse way. In Many-to-One we think that a set of entities are aggregated by another entity

  • We could also example Many-to-One relationships with the Museums example, but instead of saying that one Museum shows a set of paintings, we say that a set of paintings are being shown on a museum. All the rules are the same, the only thing that changes is the way we model our entities relationship.

Many-to-Many

  • Last but not least there are Many-to-Many relationships. We can think of them as a mix of One-to-Many and Many-to-one in the way that Many-to-Many relationships implie that a set of entities aggregate a set of other entities

  • A good example for Many-to-Many relationships is the Book-Authors mapping.

    • A book is written by an author or a group of authors, and author write books. In these case an author aggregates a set of books which we wrote, yet these books can also have been written in collaboration with other authors, so in some instances a group of authors share the same aggregated books
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment