SlideShare a Scribd company logo
1 of 21
Download to read offline
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..
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
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)
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
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
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)
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
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
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)
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
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
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
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
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
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
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
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
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
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
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
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

More Related Content

Similar to Create and Maintain COMPLEX HIERARCHIES easily

Using Backbone.js with Drupal 7 and 8
Using Backbone.js with Drupal 7 and 8Using Backbone.js with Drupal 7 and 8
Using Backbone.js with Drupal 7 and 8Ovadiah Myrgorod
 
bmarshall teaching Calculation Manager on prem
bmarshall teaching Calculation Manager on prembmarshall teaching Calculation Manager on prem
bmarshall teaching Calculation Manager on premRoma766619
 
Generate SSIS packages automatically with Biml and BimlScript (SQLKonferenz 2...
Generate SSIS packages automatically with Biml and BimlScript (SQLKonferenz 2...Generate SSIS packages automatically with Biml and BimlScript (SQLKonferenz 2...
Generate SSIS packages automatically with Biml and BimlScript (SQLKonferenz 2...Cathrine Wilhelmsen
 
Managing Oracle Streams Using Enterprise Manager Grid Control
Managing Oracle Streams Using Enterprise Manager Grid ControlManaging Oracle Streams Using Enterprise Manager Grid Control
Managing Oracle Streams Using Enterprise Manager Grid Controlscottb411
 
ViA Bootstrap 4
ViA Bootstrap 4ViA Bootstrap 4
ViA Bootstrap 4imdurgesh
 
Creating Database 2010
Creating Database 2010Creating Database 2010
Creating Database 2010tgushi12
 
Sap business-object-universe-idt-lab-i
Sap business-object-universe-idt-lab-iSap business-object-universe-idt-lab-i
Sap business-object-universe-idt-lab-iAmit Sharma
 
DNN Database Tips & Tricks
DNN Database Tips & TricksDNN Database Tips & Tricks
DNN Database Tips & TricksWill Strohl
 
WordPress Accessibility: WordCamp Chicago
WordPress Accessibility: WordCamp ChicagoWordPress Accessibility: WordCamp Chicago
WordPress Accessibility: WordCamp ChicagoJoseph Dolson
 
Tutorial - Learn SQL with Live Online Database
Tutorial - Learn SQL with Live Online DatabaseTutorial - Learn SQL with Live Online Database
Tutorial - Learn SQL with Live Online DatabaseDBrow Adm
 
Geek Sync | Rewriting Bad SQL Code 101
Geek Sync | Rewriting Bad SQL Code 101Geek Sync | Rewriting Bad SQL Code 101
Geek Sync | Rewriting Bad SQL Code 101IDERA Software
 
Twitter bootstrap training_session_ppt
Twitter bootstrap training_session_pptTwitter bootstrap training_session_ppt
Twitter bootstrap training_session_pptRadheshyam Kori
 
Biml for Beginners - Generating SSIS Packages with BimlScript (SQLSaturday Ex...
Biml for Beginners - Generating SSIS Packages with BimlScript (SQLSaturday Ex...Biml for Beginners - Generating SSIS Packages with BimlScript (SQLSaturday Ex...
Biml for Beginners - Generating SSIS Packages with BimlScript (SQLSaturday Ex...Cathrine Wilhelmsen
 
IBM Insight 2015 - 1824 - Using Bluemix and dashDB for Twitter Analysis
IBM Insight 2015 - 1824 - Using Bluemix and dashDB for Twitter AnalysisIBM Insight 2015 - 1824 - Using Bluemix and dashDB for Twitter Analysis
IBM Insight 2015 - 1824 - Using Bluemix and dashDB for Twitter AnalysisTorsten Steinbach
 
Struts 2 – Database Access
Struts 2 – Database AccessStruts 2 – Database Access
Struts 2 – Database AccessDucat India
 
171_74_216_Module_5-Non_relational_database_-mongodb.pptx
171_74_216_Module_5-Non_relational_database_-mongodb.pptx171_74_216_Module_5-Non_relational_database_-mongodb.pptx
171_74_216_Module_5-Non_relational_database_-mongodb.pptxsukrithlal008
 
Big Data: Get started with SQL on Hadoop self-study lab
Big Data:  Get started with SQL on Hadoop self-study lab Big Data:  Get started with SQL on Hadoop self-study lab
Big Data: Get started with SQL on Hadoop self-study lab Cynthia Saracco
 

Similar to Create and Maintain COMPLEX HIERARCHIES easily (20)

Using Backbone.js with Drupal 7 and 8
Using Backbone.js with Drupal 7 and 8Using Backbone.js with Drupal 7 and 8
Using Backbone.js with Drupal 7 and 8
 
bmarshall teaching Calculation Manager on prem
bmarshall teaching Calculation Manager on prembmarshall teaching Calculation Manager on prem
bmarshall teaching Calculation Manager on prem
 
Intro to KnockoutJS
Intro to KnockoutJSIntro to KnockoutJS
Intro to KnockoutJS
 
Generate SSIS packages automatically with Biml and BimlScript (SQLKonferenz 2...
Generate SSIS packages automatically with Biml and BimlScript (SQLKonferenz 2...Generate SSIS packages automatically with Biml and BimlScript (SQLKonferenz 2...
Generate SSIS packages automatically with Biml and BimlScript (SQLKonferenz 2...
 
Managing Oracle Streams Using Enterprise Manager Grid Control
Managing Oracle Streams Using Enterprise Manager Grid ControlManaging Oracle Streams Using Enterprise Manager Grid Control
Managing Oracle Streams Using Enterprise Manager Grid Control
 
MySQL document_store
MySQL document_storeMySQL document_store
MySQL document_store
 
ViA Bootstrap 4
ViA Bootstrap 4ViA Bootstrap 4
ViA Bootstrap 4
 
Creating Database 2010
Creating Database 2010Creating Database 2010
Creating Database 2010
 
Sap business-object-universe-idt-lab-i
Sap business-object-universe-idt-lab-iSap business-object-universe-idt-lab-i
Sap business-object-universe-idt-lab-i
 
Backbone js-slides
Backbone js-slidesBackbone js-slides
Backbone js-slides
 
DNN Database Tips & Tricks
DNN Database Tips & TricksDNN Database Tips & Tricks
DNN Database Tips & Tricks
 
WordPress Accessibility: WordCamp Chicago
WordPress Accessibility: WordCamp ChicagoWordPress Accessibility: WordCamp Chicago
WordPress Accessibility: WordCamp Chicago
 
Tutorial - Learn SQL with Live Online Database
Tutorial - Learn SQL with Live Online DatabaseTutorial - Learn SQL with Live Online Database
Tutorial - Learn SQL with Live Online Database
 
Geek Sync | Rewriting Bad SQL Code 101
Geek Sync | Rewriting Bad SQL Code 101Geek Sync | Rewriting Bad SQL Code 101
Geek Sync | Rewriting Bad SQL Code 101
 
Twitter bootstrap training_session_ppt
Twitter bootstrap training_session_pptTwitter bootstrap training_session_ppt
Twitter bootstrap training_session_ppt
 
Biml for Beginners - Generating SSIS Packages with BimlScript (SQLSaturday Ex...
Biml for Beginners - Generating SSIS Packages with BimlScript (SQLSaturday Ex...Biml for Beginners - Generating SSIS Packages with BimlScript (SQLSaturday Ex...
Biml for Beginners - Generating SSIS Packages with BimlScript (SQLSaturday Ex...
 
IBM Insight 2015 - 1824 - Using Bluemix and dashDB for Twitter Analysis
IBM Insight 2015 - 1824 - Using Bluemix and dashDB for Twitter AnalysisIBM Insight 2015 - 1824 - Using Bluemix and dashDB for Twitter Analysis
IBM Insight 2015 - 1824 - Using Bluemix and dashDB for Twitter Analysis
 
Struts 2 – Database Access
Struts 2 – Database AccessStruts 2 – Database Access
Struts 2 – Database Access
 
171_74_216_Module_5-Non_relational_database_-mongodb.pptx
171_74_216_Module_5-Non_relational_database_-mongodb.pptx171_74_216_Module_5-Non_relational_database_-mongodb.pptx
171_74_216_Module_5-Non_relational_database_-mongodb.pptx
 
Big Data: Get started with SQL on Hadoop self-study lab
Big Data:  Get started with SQL on Hadoop self-study lab Big Data:  Get started with SQL on Hadoop self-study lab
Big Data: Get started with SQL on Hadoop self-study lab
 

Recently uploaded

INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDINTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDRafezzaman
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdfHuman37
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...soniya singh
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home ServiceSapana Sha
 
RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.natarajan8993
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationshipsccctableauusergroup
 
NLP Project PPT: Flipkart Product Reviews through NLP Data Science.pptx
NLP Project PPT: Flipkart Product Reviews through NLP Data Science.pptxNLP Project PPT: Flipkart Product Reviews through NLP Data Science.pptx
NLP Project PPT: Flipkart Product Reviews through NLP Data Science.pptxBoston Institute of Analytics
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptxthyngster
 
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一fhwihughh
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPramod Kumar Srivastava
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]📊 Markus Baersch
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)jennyeacort
 
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一F sss
 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfSocial Samosa
 
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degreeyuu sss
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Sapana Sha
 
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAmazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAbdelrhman abooda
 
办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一
办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一
办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一F La
 
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝DelhiRS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhijennyeacort
 

Recently uploaded (20)

INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDINTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service
 
RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships
 
E-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptxE-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptx
 
NLP Project PPT: Flipkart Product Reviews through NLP Data Science.pptx
NLP Project PPT: Flipkart Product Reviews through NLP Data Science.pptxNLP Project PPT: Flipkart Product Reviews through NLP Data Science.pptx
NLP Project PPT: Flipkart Product Reviews through NLP Data Science.pptx
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
 
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
 
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
 
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
 
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAmazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
 
办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一
办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一
办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一
 
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝DelhiRS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
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