RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
Create and Maintain COMPLEX HIERARCHIES easily
1. CREATE AND MAITAIN MANY-MANY HIERARCHY (SQL CODE with DATASET provided)
1 | P a g e
www.db-tools.com https://www.linkedin.com/in/sreenivasulu-kandakuru-72687a25
CREATE AND MAINTAIN COMPLEX HIERARCHYS
This article is dedicated to my GURU Bill Karwin. He had published a best article
in the whole web about building hierarchy. Yes building hierarchy which is a holy grail
for Data Architects / Data Modeler / Data Scientists / Data Analysts..
2. CREATE AND MAITAIN MANY-MANY HIERARCHY (SQL CODE with DATASET provided)
2 | P a g e
www.db-tools.com https://www.linkedin.com/in/sreenivasulu-kandakuru-72687a25
Link published by Bill https://www.slideshare.net/billkarwin/models-for-hierarchical-data
3. CREATE AND MAITAIN MANY-MANY HIERARCHY (SQL CODE with DATASET provided)
3 | P a g e
www.db-tools.com https://www.linkedin.com/in/sreenivasulu-kandakuru-72687a25
I will talk ONLY about the Closure Table Design which is the best design for updating
/deleting and as well as maintaining the hierarchy (highlighted below)
4. CREATE AND MAITAIN MANY-MANY HIERARCHY (SQL CODE with DATASET provided)
4 | P a g e
www.db-tools.com https://www.linkedin.com/in/sreenivasulu-kandakuru-72687a25
SCOPE OF THE PRESENTATION ............................................................................................................................................................................................................ 5
Create Structures and populate initial data set............................................................................................................................................................................... 6
Let’s Try few SQL queries to generate the ‘depth of hierarchy’.................................................................................................................................................. 8
Add a node and its children ............................................................................................................................................................................................................. 9
Let’s Try few SQL queries to generate the ‘depth of hierarchy’ after addition......................................................................................................................... 11
‘Generated Hierarchy’ after addition looks like this ................................................................................................................................................................. 12
Delete a node and its children....................................................................................................................................................................................................... 13
After Delete It would look like this............................................................................................................................................................................................ 14
Let’s Try few SQL queries to generate the ‘depth of hierarchy’ after DELETION...................................................................................................................... 15
‘Generated Hierarchy Levels’ After delete................................................................................................................................................................................. 16
Update a node and its children...................................................................................................................................................................................................... 17
Our objective to achieve after update....................................................................................................................................................................................... 18
Let’s Try Updating the Hierarchy............................................................................................................................................................................................... 19
‘Generated Hierarchy Levels’ After update............................................................................................................................................................................... 20
5. CREATE AND MAITAIN MANY-MANY HIERARCHY (SQL CODE with DATASET provided)
5 | P a g e
www.db-tools.com https://www.linkedin.com/in/sreenivasulu-kandakuru-72687a25
SCOPE OF THE PRESENTATION
I will cover adding a node , deleting a node , updating a node . An updated hierarchy is
generated after each operation which would clearly show how easily a hierarchy
can be maintained
6. CREATE AND MAITAIN MANY-MANY HIERARCHY (SQL CODE with DATASET provided)
6 | P a g e
www.db-tools.com https://www.linkedin.com/in/sreenivasulu-kandakuru-72687a25
Create Structures and populate initial data set
CREATE TABLE dbo.product_hierarchy1
(
id INT NOT NULL,
parent_id INT NULL
)
INSERT INTO dbo.product_hierarchy1 (id, parent_id)
VALUES
(1, NULL),
(2, 1),
(3, 1),
(4, 2)
7. CREATE AND MAITAIN MANY-MANY HIERARCHY (SQL CODE with DATASET provided)
7 | P a g e
www.db-tools.com https://www.linkedin.com/in/sreenivasulu-kandakuru-72687a25
The hierarchy diagram of the initial dataset looks like this below
ow
8. CREATE AND MAITAIN MANY-MANY HIERARCHY (SQL CODE with DATASET provided)
8 | P a g e
www.db-tools.com https://www.linkedin.com/in/sreenivasulu-kandakuru-72687a25
Let’s Try few SQL queries to generate the ‘depth of hierarchy’
WITH product_hierarchy_cte AS
(
SELECT
id AS ancestor,
id AS descendant,
0 AS depth
FROM dbo.product_hierarchy1
UNION ALL
SELECT
CTE.ancestor AS ancestor,
C.id AS descendant,
CTE.depth + 1 AS depth
FROM dbo.product_hierarchy1 AS C
JOIN product_hierarchy_cte AS CTE
ON C.parent_id = CTE.descendant
)
SELECT * FROM product_hierarchy_cte
9. CREATE AND MAITAIN MANY-MANY HIERARCHY (SQL CODE with DATASET provided)
9 | P a g e
www.db-tools.com https://www.linkedin.com/in/sreenivasulu-kandakuru-72687a25
Add a node and its children
select * into dbo.product_hierarchy3 from dbo.product_hierarchy1
INSERT INTO dbo.product_hierarchy3 (id, parent_id)
VALUES
(5, 3),
(6, 4),
(7, 4),
(8, 5),
(9, 5)
10. CREATE AND MAITAIN MANY-MANY HIERARCHY (SQL CODE with DATASET provided)
10 | P a g e
www.db-tools.com https://www.linkedin.com/in/sreenivasulu-kandakuru-72687a25
The hierarchy diagram now looks like this below
11. CREATE AND MAITAIN MANY-MANY HIERARCHY (SQL CODE with DATASET provided)
11 | P a g e
www.db-tools.com https://www.linkedin.com/in/sreenivasulu-kandakuru-72687a25
Let’s Try few SQL queries to generate the ‘depth of hierarchy’ after addition
WITH product_hierarchy_cte3 AS
(
SELECT
id AS ancestor,
id AS descendant,
0 AS depth
FROM dbo.product_hierarchy3
UNION ALL
SELECT
CTE.ancestor AS ancestor,
C.id AS descendant,
CTE.depth + 1 AS depth
FROM dbo.product_hierarchy3 AS C
JOIN product_hierarchy_cte3 AS CTE
ON C.parent_id = CTE.descendant
)
SELECT * FROM product_hierarchy_cte3
12. CREATE AND MAITAIN MANY-MANY HIERARCHY (SQL CODE with DATASET provided)
12 | P a g e
www.db-tools.com https://www.linkedin.com/in/sreenivasulu-kandakuru-72687a25
‘Generated Hierarchy’ after addition looks like this
13. CREATE AND MAITAIN MANY-MANY HIERARCHY (SQL CODE with DATASET provided)
13 | P a g e
www.db-tools.com https://www.linkedin.com/in/sreenivasulu-kandakuru-72687a25
Delete a node and its children
Let’s delete node 5
Before Delete DataSet
14. CREATE AND MAITAIN MANY-MANY HIERARCHY (SQL CODE with DATASET provided)
14 | P a g e
www.db-tools.com https://www.linkedin.com/in/sreenivasulu-kandakuru-72687a25
After Delete It would look like this
15. CREATE AND MAITAIN MANY-MANY HIERARCHY (SQL CODE with DATASET provided)
15 | P a g e
www.db-tools.com https://www.linkedin.com/in/sreenivasulu-kandakuru-72687a25
Let’s Try few SQL queries to generate the ‘depth of hierarchy’ after DELETION
delete from product_hierarchy3 where parent_id = 5
delete from product_hierarchy3 where id = 5
WITH product_hierarchy_cte3 AS
(
SELECT
id AS ancestor,
id AS descendant,
0 AS depth
FROM dbo.product_hierarchy3
UNION ALL
SELECT
CTE.ancestor AS ancestor,
C.id AS descendant,
CTE.depth + 1 AS depth
FROM dbo.product_hierarchy3 AS C
JOIN product_hierarchy_cte3 AS CTE
ON C.parent_id = CTE.descendant
)
SELECT * FROM product_hierarchy_cte3
16. CREATE AND MAITAIN MANY-MANY HIERARCHY (SQL CODE with DATASET provided)
16 | P a g e
www.db-tools.com https://www.linkedin.com/in/sreenivasulu-kandakuru-72687a25
‘Generated Hierarchy Levels’ After delete
17. CREATE AND MAITAIN MANY-MANY HIERARCHY (SQL CODE with DATASET provided)
17 | P a g e
www.db-tools.com https://www.linkedin.com/in/sreenivasulu-kandakuru-72687a25
Update a node and its children
Let’s Update 4 to 10 from the initial data set which was this
18. CREATE AND MAITAIN MANY-MANY HIERARCHY (SQL CODE with DATASET provided)
18 | P a g e
www.db-tools.com https://www.linkedin.com/in/sreenivasulu-kandakuru-72687a25
Our objective to achieve after update
19. CREATE AND MAITAIN MANY-MANY HIERARCHY (SQL CODE with DATASET provided)
19 | P a g e
www.db-tools.com https://www.linkedin.com/in/sreenivasulu-kandakuru-72687a25
Let’s Try Updating the Hierarchy
select * from dbo.[product_hierarchy3]
update [product_hierarchy3] set id = 10 where id = 4
update [product_hierarchy3] set parent_id = 10 where parent_id = 4
WITH product_hierarchy_cte4 AS
(
SELECT
id AS ancestor,
id AS descendant,
0 AS depth
FROM dbo.product_hierarchy3
UNION ALL
SELECT
CTE.ancestor AS ancestor,
C.id AS descendant,
CTE.depth + 1 AS depth
FROM dbo.product_hierarchy3 AS C
JOIN product_hierarchy_cte4 AS CTE
ON C.parent_id = CTE.descendant
)
select * from product_hierarchy_cte4
20. CREATE AND MAITAIN MANY-MANY HIERARCHY (SQL CODE with DATASET provided)
20 | P a g e
www.db-tools.com https://www.linkedin.com/in/sreenivasulu-kandakuru-72687a25
‘Generated Hierarchy Levels’ After update
21. CREATE AND MAITAIN MANY-MANY HIERARCHY (SQL CODE with DATASET provided)
21 | P a g e
www.db-tools.com https://www.linkedin.com/in/sreenivasulu-kandakuru-72687a25