trees and hierarchies in sql
DESCRIPTION
TRANSCRIPT
![Page 1: Trees and Hierarchies in SQL](https://reader033.vdocuments.site/reader033/viewer/2022042601/5492bb1db47959514d8b467f/html5/thumbnails/1.jpg)
Trees and Hierarchies in SQLby Eduard Hildebrandt
![Page 2: Trees and Hierarchies in SQL](https://reader033.vdocuments.site/reader033/viewer/2022042601/5492bb1db47959514d8b467f/html5/thumbnails/2.jpg)
Think about…• Categories• Organisation• Threads• Folders• Components• …
In most projects we have to deal withsome kind of trees or hierarchies!
![Page 3: Trees and Hierarchies in SQL](https://reader033.vdocuments.site/reader033/viewer/2022042601/5492bb1db47959514d8b467f/html5/thumbnails/3.jpg)
How can we store an retrieve trees and hierarchieseffenciently from relational databases?
![Page 4: Trees and Hierarchies in SQL](https://reader033.vdocuments.site/reader033/viewer/2022042601/5492bb1db47959514d8b467f/html5/thumbnails/4.jpg)
Agenda
• Adjacency List Model• Closure Table Model• Path Enumeration Model• David Chandler Model• Modified David Chandler Model• Nested Set Model• Hyprid Model• Frequent Insertion in Nested Sets
![Page 5: Trees and Hierarchies in SQL](https://reader033.vdocuments.site/reader033/viewer/2022042601/5492bb1db47959514d8b467f/html5/thumbnails/5.jpg)
B
D E
C
F G
A
H
I
What is a „tree“?
A tree is a connected, undirected, acyclic grap.
![Page 6: Trees and Hierarchies in SQL](https://reader033.vdocuments.site/reader033/viewer/2022042601/5492bb1db47959514d8b467f/html5/thumbnails/6.jpg)
B
D E
C
F G
A
H
I
What is a „tree“?
A tree is a connected, undirected, acyclic grap.
X
![Page 7: Trees and Hierarchies in SQL](https://reader033.vdocuments.site/reader033/viewer/2022042601/5492bb1db47959514d8b467f/html5/thumbnails/7.jpg)
B
D E
C
F G
A
H
I
What is a „tree“?
A tree is a connected, undirected, acyclic grap.
![Page 8: Trees and Hierarchies in SQL](https://reader033.vdocuments.site/reader033/viewer/2022042601/5492bb1db47959514d8b467f/html5/thumbnails/8.jpg)
B
D E
C
F G
A
H
I
What is a „tree“?
A tree is a connected, undirected, acyclic grap.
![Page 9: Trees and Hierarchies in SQL](https://reader033.vdocuments.site/reader033/viewer/2022042601/5492bb1db47959514d8b467f/html5/thumbnails/9.jpg)
B
D E
C
F G
A
H
Ititle parent
A null
B A
C A
D B
E B
F C
G C
H C
I F
CREATE TABLE nodes
(title VARCHAR(255) NOT NULL PRIMARY KEY,
parent VARCHAR(255));
SELECT *
FROM nodes
WHERE parent IS NULL;
SELECT node1.*
FROM nodes AS node1
LEFT JOIN node AS node2 ON node1.title = node2.parent
WHERE node2.title = ‘A’;
Finding the root node:
Finding the leaf nodes:
Creating the table:
Adjacency List Model
![Page 10: Trees and Hierarchies in SQL](https://reader033.vdocuments.site/reader033/viewer/2022042601/5492bb1db47959514d8b467f/html5/thumbnails/10.jpg)
B
D E
C
F G
A
H
I
UPDATE nodes
SET title = ‘X’
WHERE title = ‘C’;
START TRANSACTION;
UPDATE nodes
SET title = ‘X’
WHERE title = ‘C’;
UPDATE nodes
SET parent = ‘X’
WHERE parent = ‘C’;
COMMIT;
title parent
A null
B A
C A
D B
E B
F C
G C
H C
I F
Adjacency List ModelUPDATE anomalies
![Page 11: Trees and Hierarchies in SQL](https://reader033.vdocuments.site/reader033/viewer/2022042601/5492bb1db47959514d8b467f/html5/thumbnails/11.jpg)
XINSERT INTO nodes (title, parent) VALUES
(‘X’, ‘Y’),
(‘Y’, ‘X’);
Adjacency List Model
Y
INSERT INTO nodes (title, parent) VALUES
(‘X’, ‘X’);X
INSERT anomalies
CHECK (title <> parent)
UNIQUE (title, parent)
CHECK ((SELECT COUNT(*) FROM nodes WHERE parent IS NULL) = 1);
One root only:
Node can not be it‘s own parent:
Prevent double connections:
CHECK ((SELECT COUNT(*) FROM nodes) – 1
= SELECT COUNT(parent) FROM nodes;
Connected graph (edges = nodes – 1):
ALTER TABLE nodes ADD CONSTRAINT parent_fk FOREIGN KEY (parent)
REFERENCES nodes (id);
Parent must exist:
![Page 12: Trees and Hierarchies in SQL](https://reader033.vdocuments.site/reader033/viewer/2022042601/5492bb1db47959514d8b467f/html5/thumbnails/12.jpg)
B
D E
C
F G
A
H
I
DELETE FROM nodes WHERE title = ‘C’;
Adjacency List ModelDELETE anomalies
![Page 13: Trees and Hierarchies in SQL](https://reader033.vdocuments.site/reader033/viewer/2022042601/5492bb1db47959514d8b467f/html5/thumbnails/13.jpg)
B
D E F G
A
H
I
Adjacency List Model
What happens with nodes F, G, H and I?
DELETE anomalies
?
ALTER TABLE nodes ADD CONSTRAINT parent_fk FOREIGN KEY (parent)
REFERENCES nodes (id)
ON DELETE ...;
![Page 14: Trees and Hierarchies in SQL](https://reader033.vdocuments.site/reader033/viewer/2022042601/5492bb1db47959514d8b467f/html5/thumbnails/14.jpg)
B
D E
C
F G
A
H
I
Adjacency List ModelDELETE anomalies
Solution 1: delete all children
![Page 15: Trees and Hierarchies in SQL](https://reader033.vdocuments.site/reader033/viewer/2022042601/5492bb1db47959514d8b467f/html5/thumbnails/15.jpg)
B
D E
F G
A
H
I
Adjacency List ModelDELETE anomalies
Solution 2: move children to the level of the parent
![Page 16: Trees and Hierarchies in SQL](https://reader033.vdocuments.site/reader033/viewer/2022042601/5492bb1db47959514d8b467f/html5/thumbnails/16.jpg)
B
D E
F
G
A
HI
Adjacency List ModelDELETE anomalies
Solution 3: replace parent with the first child
![Page 17: Trees and Hierarchies in SQL](https://reader033.vdocuments.site/reader033/viewer/2022042601/5492bb1db47959514d8b467f/html5/thumbnails/17.jpg)
B
D E
C
F G
A
H
Ititle parent
A A
B A
C A
D B
E B
F C
G C
H C
I F
CREATE TABLE nodes
(title VARCHAR(255) NOT NULL PRIMARY KEY,
parent VARCHAR(255) NOT NULL);
Creating the table:
Adjacency List Modelwith self -references
Avoids NULL in the parent column!
![Page 18: Trees and Hierarchies in SQL](https://reader033.vdocuments.site/reader033/viewer/2022042601/5492bb1db47959514d8b467f/html5/thumbnails/18.jpg)
B
D E
C
F G
A
H
Ititle path
A A
B A/B
C A/C
D A/B/D
E A/B/E
F A/C/F
G A/C/G
H A/C/H
I A/C/F/I
CREATE TABLE nodes
(title VARCHAR(255) NOT NULL PRIMARY KEY
-- don’t use a separater in primary key
CHECK (REPLACE (title, ‘/’, ‘’) = title,
path VARCHAR(1024) NOT NULL);
Path Enumeration Model
Creating the table:
![Page 19: Trees and Hierarchies in SQL](https://reader033.vdocuments.site/reader033/viewer/2022042601/5492bb1db47959514d8b467f/html5/thumbnails/19.jpg)
B
D E
C
F G
A
H
ID parent
1 A
2 B
3 C
4 D
5 E
6 F
7 G
8 H
Closure Table Model
Ancestor Descendant
1 2
1 4
1 5
2 4
2 5
1 3
1 6
1 7
1 8
3 6
![Page 20: Trees and Hierarchies in SQL](https://reader033.vdocuments.site/reader033/viewer/2022042601/5492bb1db47959514d8b467f/html5/thumbnails/20.jpg)
B
D E
C
F G
A
H
ID T L1 L2 L3
1 A 1 0 0
2 B 1 1 0
3 C 1 2 0
4 D 1 1 1
5 E 1 1 2
6 F 1 2 1
7 G 1 2 2
8 H 1 2 3
CREATE TABLE nodes
(id INTEGER NOT NULL PRIMARY KEY
title VARCHAR(255) NOT NULL
L1 INTEGER NOT NULL,
L2 INTEGER NOT NULL,
L3 INTEGER NOT NULL);
David Chandler Model
Creating the table:
Algorithm is patened by David Chandler!
Hierarchy level is fixed!
![Page 21: Trees and Hierarchies in SQL](https://reader033.vdocuments.site/reader033/viewer/2022042601/5492bb1db47959514d8b467f/html5/thumbnails/21.jpg)
B
D E
C
F G
A
H
ID T L1 L2 L3
1 A 1 0 0
2 B 1 2 0
3 C 1 3 0
4 D 1 2 4
5 E 1 2 5
6 F 1 3 6
7 G 1 3 7
8 H 1 3 8
CREATE TABLE nodes
(id INTEGER NOT NULL PRIMARY KEY
title VARCHAR(255) NOT NULL
L1 INTEGER NOT NULL,
L2 INTEGER NOT NULL,
L3 INTEGER NOT NULL);
Modified David Chandler Model
Creating the table:
Not sure about implecations of David Chandlers patent…
Hierarchy level is fixed!
![Page 22: Trees and Hierarchies in SQL](https://reader033.vdocuments.site/reader033/viewer/2022042601/5492bb1db47959514d8b467f/html5/thumbnails/22.jpg)
B2 7
D3 4 E5 6
C8 17
F9 12 G13 14
A1 18
H15 16
I10 11
Nested Set Model
title lft rgt
A 1 18
B 2 7
C 8 17
D 3 4
E 5 6
F 9 12
G 13 14
H 15 16
I 10 11
CREATE TABLE nodes
(title VARCHAR(255) NOT NULL PRIMARY KEY,
lft INTEGER NOT NULL,
rgt INTEGER NOT NULL);
Creating the table:
SELECT *
FROM nodes
WHERE lft = 1;
SELECT *
FROM nodes
WHERE lft = (MAX(rgt) – 1)
Finding the root node:
Finding the leaf nodes:
![Page 23: Trees and Hierarchies in SQL](https://reader033.vdocuments.site/reader033/viewer/2022042601/5492bb1db47959514d8b467f/html5/thumbnails/23.jpg)
B2 7
D3 4 E5 6
C8 17
F9 12 G13 14
A1 18
H15 16
I10 11
Nested Set Model
title lft rgt
A 1 18
B 2 7
C 8 17
D 3 4
E 5 6
F 9 12
G 13 14
H 15 16
I 10 11
SELECT * FROM nodes
WHERE lft BETWEEN lft AND rgt
ORDER BY lft ASC;
Finding subtrees:
Finding path to a node:
SELECT * FROM nodes
WHERE lft < ? AND rgt > ?
ORDER BY lft ASC;
![Page 24: Trees and Hierarchies in SQL](https://reader033.vdocuments.site/reader033/viewer/2022042601/5492bb1db47959514d8b467f/html5/thumbnails/24.jpg)
Hybrid Models
Combining models
Adjacency List
Path Enumeration
David Chandler
Nested Set
Closure Table
![Page 25: Trees and Hierarchies in SQL](https://reader033.vdocuments.site/reader033/viewer/2022042601/5492bb1db47959514d8b467f/html5/thumbnails/25.jpg)
B2 49
D
3 24
E
48
C50
F
51 72
G
73 14
A1 100
H
15 16
Nested Set Model
Problem: Tree must be reorganized on every insertion (table lock).
Possible solution: Avoid overhead with larger spreads and bigger gaps
99
25
![Page 26: Trees and Hierarchies in SQL](https://reader033.vdocuments.site/reader033/viewer/2022042601/5492bb1db47959514d8b467f/html5/thumbnails/26.jpg)
Nested Set Model
What datatype shout I use for left and right values?
INTEGER
FLOAT / REAL / DOUBLE
NUMERIC / DECIMAL
![Page 27: Trees and Hierarchies in SQL](https://reader033.vdocuments.site/reader033/viewer/2022042601/5492bb1db47959514d8b467f/html5/thumbnails/27.jpg)
Nested Set Modelwith rational numbers
B C
A0/5 5/5
1/5 2/5 4/53/5
a b
a+(b-a)/3 a+2(b-a)/3
![Page 28: Trees and Hierarchies in SQL](https://reader033.vdocuments.site/reader033/viewer/2022042601/5492bb1db47959514d8b467f/html5/thumbnails/28.jpg)
Nested Set Modelwith rational numbers
B C
A0/5 5/5
1/5 2/5 4/53/5
D 17/2516/25
T ln ld rn rd
A 0 5 5 5
B 1 5 2 5
C 3 5 4 5
D 16 25 17 25
Adding an aditional child node is alway possible.No need to reorganize the table!
![Page 29: Trees and Hierarchies in SQL](https://reader033.vdocuments.site/reader033/viewer/2022042601/5492bb1db47959514d8b467f/html5/thumbnails/29.jpg)
Nested Set ModelBinary Fraction
node X Y
1 1 1/2
1.1 1 3/4
1.1.1 1 7/8
1.1.1.1 1 15/16
1.1.2 7/8 13/16
1.1.3 13/16 25/32
1.2 3/4 5/8
1.2.1 3/4 11/16
1.3 5/8 9/16
![Page 30: Trees and Hierarchies in SQL](https://reader033.vdocuments.site/reader033/viewer/2022042601/5492bb1db47959514d8b467f/html5/thumbnails/30.jpg)
Literature
Titel: Trees and Hierarchies in SQLAutor: Joe CelkoVerlag: Morgan KaufmannISBN: 1558609202
![Page 31: Trees and Hierarchies in SQL](https://reader033.vdocuments.site/reader033/viewer/2022042601/5492bb1db47959514d8b467f/html5/thumbnails/31.jpg)
Q&A
![Page 32: Trees and Hierarchies in SQL](https://reader033.vdocuments.site/reader033/viewer/2022042601/5492bb1db47959514d8b467f/html5/thumbnails/32.jpg)