The Wayback Machine - https://web.archive.org/web/20241001053037/https://www.geeksforgeeks.org/recursive-relationships-in-er-diagrams/
Open In App

Recursive Relationships in ER diagrams

Last Updated : 24 Mar, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Prerequisite – ER Model 

A relationship between two entities of a similar entity type is called a recursive relationship. Here the same entity type participates more than once in a relationship type with a different role for each instance. In other words, a relationship has always been between occurrences in two different entities. However, the same entity can participate in the relationship. This is termed a recursive relationship. 

Recursive relationships are often used to represent hierarchies or networks, where an entity can be connected to other entities of the same type. For example, in an organizational chart, an employee can have a relationship with other employees who are also in a managerial position. Similarly, in a social network, a user can have a relationship with other users who are their friends.

To represent a recursive relationship in an ER diagram, we use a self-join, which is a join between a table and itself. In other words, we create a relationship between the same entity type. The self-join involves creating two instances of the same entity and connecting them with a relationship. One instance is considered the parent, and the other instance is considered the child.

We use cardinality constraints to specify the number of instances of the entity that can participate in the relationship. For example, in an organizational chart, an employee can have many subordinates, but each subordinate can only have one manager. This is represented as a one-to-many (1:N) relationship between the employee entity and itself.

Overall, recursive relationships are an important concept in ER modeling, and they allow us to represent complex relationships between entities of the same type. They are particularly useful in modeling hierarchical data structures and networks.

Example: Let us suppose that we have an employee table. A manager supervises a subordinate. Every employee can have a supervisor except the CEO and there can be at most one boss for each employee. One employee may be the boss of more than one employee. Let’s suppose that REPORTS_TO is a recursive relationship on the Employee entity type where each Employee plays two roles.

  1. Supervisor
  2. Subordinate


 Supervisors and subordinates are called “Role Names”. Here the degree of the REPORTS_TO relationship is 1 i.e. a unary relationship.  

  • The minimum cardinality of the Supervisor entity is ZERO since the lowest level employee may not be a manager for anyone.
  • The maximum cardinality of the Supervisor entity is N since an employee can manage many employees.
  • Similarly, the Subordinate entity has a minimum cardinality of ZERO to account for the case where CEO can never be a subordinate.
  • Its maximum cardinality is ONE since a subordinate employee can have at most one supervisor.

Note – Here none of the participants have total participation since both minimum cardinalities are Zero. Hence, the relationships are connected by a single line instead of a double line in the ER diagram. 

To implement a recursive relationship, a foreign key of the employee’s manager number would be held in each employee record. A Sample table would look something like this:- 

Emp_entity( Emp_no,Emp_Fname, Emp_Lname, Emp_DOB, Emp_NI_Number, Manager_no);

Manager no - (this is the employee no of the employee's manager)

Example

CREATE TABLE employee (
    id INT PRIMARY KEY,
    name VARCHAR(50),
    manager_id INT,
    FOREIGN KEY (manager_id) REFERENCES employee(id)
);

Here, the employee table has a foreign key column called manager_id that references the id column of the same employee table. This allows you to create a recursive relationship where an employee can have a manager who is also an employee.

 


Previous Article
Next Article

Similar Reads

Total Recursive Functions and Partial Recursive Functions in Automata
Total Recursive Functions: A recursive function is called total recursive function if it is defined for its all arguments.Let f(a1, a2, ...an) be a function defined on function g(b1, b2, ...bn).Then f is a total function if every element of f is assigned to some unique element of function g. A total function is called recursive or primitive recursi
2 min read
Difference Between Recursive Predictive Descent Parser and Non-Recursive Predictive Descent Parser
Syntax analysis is an important task of compilers. Parsing techniques can be used for the analysis of the structure of programming languages. Out of these techniques, Recursive Predictive Descent Parsing and Non-Recursive Predictive Descent Parsing are two well-known top-down methods. Although both intend to analyze strings according to grammar rul
6 min read
Attributes to Relationships in ER Model
In ER model, entities have attributes which can be of various types like single-valued, multi-valued, composite, simple, stored, derived and complex. But relationships can also have attributes associated to them. Generally it is not recommended to give attributes to the relationships if not required because while converting the ER model into Relati
2 min read
Relationships in ER model
Entity Relationship model (ER model) contains entities and relationships. ER model enables us to know how these entities are associated with each other. Entities interact with other entities through associations or relationships. Let us assume that Geeks is the name of a student, Placement 100 is the name of a course. For example, student Geeks enr
2 min read
Data Objects, Attributes and Relationships in DBMS
Data Model is an abstract model that represents the data objects, data flow between these data objects, and the interrelationship between these data objects. It is a way of storing data on a computer so that it can be used in a more efficient manner for further purposes. Data model or data structure consists of following fundamental elements: 1. Da
3 min read
Difference between identifying and non-identifying relationships
In this article, we will discuss the overview of an entity and identifying and non-identifying relationships and finally will focus on differences between both of them. Let's discuss it one by one. Overview : Entity types that do not have their own key attributes are known as the weak entity type. Unlike this, there are regular entity types that ha
7 min read
Can Two Relationships be Related in an ER diagram?
Answer: Yes, two relationships in an ER diagram can be related through shared entities, linking tables, or generalization hierarchies.In traditional Entity-Relationship (ER) diagrams, relationships between entities are typically represented independently. However, interrelations between relationships can exist indirectly through shared entities, li
1 min read
Structural Constraints of Relationships in ER Model
Structural constraints, within the context of Entity-Relationship (ER) modeling, specify and determine how the entities take part in the relationships and this gives an outline of how the interactions between the entities can be designed in a database. Two primary types of constraints are cardinality (specifying the number of instances in a relatio
6 min read
Weak Entity Set in ER diagrams
An entity type should have a key attribute which uniquely identifies each entity in the entity set, but there exists some entity type for which key attribute can’t be defined. These are called Weak Entity type. The entity sets which do not have sufficient attributes to form a primary key are known as weak entity sets and the entity sets which have
2 min read
How to Convert ER Diagrams to Tables in DBMS?
Answer: Converting an ER diagram into tables involves a systematic process starting from table mapping to refining and reviewing.Step 1: Analyze the EntitiesIdentify each distinct entity in the diagram represented by a rectangle.Examine the attributes associated with each entity which are its characteristics represented by ellipses within the recta
1 min read
What is Mapping Cardinalities in ER Diagrams
In a Database Management System (DBMS), mapping cardinalities explains how entities (tables) in a database schema relate to one another. They provide the number of instances of one entity that can be linked to the number of instances of another entity via a certain kind of relationship, such as many-to-many, one-to-one, one-to-many, or one-to-many.
6 min read
Discrete Mathematics | Hasse Diagrams
Hasse Diagram A Hasse diagram is a graphical representation of the relation of elements of a partially ordered set (poset) with an implied upward orientation. A point is drawn for each element of the partially ordered set (poset) and joined with the line segment according to the following rules:   If p<q in the poset, then the point correspondin
10 min read
Minimization of ER Diagrams
Pre-Requisite: ER Diagram Entity-Relationship (ER) Diagram is a diagrammatic representation of data in databases, it shows how data is related to one another. In this article, we require previous knowledge of ER diagrams and how to draw ER diagrams. Minimization of ER Diagram simply means reducing the quantity of the tables in the ER Diagram. When
4 min read
MySQL | Recursive CTE (Common Table Expressions)
What is a CTE? In MySQL every query generates a temporary result or relation. In order to give a name to those temporary result set, CTE is used. A CTE is defined using WITH clause. Using WITH clause we can define more than one CTEs in a single statement. A CTE can be referenced in the other CTEs that are part of same WITH clause but those CTEs sho
5 min read
Recursive Descent Parser
Prerequisite - Construction of LL(1) Parsing Table, Classification of top down parsers Parsing is the process to determine whether the start symbol can derive the program or not. If the Parsing is successful then the program is a valid program otherwise the program is invalid. There are generally two types of Parsers: Top-Down Parsers: In this Pars
4 min read
Algorithm for non recursive Predictive Parsing
Prerequisite - Classification of Top Down Parsers Predictive parsing is a special form of recursive descent parsing, where no backtracking is required, so this can predict which products to use to replace the input string. Non-recursive predictive parsing or table-driven is also known as LL(1) parser. This parser follows the leftmost derivation (LM
4 min read
DBMS Tutorial – Learn Database Management System
Database Management System is a software or technology used to manage data from a database. Some popular databases are MySQL, Oracle, MongoDB, etc. DBMS provides many operations e.g. creating a database, Storing in the database, updating an existing database, delete from the database. DBMS is a system that enables you to store, modify, and retrieve
8 min read
Operating System Tutorial
An Operating System(OS) is software that manages and handles the hardware and software resources of a computer system. It provides interaction between users of computers and computer hardware. An operating system is responsible for managing and controlling all the activities and sharing of computer resources. An operating system is a low-level Soft
7 min read
SQL Query Interview Questions
SQL or Structured Query Language is a standard language for relational databases. SQL queries are powerful tools used to, manipulate, and manage data stored in these databases like MySQL, Oracle, PostgreSQL, etc. Whether you're fetching specific data points, performing complex analyses, or modifying database structures, SQL queries provide a standa
10 min read
Types of Operating Systems
An Operating System performs all the basic tasks like managing files, processes, and memory. Thus, the operating system acts as the manager of all the resources, i.e. resource manager. Thus, the operating system becomes an interface between the user and the machine. It is one of the most required software that is present in the device. Operating Sy
11 min read
TCP/IP Model
The TCP/IP model is a fundamental framework for computer networking. It stands for Transmission Control Protocol/Internet Protocol, which are the core protocols of the Internet. This model defines how data is transmitted over networks, ensuring reliable communication between devices. It consists of four layers: the Link Layer, the Internet Layer, t
13 min read
What is OSI Model? - Layers of OSI Model
OSI stands for Open Systems Interconnection , where open stands to say non-proprietary. It is a 7-layer architecture with each layer having specific functionality to perform. All these 7 layers work collaboratively to transmit the data from one person to another across the globe. The OSI reference model was developed by ISO - 'International Organiz
15+ min read
Types of Network Topology
Network topology refers to the arrangement of different elements like nodes, links, or devices in a computer network. It defines how these components are connected and interact with each other. Understanding various types of network topologies helps in designing efficient and robust networks. Common types include bus, star, ring, mesh, and tree top
12 min read
SQL Interview Questions
SQL is a standard database language used for accessing and manipulating data in databases. It stands for Structured Query Language and was developed by IBM Computer Scientists in the 1970s. By executing queries, SQL can create, update, delete, and retrieve data in databases like MySQL, Oracle, PostgreSQL, etc. Overall, SQL is a query language that
15+ min read
SQL Joins (Inner, Left, Right and Full Join)
SQL Join operation combines data or rows from two or more tables based on a common field between them. In this article, we will learn about Joins in SQL, covering JOIN types, syntax, and examples. SQL JOINSQL JOIN clause is used to query and access data from multiple tables by establishing logical relationships between them. It can access data from
5 min read
Types of Keys in Relational Model (Candidate, Super, Primary, Alternate and Foreign)
Keys are one of the basic requirements of a relational database model. It is widely used to identify the tuples(rows) uniquely in the table. We also use keys to set up relations amongst various columns and tables of a relational database. Different Types of Database Keys Candidate Key Primary Key Super Key Alternate Key Foreign Key Composite Key To
6 min read
Introduction of DBMS (Database Management System)
A database is a collection of interrelated data that helps in the efficient retrieval, insertion, and deletion of data from the database and organizes the data in the form of tables, views, schemas, reports, etc. For Example, a university database organizes the data about students, faculty, admin staff, etc. which helps in the efficient retrieval,
8 min read
ACID Properties in DBMS
This article is based on the concept of ACID properties in DBMS that are necessary for maintaining data consistency, integrity, and reliability while performing transactions in the database. Let's explore them. A transaction is a single logical unit of work that accesses and possibly modifies the contents of a database. Transactions access data usi
7 min read
Introduction of ER Model
Peter Chen developed the ER diagram in 1976. The ER model was created to provide a simple and understandable model for representing the structure and logic of databases. It has since evolved into variations such as the Enhanced ER Model and the Object Relationship Model The Entity Relational Model is a model for identifying entities to be represent
10 min read
Normal Forms in DBMS
Normalization is the process of minimizing redundancy from a relation or set of relations. Redundancy in relation may cause insertion, deletion, and update anomalies. So, it helps to minimize the redundancy in relations. Normal forms are used to eliminate or reduce redundancy in database tables. Normalization of DBMSIn database management systems (
12 min read
Article Tags :