riven

Riven

Riven

hierarchical database model

The hierarchical database model is one of the oldest types of database models, primarily characterized by its tree-like structure. In this model, data is organized into a hierarchy, resembling a parent-child relationship. Each parent can have multiple children, but each child can only have one parent. This structure is well-suited for applications where the relationships between data are clear and fixed.

Key Characteristics

  1. Tree Structure: Data is organized into a tree with nodes representing records.
  2. Parent-Child Relationships: Each child node has exactly one parent node, establishing a clear hierarchy.
  3. Data Redundancy: The hierarchical model can lead to redundancy if data needs to be repeated across multiple nodes.
  4. Navigational Access: The data access method is primarily navigational, where users traverse the hierarchy to reach specific data.

Hierarchical Model Representation

To represent a hierarchical database model, we often use tree diagrams. Below is a simple example of a hierarchical structure for a company.

hierarchical database model

Hierarchical Database Management Systems (DBMS)

While SQL databases can simulate a hierarchical model, some dedicated hierarchical DBMS include:

  • IBM Information Management System (IMS): One of the most well-known hierarchical DBMS.
  • Windows Registry: A hierarchical database used by Windows to store configuration settings.

Use Cases for Hierarchical Database Model

  1. Organizational Structures: Ideal for representing company structures where departments and employees have a clear hierarchy.
  2. File Systems: Hierarchical models are perfect for file systems where directories and files have parent-child relationships.
  3. XML and JSON: These data formats are often hierarchical and can be effectively modeled using this approach.

Example Scenario

Let’s consider a company database that includes information about departments and employees. We will define the structure, create tables, and perform operations using SQL.

Table Structure

In a relational database, the hierarchical model can be simulated using tables with foreign key relationships. Here’s how we might structure our tables for the company example:

  1. Department Table

    • DepartmentID (Primary Key)
    • DepartmentName
    • ManagerID (Foreign Key referencing Employee)
  2. Employee Table

    • EmployeeID (Primary Key)
    • EmployeeName
    • DepartmentID (Foreign Key referencing Department)

SQL Table Creation

				
					CREATE TABLE Department (
    DepartmentID INT PRIMARY KEY,
    DepartmentName VARCHAR(100),
    ManagerID INT,
    FOREIGN KEY (ManagerID) REFERENCES Employee(EmployeeID)
);

CREATE TABLE Employee (
    EmployeeID INT PRIMARY KEY,
    EmployeeName VARCHAR(100),
    DepartmentID INT,
    FOREIGN KEY (DepartmentID) REFERENCES Department(DepartmentID)
);
				
			

Inserting Data

Now, let’s populate the tables with sample data.

				
					-- Inserting Employees
INSERT INTO Employee (EmployeeID, EmployeeName) VALUES (1, 'Alice');
INSERT INTO Employee (EmployeeID, EmployeeName) VALUES (2, 'Bob');
INSERT INTO Employee (EmployeeID, EmployeeName) VALUES (3, 'Charlie');
INSERT INTO Employee (EmployeeID, EmployeeName) VALUES (4, 'Diana');
INSERT INTO Employee (EmployeeID, EmployeeName) VALUES (5, 'Eve');

-- Inserting Departments
INSERT INTO Department (DepartmentID, DepartmentName, ManagerID) VALUES (1, 'Department A', 1);
INSERT INTO Department (DepartmentID, DepartmentName, ManagerID) VALUES (2, 'Department B', 2);
INSERT INTO Department (DepartmentID, DepartmentName, ManagerID) VALUES (3, 'Department C', 1);

-- Assigning Employees to Departments
UPDATE Employee SET DepartmentID = 1 WHERE EmployeeID IN (1, 2);
UPDATE Employee SET DepartmentID = 2 WHERE EmployeeID IN (3, 4);
UPDATE Employee SET DepartmentID = 3 WHERE EmployeeID = 5;
				
			

Hierarchical Queries

In hierarchical databases, certain queries become necessary to traverse the hierarchy. For instance, to find all employees under a specific department, we can use a recursive Common Table Expression (CTE) in SQL:

				
					WITH RECURSIVE EmployeeHierarchy AS (
    SELECT EmployeeID, EmployeeName, DepartmentID
    FROM Employee
    WHERE DepartmentID = 1  -- Start with Department A

    UNION ALL

    SELECT e.EmployeeID, e.EmployeeName, e.DepartmentID
    FROM Employee e
    JOIN EmployeeHierarchy eh ON e.DepartmentID = eh.DepartmentID
)
SELECT * FROM EmployeeHierarchy;
				
			

Advantages

  • Simplicity: The hierarchical model is simple and easy to understand, especially for applications with a clear hierarchy.
  • Performance: Accessing data can be very fast due to the predefined paths and relationships.
  • Data Integrity: The parent-child relationship enforces data integrity, as children must belong to parents.

Disadvantages

  • Rigidity: The hierarchical model can be inflexible, as changes to the hierarchy require reorganization of the entire structure.
  • Complex Queries: Complex queries can be challenging, especially if the data relationships are not straightforward.
  • Limited Relationships: The model primarily supports one-to-many relationships, making it difficult to represent many-to-many relationships
Previous
Next
SQL-HAVING clause
SQL- HAVING clause The HAVING clause in SQL is an essential tool for filtering results after grouping...
SQL ROW_NUMBER()
What is SQL ROW_NUMBER() The ROW_NUMBER() function is part of the SQL window functions category. It generates...
SQL CROSS JOIN
SQL CROSS JOIN  A CROSS JOIN is a type of join that returns the Cartesian product of two tables. The...
Network Model
Network model (DBMS) The network model is a data model that organizes data in a graph structure, where...
Database Engines
Database Engines What is a Database Engine? A database engine is the underlying component of a database...
SQL-EXCEPT operator
What is SQL EXCEPT operator? The EXCEPT operator allows you to subtract one result set from another....
Relational Database In SQL
What is Relational database A relational database is a type of database that stores data in tables. Each...
SQL-COUNT function
What is SQL COUNT function? The COUNT function is an aggregate function that returns the number of rows...
SQL-Stored Procedure
SQL stored procedure   What is  Stored Procedure? A stored procedure is a set of SQL statements that...
SQL SELECT statement
SQL SELECT statement The SELECT statement is fundamental to SQL (Structured Query Language) and is used...