8.1 Representing Hierarchical InformationLet's look at an example to understand how we can represent hierarchical information in a relational database. As a basis for the example, we'll use an organization chart showing how one employee reports to another within a large organization, as shown in Figure 8-1. Figure 8-1. An organization chartFigure 8-1 represents a hierarchy of employees. The information regarding an employee, his manager, and the reporting relationship need to be represented in one table, EMPLOYEE, as shown in the Entity Relationship Diagram in Figure 8-2. Figure 8-2. Entity Relationship Diagram of the EMPLOYEE tableIn Figure 8-2, the EMPLOYEE table refers to itself. The column MANAGER_EMP_ID refers to the EMP_ID column of the same table. To represent hierarchical data, we need to make use of a relationship such as when one column of a table references another column of the same table. When such a relationship is implemented using a database constraint, it is known as self-referential integrity constraint . The corresponding CREATE TABLE statement will look as follows: CREATE TABLE EMPLOYEE ( EMP_ID NUMBER (4) CONSTRAINT EMP_PK PRIMARY KEY, FNAME VARCHAR2 (15)NOT NULL, LNAME VARCHAR2 (15)NOT NULL, DEPT_ID NUMBER (2)NOT NULL, MANAGER_EMP_ID NUMBER (4) CONSTRAINT EMP_FK REFERENCES EMPLOYEE(EMP_ID), SALARY NUMBER (7,2)NOT NULL, HIRE_DATE DATENOT NULL, JOB_ID NUMBER (3)); As a basis for the examples in this chapter, we'll use the following sample data: SELECT EMP_ID, LNAME, DEPT_ID, MANAGER_EMP_ID, SALARY, HIRE_DATE FROM EMPLOYEE; EMP_ID LNAME DEPT_ID MANAGER_EMP_ID SALARY HIRE_DATE --------- ---------- --------- -------------- --------- --------- 7369 SMITH 20 7902 800 17-DEC-80 7499 ALLEN 30 7698 1600 20-FEB-81 7521 WARD 30 7698 1250 22-FEB-81 7566 JONES 20 7839 2000 02-APR-81 7654 MARTIN 30 7698 1250 28-SEP-81 7698 BLAKE 30 7839 2850 01-MAY-80 7782 CLARK 10 7839 2450 09-JUN-81 7788 SCOTT 20 7566 3000 19-APR-87 7839 KING 10 5000 17-NOV-81 7844 TURNER 30 7698 1500 08-SEP-81 7876 ADAMS 20 7788 1100 23-MAY-87 7900 JAMES 30 7698 950 03-DEC-81 7902 FORD 20 7566 3000 03-DEC-81 7934 MILLER 10 7782 1300 23-JAN-82 The EMPLOYEE table has two important aspects to be aware of:
The column MANAGER_EMP_ID stores the EMP_ID of the employee's manager. For example, The MANAGER_EMP_ID for Smith is 7902, which means that Ford is Smith's manager. The employee King doesn't have a MANAGER_EMP_ID, which indicates that King is the uppermost employee. To be able to represent the uppermost employee, the MANAGER_EMP_ID column must be NULLABLE. There is a foreign key constraint on the MANAGER_EMP_ID column. This enforces the rule that any value we put in the MANAGER_EMP_ID column must be the EMP_ID of a valid employee. Such a constraint is not mandatory when representing hierarchical information. However, it is a good practice to define database constraints to enforce such business rules. Before moving on to the following sections on manipulating hierarchies, we will introduce some hierarchy terminology. The following list defines terms that we'll use often when working with hierarchical data:
|