Sub topic 11
Handling Cycles in the Data
Why cycles matter
A hierarchical table stores each row's parent key in the same table. In an employee table, manager_id points to another employee's employee_id. A valid organization chart eventually reaches a root whose manager_id is NULL.
A cycle breaks that rule. For example, employee 100 reports to 200, employee 200 reports to 300, and employee 300 reports back to 100. Following managers or reports can then return to a row already visited.
This chapter assumes that you can write START WITH and CONNECT BY PRIOR queries and understand LEVEL.
The default behavior
Without cycle protection, Oracle detects a loop while evaluating a hierarchical query and raises an error instead of returning an endlessly repeated sequence. This is useful because it exposes invalid data, but it can interrupt a report or application query.
The data should still be corrected. Cycle handling controls the query's behavior; it does not repair the table.
Stop safely with NOCYCLE
Add NOCYCLE after CONNECT BY to tell Oracle to stop following a branch when it would revisit an ancestor. CONNECT_BY_ISCYCLE then identifies the row at the cycle boundary.
SELECT employee_id,
manager_id,
LEVEL,
CONNECT_BY_ISCYCLE AS is_cycle
FROM employees
START WITH employee_id = 100
CONNECT BY NOCYCLE PRIOR employee_id = manager_id;Here, PRIOR employee_id = manager_id walks downward. The employee from the current row becomes the manager of the next row. The query starts at employee 100 and follows reports.
CONNECT_BY_ISCYCLE returns:
0for a row that does not close a cycle.1for a row whose next child would lead back to an ancestor.
The cycle-marked row is returned once, but Oracle does not continue through the repeating path.
Finding and fixing the bad relationship
Add a path to make the problem easier to inspect.
SELECT employee_id,
manager_id,
SYS_CONNECT_BY_PATH(TO_CHAR(employee_id), '/') AS employee_path,
CONNECT_BY_ISCYCLE AS is_cycle
FROM employees
START WITH employee_id = 100
CONNECT BY NOCYCLE PRIOR employee_id = manager_id;The path shows the sequence Oracle followed. If the final relationship points back to an earlier employee, inspect that employee's manager_id values and correct the relationship according to the business rules.
A cycle can also occur in a bottom-up query. The direction changes, but NOCYCLE works the same way:
SELECT employee_id,
manager_id,
LEVEL,
CONNECT_BY_ISCYCLE AS is_cycle
FROM employees
START WITH employee_id = 100
CONNECT BY NOCYCLE employee_id = PRIOR manager_id;Use NOCYCLE when the data may be imperfect and the query must complete. For administrative validation, you may omit it so that Oracle's error exposes the invalid hierarchy. In production, combine protection with constraints, application checks, or a validation job that prevents a manager relationship from creating a loop.
Exercise
What does NOCYCLE do, and what does CONNECTBYISCYCLE = 1 tell you?
Show answer
NOCYCLE stops Oracle from continuing along a branch when it would revisit an ancestor. CONNECTBYISCYCLE = 1 marks the row where the next relationship would close a cycle.