1) One to One(1-1): In this case a record/entity from a table can only relate to one record/entity from the other table.
2) One to Many(1-M): In this case, a record/entity from a table can relate to multiple records/entities from the other table
3) Many to Many(M-M): This is a case where multiple record/entities from a table can relate to multiple records/entities from the other table. Note: This may get quite complex sometimes so in order to simplify it you can introduce a weak entity in between the two table.
In SQL (Structured Query Language), there are several types of relationships that can be established between database tables. These relationships define how the data in different tables are related to each other. The most common types of relationships in SQL are:
One-to-One (1:1) Relationship:
In a one-to-one relationship, one record in a table is related to only one record in another table.This relationship is established by having a primary key in one table that is also used as a foreign key in the other table.
One-to-one relationships are relatively rare in database design but can be useful for separating less frequently used or optional attributes into a separate table.
One-to-Many (1:N) Relationship:
In a one-to-many relationship, one record in a table can be related to multiple records in another table, but each record in the second table is related to only one record in the first table.This relationship is established by having a primary key in one table that is referenced as a foreign key in the other table.
It is the most common type of relationship in database design and is used to represent hierarchical structures or when one entity can have multiple related entities.
Many-to-Many (N:M) Relationship:
In a many-to-many relationship, multiple records in one table can be related to multiple records in another table.This relationship is implemented by introducing a junction table, also known as a linking or associative table, that contains foreign keys from both tables.
The junction table resolves the many-to-many relationship into two one-to-many relationships.
Self-Referential Relationship:
A self-referential relationship occurs when a table relates to itself.
It is commonly used to represent hierarchical structures, such as organizational charts or category hierarchies, where each record can have a relationship with other records in the same table.
"One of the most important things in databases is to understand the types of relations in the databases. That stands for both – a process of designing a database model as well as when you’re analyzing your data. Understanding these relations is somehow natural and not so complex but is still essential in the database theory (and practice).
Data model
In the previous article, Learn SQL: SQL Scripts, we’ve extended the data model we’ve used so far by adding a few new tables and filling them with the data. “Very nice. I like.”. That is the model in the picture below:
While the model itself has only 6 tables (real-life models could have hundreds of tables), it contains the most common rules we’ll meet in many other models as well. This also stands for types of relations between tables. Without much effort, you can easily notice that each table is connected/related to another table with exactly one line (foreign key). The primary key from one table (e.g. employee.id) is related to the value from another table (e.g. call.employee_id). We’ll use this relation when we need data from both these tables, mostly when we’re writing select queries. The question remains on how to relate two tables. Even between two lines (relations), we could have some minor differences. We’ll discuss that now.
Types of relations
There are 3 different types of relations in the database:
one-to-one
one-to-many, and
many-to-many
Although they are different, they are represented in (almost) the same manner in the databases and that is the line between the two tables."