Sub topic 1
Tree Data in a Self-Referencing Table
Hierarchical data has parent-child relationships. An employee reports to a manager, a folder contains a subfolder, and a product category contains smaller categories. Each row stores one item and a reference to its parent.
A self-referencing table is a table whose foreign key points back to the primary key in the same table. In an employee table, employee_id identifies an employee, while manager_id identifies that employee's manager.
Prerequisites
You should know basic SELECT, WHERE, and ORDER BY clauses. You should also understand primary keys, foreign keys, self-joins, and Oracle's DUAL table. This chapter assumes that you can read a table definition and identify the parent and child columns.
Here is a small employee table:
CREATE TABLE employees (
employee_id NUMBER PRIMARY KEY,
last_name VARCHAR2(50) NOT NULL,
manager_id NUMBER,
CONSTRAINT employees_manager_fk
FOREIGN KEY (manager_id)
REFERENCES employees (employee_id)
);
INSERT INTO employees (employee_id, last_name, manager_id)
VALUES (100, 'King', NULL);
INSERT INTO employees (employee_id, last_name, manager_id)
VALUES (101, 'Kochhar', 100);
INSERT INTO employees (employee_id, last_name, manager_id)
VALUES (102, 'De Haan', 100);
INSERT INTO employees (employee_id, last_name, manager_id)
VALUES (103, 'Hunold', 102);
COMMIT;manager_id is nullable because the top-level employee has no manager in this table. That row is called a root. A row that manages other rows is a parent. A row that reports to another employee is a child. A row with no direct reports is a leaf.
The relationships look like this:
The table does not store a complete path such as King/De Haan/Hunold. It stores only each row's immediate parent. Oracle can derive the full tree by repeatedly matching a parent's employee_id with a child's manager_id.
This design is called an adjacency list. It is simple to update: moving an employee to a different manager requires changing one manager_id value. It also supports multiple roots because more than one row may have a null manager_id.
The foreign key prevents an employee from naming a manager that does not exist. However, it does not prevent every logical problem. An employee could be assigned as their own manager if an additional rule does not reject it. Longer loops can also occur if existing relationships are changed carelessly. Hierarchical queries need explicit cycle handling when the data might contain such problems.
You can inspect the immediate relationships with an ordinary self-join:
SELECT child.last_name AS employee,
parent.last_name AS manager
FROM employees child
LEFT JOIN employees parent
ON parent.employee_id = child.manager_id
ORDER BY child.employee_id;This query shows only one parent level. It does not show De Haan's manager and then Hunold's manager as a full chain. Oracle's START WITH and CONNECT BY clauses are designed to follow these relationships across multiple levels.
Exercise
In the sample data, which row is the root, which row is a parent and a child, and which row is a leaf?
Show answer
King is the root. De Haan is both a child of King and a parent of Hunold. Hunold is a leaf.