Questions tagged [transitive-closure-table]

A transitive closure table is a method for storing hierarchical information in a flat database. It supports referential integrity like adjacency list, but also supports querying hierarchies of arbitrary depth like nested sets.

A transitive closure table is a method for storing a hierarchical graph in a flat database. It supports referential integrity like adjacency lists, but also supports querying hierarchies of arbitrary depth like nested sets (also known as MPTT), smoothing out some of the difficulties inherent in each in exchange for storage space.

Example hierarchy

Let's start out with a tree like this (IDs are in bold):

  • 1 Pet shop
    • 2 Fish
      • 3 Zebrafish
      • 4 Tilapia
    • 5 Rodents
      • 6 Gerbil
      • 7 Squirrels
        • 8 Flying squirrel
        • 9 Red squirrel
    • 10 Birds
      • 11 Cardinal

Closure table

You need a separate table to store your hierarchical information, unlike either adjacency lists or nested sets. For each node, you store the entire path as a set of individual records. For example, 'Squirrels' (node 7) has the following records:

| ancestor | descendant |
-------------------------
| 1        | 7          |
| 5        | 7          |
| 7        | 7          |
| 7        | 8          |
| 7        | 9          |

You'll note a few things:

  • All the ancestors of node 7 have their own record, so you can find out the path of a node just by going SELECT * FROM closure WHERE descendant = 7 (if you're storing your records in SQL).
  • All the direct and indirect descendants can be retrieved as well, by going SELECT * FROM closure WHERE ancestor = 7.
  • Each node is an ancestor and descendant of itself.

Pros

  • Just as with nested sets, an entire path or tree can be retrieved quite simply with one query.
  • Just as with adjacency lists, referential integrity is preserved.
  • Write queries are quite fast and straightforward -- much simpler than nested sets.

Cons

  • Still more complex to understand than adjacency lists.
  • Sacrifices storage space for efficiency.
  • Requires a separate table (although a database that supports triggers can minimize the maintenance cost of this issue).

References

90 questions
1
vote
3 answers

How do I query for all the nodes between two nodes in a tree?

I have a hierarchical database strucutre, e.g. columns ID and PARENT_ID defined for each row, with the top level rows having a NULL PARENT_ID. I have all the relationships from this table flattened into another table, e.g. if there were three…
1
vote
0 answers

What is the best way to limit a query with sorted results on a Closure table with a depth field in MySQL?

Researching hierarchical data persistence and led me to closure tables and pieced together this comment structure based off of the culmination of said research. Queries for creating new nodes in the closure table were easy enough for me to grasp…
Billy
  • 886
  • 8
  • 18
1
vote
1 answer

transitive-closure-table example

I know that transitive-closure concept is used for storing tree structures data. This concept also used to retrieve hierarchical data in very efficient and quickly with minimum complex query. In SQLite Query browser, I have tried these…
Durgesh Patel
  • 1,035
  • 8
  • 15
1
vote
1 answer

Run SQL query from an Access Data Macro

I'm trying to update a closure table using data macros, which requires running the following code whenever a new record is inserted into the main table: INSERT INTO tblClosure (parent_id, child_id, depth) SELECT p.parent_id, c.child_id,…
1
vote
0 answers

How to return a record based on parent child relationship

I have a requirement wherein I need to select Parent Department for a particular position. I have the tables as public partial class Position { public int PositionId { get; set; } public string PositionName { get; set; } public int…
user4867718
  • 31
  • 1
  • 5
1
vote
1 answer

What query would I use to obtain sibling records when using closure tables?

If I have the following schema & data and am employing the closure table pattern: +----+----------+------------+--------+ | id | ancestor | descendant | length | +----+----------+------------+--------+ | 1 | 2 | 2 | 0 | | 2 |…
Alexander Trauzzi
  • 7,277
  • 13
  • 68
  • 112
1
vote
0 answers

JQuery plugin to display and edit Trees using a closure table

I have a tree structure in an SQL database and I'm looking to display and edit it via web pages using Javascript, PHP and MYSQL. I can't seem to find any plugins that meet my needs. I would like to be able to add new nodes into the tree, edit and…
Guy
  • 876
  • 1
  • 10
  • 28
1
vote
0 answers

Closure Table for users\groups management?

any of you have ever used Closure Table to represent the hierarchy users groups ? In all the examples I found online the id of the node is always unique within the structure. Try to imagine this issue : In a classic security management in my…
Luca
  • 11
  • 2
1
vote
0 answers

Finding closures in a DB table of a given depth with SQLite

I have the following table: ( I am using sqlite) .schema T CREATE TABLE T (i TEXT, j TEXT, v INTEGER); select * from T; i |j |v x |y |1 x |z |1 y |a |1 z |b |1 a |c |1 c |d |1 I need to find the closure of a particular i to a given depth.…
1
vote
0 answers

Closure Tables - Is this enough data to display a tree view?

Here is the table I have created by testing the closure table method. | id | parentId | childId | hops | | | | | 270 | 6 | 6 | 0 | 271 | 7 …
1
vote
1 answer

MySql closure table won't support duplicate sub-category for different parents

I have been going around in circles with this closure table for awhile. The problem I have is with the second occurrence of a descendant. I have instances of sub-categories that appear in more than one parent category. I have reverted to this…
Rob
  • 357
  • 1
  • 3
  • 13
1
vote
1 answer

Unique name constraint accross siblings in a closure tree

I'm wondering how to enforce a unique constraint for the names of sibling nodes when using a closure table in MySQL to model a hierarchy. This is my schema: create table spaces ( id int not null, name varchar(50) not null, parent int not…
0
votes
1 answer

update table based on subquery of table

I have am using a closure table for some page heirarchy. I want to be able to delete a page and update the level of the children it leaves. par child level 1 1 0 1 2 1 2 2 0 1 3 2 2 3 1 3 3 0 1 4 3 2 4 …
Ian Wood
  • 6,515
  • 5
  • 34
  • 73
0
votes
1 answer

What combination of select query and SQL triggers will return each node's ancestry and depth, with triggers maintaining the closure table?

Given a "location" table and a location_closure table which stores the depth between the various nodes in location tree(s), what combination of select query and SQL triggers will consistently return a result set containing each node's ancestry and…
0
votes
1 answer

How to turn this cursor operation into a set based operation on a closure hierarchy?

Trees.DirectReports is a closure (hierarchy) table. There is a table called Users with: RowID, EmployeeId, and MangerId. @RowCount is the number of records in this table and #EmpMgr is a cursor for this table. Below is the relevant sql code that I…