DEV Community

nadirbasalamah
nadirbasalamah

Posted on

MySQL tutorial - 2 Relationship in Database

In SQL database, each entity is represented as a table that can connected each other. There are many types of relation in SQL database especially in MySQL database.

One to One Relation

One to one relation is a relation that represents each entity has a relation with single other entity. For example a single post is written by an author. The one to one relation is illustrated in this picture below.

One to one relation

One to Many Relation

One to many relation is a relation that represents an entity has a relation with many entities. For example a single post has a lot of comments. The one to many relation is illustrated in this picture below.

One to many relation

Many to Many Relation

Many to many relation is a relation that represents many entities has a relation with many other entities. The main characteristic of this relation in SQL database is the existence of a table that act as a connector between two other tables. For example a many posts can be liked by many users. This relation is illustrated in this picture below.

Many to many relation

How to define relation

Entity relation is define based on the use cases. To identify the relation that will be used can be done by abstraction using entity relationship diagram (ERD).

I hope this article is helpful for learning relation in SQL, If you have any thoughts or comments you can write in the discussion section below.

Top comments (0)