Types of Relationship in DBMS

Learn via video course
FREE
View all courses
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
Topics Covered

A relationship in DBMS is a fundamental concept, and it acts as a basis or the fundamental pillar to understanding other parts and concepts of the DBMS and SQL too. In real life, a relationship can be defined as a parent-child relationship, you and your father are related, etc. Similarly, a relationship in DBMS is the way in which two or more data sets are linked, i.e., any association between two entity types is called a relationship. So entity takes part in the relationship, and it is represented by a diamond shape. Three specific types of relationships can exist between the tables, and they are explained with the examples below.

This article covers the various types of relationships in DBMS. To study the entity in DBMS, refer to Entity in DBMS. Also, there are various topics on SQL and other programming languages available at Scaler Topics.

Types of Relationship in DBMS

Relationships allow the datasets to share and store data in separate tables. Relationships are of three types in DBMS and the next section explains those different types of relationships in DBMS with examples.

  • One-to-One relationship
  • One-to-Many relationship or Many-to-One relationship
  • Many-to-Many relationship

One-to-One Relationship

A One-to-one relationship means a single record in Table A is related to a single record in Table B and vice-versa.

For example, If there are two entities, 'Person'(Name, age, address, contact no.) and 'Aadhar card'(Name, Aadhar no.). So each person can have only one Aadhar card, and the single Aadhar card belongs to only one person.

You can visualize the one-to-one relationship in DBMS like this:

Example of one-to-one relationship in dbms

This type of relationship is used for security purposes. In the above example, we can store the Aadhar card number in the same entity 'Person', but we created another table for the 'Aadhar card' because the Aadhar card number may be sensitive data and should be hidden from others. Let's take another example with the databases.

Example:

Consider a table of Employees as shown below:

Table A:

emp_idemp_nameemp_address
001Claira Anderson113, Zaraiah Road, TX 77001
003Marc Doe34343, Palm Jumeirah Road, Dubai 990039
005Bruce Quilt23, Santa Cruiz Road, NY 44303

Now, you can place the employee address in a separate table as shown below:

Employee:

emp_idemp_nameemp_address_id
001Claira Anderson901
003Marc Doe903
005Bruce Quilt905

Employee Address:

emp_address_idemp_address
901113, Zaraiah Road, TX 77001
90334343, Palm Jumeriah Road, Dubai 990039
90523, Santa Cruiz Road, NY 44303

Now, the relationship between the Employees table and Employee Address can be created. In the above example, one emp_address_id belongs to only one emp_address, that is, the relationship is a one-to-one relationship. This type of relationship is not very common as the employee's address can also be included in the Employee table as shown in Table A and this would work too.

One-to-Many Relationship

Such a relationship exists when each record of table A can be related to one or more records of another table i.e., table B. However, a single record in table B will have a link to a single record in table A. This is the most common relationship you will find that is widely used. A one-to-many relationship in DBMS can also be named a many-to-one relationship, depending on the way we view it.

The one-to-many relationship in DBMS exists between the pair of tables if a single record in one table is related to one or more records of the other table. For example, if there are two entities, 'Customer' and 'Account', then each customer can have more than one account, and also, each account is owned by one customer only.

You can visualize the one-to-many relationship in DBMS like this:

Example of a one-to-many relationship in dbms

Example:

Consider a table of Customers and Orders as shown below:

Customers:

customer_idcustomer_namecustomer_no
111Maria Danzie1199229921
222Alex Brat3939637382
333Sania Martini82492835634

Orders:

order_idorder_amountcustomer_id
100011200222
100022000333
100034500222
100041220111
100053550222

The relationship between the above two tables can be visualized like this:

Example of a one-to-many relationship between the tables

In this example, there is a one-to-many relationship if looked at from the perspective of the Customers. As shown above, the customer_id - 222 is related to the three different order_id. In a similar way, there is a many-to-one relationship between the tables if looked at from the perspective of the Orders table.

Many-to-Many Relationship

A many-to-many relationship exists between the tables if a single record of the first table is related to one or more records of the second table and a single record in the second table is related to one or more records of the first table. Consider the tables A and B. In a many-to-many relationship, each record in Table A can be linked to one or more records in Table B and vice-versa. It is also represented as an N : N relationship.

For example, consider the two tables i.e., a student table and a courses table. A particular student may enroll himself in one or more than one course, while a course also may have one or more students. Hence, this is a great example of many-to-many relationships.

You can visualize the many-to-many relationship in DBMS like this:

A many-to-many relationship from the perspective of table A.

First example of the many-to-many relationship between the tables

A many-to-many relationship from the perspective of table B.

Second example of the many-to-many relationship between the tables

Example:

Consider the tables of Orders, Items, and Items Orders as shown below:

Orders:

order_idorder_amountcustomer_id
100011200222
100022000333
100034500222
100041220111
100053550222

Items:

item_iditem_name
1201Maggi
1202Pizza
1203Kurtossh

Items Orders:

order_iditem_id
100011201
100011203
100041202
100041203
100051201
100031201

The relationship between the above two tables can be visualized like this:

Example of a many-to-many relationship in dbms

As shown in the above example, the table Items Orders has only one purpose, and that is to create many-to-many relationships between the items.

Conclusion

  • The relationship in DBMS is the most essential or fundamental part of its functioning, and there are three types of relationships available in RDBMS.
  • If relationships would not have been there, the entire database structure would become useless. Click here, to read more about structure in DBMS.
  • A one-to-one relationship means when a single record in the first table is related to only one record in the other table.
  • A one-to-many relationship is defined as when a single record in the first table is related to one or more records in the other table, but a single record in the other table is related to only one record in the first table.
  • A many-to-many relationship can be defined as when a single record in the first table is related to one or more products in the second table and a single record in the second table is related to one or more records in the first table.
  • Redundancy of the data is minimized in relationships.

Read More: