SlideShare uma empresa Scribd logo
1 de 19
Managing category structures in relational databases An introduction to numbered sets Antoine Osanz  <antoine@ivt.com.au>
[object Object],[object Object],[object Object],[object Object],Hierarchical Data
Hierarchical Data – A tree Home Theatre Projectors Surround Sound Sub Woofers Satellites Speakers
Common Category Storage ,[object Object],[object Object]
function   getPathDetails( $categorySerial ,  $pathDetails = array ())  { $sql   =   &quot;SELECT parentSerial, title  FROM adjacentList  WHERE categorySerial = '&quot; . $categorySerial . &quot;' LIMIT 0,1&quot; ; $result   =   $this ->dbase->query( $sql ) ; if  ( $result ->numRows()  ==   1 )  { $row   =   $result ->fetchAssoc( $result ) ; $pathDetails []  =   $row [ 'title' ] ; if  ( $row [ 'parentSerial' ]  >  0)  {   $pathDetails   =   $this ->getPathDetails( $row [ 'parentSerial' ],  $pathDetails ) ; } return ( $pathDetails ) ; } else { return ( $pathDetails ) ; } } Projectors: getPathDetails(2); [0] => Projectors [1] => Home Theatre Satellites: getPathDetails(6); [0] => Satellites [1] => Speakers [2] => Surround Sound [3] => Home Theatre‏ Path details: Recursive Function
SELECT  level1.title AS lev1Title,  level2.title as lev2Title, level3.title as lev3Title,  level4.title as lev4Title FROM adjacentList AS level1 LEFT JOIN adjacentList AS level2 ON (level2.parentSerial = level1.categorySerial)‏ LEFT JOIN adjacentList AS level3 ON (level3.parentSerial = level2.categorySerial)‏ LEFT JOIN adjacentList AS level4 ON (level4.parentSerial = level3.categorySerial)‏ WHERE level4.categorySerial = '6'; +--------------+-------------------+--------------+------------+ | lev1Title  | lev2Title  | lev3Title  | lev4Title  | +--------------+-------------------+--------------+------------+ | Home Theatre | Surround Sound  | Speaker  | Satellites | +--------------+-------------------+--------------+------------+ Path details: Self Joins
Too much effort ,[object Object],[object Object],[object Object]
The tree again… Home Theatre Projectors Surround Sound Sub Woofers Satellites Speakers
Nested and Numbered Sets
A Numbered Tree Home Theatre Projectors Surround Sound Sub Woofers Satellites Speakers 2 3 4 1 12 5 10 7 9 8 6 11
Storing a Numbered Tree
function   getPathDetails( $categorySerial ,  $pathDetails = array ())  { $sql   =   &quot;SELECT parent.title  FROM productCategories AS node,   productCategories AS parent WHERE parent.leftNode <= node.leftNode   AND parent.rightNode >= node.rightNode   AND node.categorySerial = '&quot; . $categorySerial . &quot;' ORDER BY parent.leftNode DESC&quot; ; $result   =   $this ->dbase->query( $sql ) ; $pathDetails  =   array () ; while  ( $row   =   $result ->fetchAssoc( $result ))  { $pathDetails []  =   $row [ 'title' ] ; } return ( $pathDetails ) ; } Projectors: getPathDetails(2); [0] => Projectors [1] => Home Theatre Satellites: getPathDetails(6); [0] => Satellites [1] => Speakers [2] => Surround Sound [3] => Home Theatre Path details: Revisited
function   getDescendants( $categorySerial )  { $sql   =   &quot;SELECT node.title  FROM productCategories AS node,   productCategories AS parent WHERE    node.leftNode BETWEEN parent.leftNode AND parent.rightNode   AND parent.categorySerial = '&quot; . $categorySerial . &quot;'   AND node.categorySerial != '&quot;. $categorySerial .&quot;' ORDER BY node.leftNode ASC&quot; ; $result   =   $this ->dbase->query( $sql ) ; $pathDetails  =   array () ; while  ( $row   =   $result ->fetchAssoc( $result ))  { $pathDetails []  =   $row [ 'title' ] ; } return ( $pathDetails ) ; } Speakers: getDescendants(5); [0] => Satellites Surround Sound: getDescendants(3); [0] => Speakers [1] => Satellites [2] => Sub Woofers Getting Category Descendants
START TRANSACTION SELECT @myLeft := leftNode  FROM productCategories   WHERE categorySerial = '$parentSerial'  LIMIT 0,1 UPDATE productCategories SET rightNode = rightNode + 2  WHERE rightNode > @myLeft UPDATE productCategories   SET leftNode = leftNode + 2  WHERE leftNode > @myLeft INSERT INTO productCategories SET leftNode = @myLeft + 1, rightNode = @myLeft + 2, title = 'NEW CATEGORY', parentSerial = '$parentSerial' COMMIT Adding Nodes
START TRANSACTION SELECT  @myLeft := IF(MAX(rightNode) IS NULL,    0,    MAX(rightNode))  FROM productCategories WHERE parentSerial = '0'   LIMIT 0,1 INSERT INTO productCategories SET leftNode = @myLeft + 1, rightNode = @myLeft + 2, title = 'NEW CATEGORY', parentSerial = '$parentSerial' COMMIT Adding Primary Nodes
START TRANSACTION SELECT @myLeft := leftNode,  @myRight := rightNode,  @myWidth := rightNode - leftNode + 1 FROM productCategories WHERE categorySerial = '$categorySerial' LIMIT 0,1&quot;; DELETE FROM productCategories WHERE leftNode BETWEEN @myLeft AND @myRight UPDATE productCategories SET rightNode = rightNode - @myWidth WHERE rightNode > @myRight UPDATE productCategories SET leftNode = leftNode - @myWidth WHERE leftNode > @myLeft COMMIT Delete a node & its children
[object Object],[object Object],[object Object],Why Bother?
[object Object],[object Object],[object Object],Current Applications
Thankyou Special thanks to Arjen Lentz, Jonathan Oxer and Mike Hellyer for edumacating me. Questions?

Mais conteúdo relacionado

Mais procurados

Webmontag Berlin "coffee script"
Webmontag Berlin "coffee script"Webmontag Berlin "coffee script"
Webmontag Berlin "coffee script"
Webmontag Berlin
 
Array in php
Array in phpArray in php
Array in php
ilakkiya
 

Mais procurados (20)

6. list
6. list6. list
6. list
 
Chap 3php array part1
Chap 3php array part1Chap 3php array part1
Chap 3php array part1
 
7. tuples, set &amp; dictionary
7. tuples, set &amp; dictionary7. tuples, set &amp; dictionary
7. tuples, set &amp; dictionary
 
Pitfalls to Avoid for Cascade Server Newbies by Lisa Hall
Pitfalls to Avoid for Cascade Server Newbies by Lisa HallPitfalls to Avoid for Cascade Server Newbies by Lisa Hall
Pitfalls to Avoid for Cascade Server Newbies by Lisa Hall
 
Webmontag Berlin "coffee script"
Webmontag Berlin "coffee script"Webmontag Berlin "coffee script"
Webmontag Berlin "coffee script"
 
4.1 PHP Arrays
4.1 PHP Arrays4.1 PHP Arrays
4.1 PHP Arrays
 
Data structure in perl
Data structure in perlData structure in perl
Data structure in perl
 
Syntactic sugar in Postgre SQL
Syntactic sugar in Postgre SQLSyntactic sugar in Postgre SQL
Syntactic sugar in Postgre SQL
 
Syntactic sugar in postgre sql
Syntactic sugar in postgre sqlSyntactic sugar in postgre sql
Syntactic sugar in postgre sql
 
WP_Query, pre_get_posts, and eliminating query_posts()
WP_Query, pre_get_posts, and eliminating query_posts()WP_Query, pre_get_posts, and eliminating query_posts()
WP_Query, pre_get_posts, and eliminating query_posts()
 
PHP array 2
PHP array 2PHP array 2
PHP array 2
 
Sorting arrays in PHP
Sorting arrays in PHPSorting arrays in PHP
Sorting arrays in PHP
 
Developing applications for performance
Developing applications for performanceDeveloping applications for performance
Developing applications for performance
 
Oops in php
Oops in phpOops in php
Oops in php
 
dcs plus Catalogue 2015
dcs plus Catalogue 2015dcs plus Catalogue 2015
dcs plus Catalogue 2015
 
Intoduction to php arrays
Intoduction to php arraysIntoduction to php arrays
Intoduction to php arrays
 
PHP performance 101: so you need to use a database
PHP performance 101: so you need to use a databasePHP performance 101: so you need to use a database
PHP performance 101: so you need to use a database
 
Array in php
Array in phpArray in php
Array in php
 
Everything About PowerShell
Everything About PowerShellEverything About PowerShell
Everything About PowerShell
 
PHP 101
PHP 101 PHP 101
PHP 101
 

Semelhante a Managing category structures in relational databases

Introduction to Perl - Day 2
Introduction to Perl - Day 2Introduction to Perl - Day 2
Introduction to Perl - Day 2
Dave Cross
 
Graph Databases
Graph DatabasesGraph Databases
Graph Databases
Josh Adell
 
Building a horizontally scalable API in php
Building a horizontally scalable API in phpBuilding a horizontally scalable API in php
Building a horizontally scalable API in php
Wade Womersley
 

Semelhante a Managing category structures in relational databases (20)

Intro to The PHP SPL
Intro to The PHP SPLIntro to The PHP SPL
Intro to The PHP SPL
 
Views notwithstanding
Views notwithstandingViews notwithstanding
Views notwithstanding
 
Introduction to Perl - Day 2
Introduction to Perl - Day 2Introduction to Perl - Day 2
Introduction to Perl - Day 2
 
Drupal Lightning FAPI Jumpstart
Drupal Lightning FAPI JumpstartDrupal Lightning FAPI Jumpstart
Drupal Lightning FAPI Jumpstart
 
Graph Databases
Graph DatabasesGraph Databases
Graph Databases
 
Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.
 
Drupal7 dbtng
Drupal7  dbtngDrupal7  dbtng
Drupal7 dbtng
 
Building a horizontally scalable API in php
Building a horizontally scalable API in phpBuilding a horizontally scalable API in php
Building a horizontally scalable API in php
 
Intermediate Perl
Intermediate PerlIntermediate Perl
Intermediate Perl
 
Php My Sql
Php My SqlPhp My Sql
Php My Sql
 
JQuery Basics
JQuery BasicsJQuery Basics
JQuery Basics
 
Exploiting Php With Php
Exploiting Php With PhpExploiting Php With Php
Exploiting Php With Php
 
[PHPCon 2023] “Kto to pisał?!... a, to ja.”, czyli sposoby żeby znienawidzić ...
[PHPCon 2023] “Kto to pisał?!... a, to ja.”, czyli sposoby żeby znienawidzić ...[PHPCon 2023] “Kto to pisał?!... a, to ja.”, czyli sposoby żeby znienawidzić ...
[PHPCon 2023] “Kto to pisał?!... a, to ja.”, czyli sposoby żeby znienawidzić ...
 
Oracle basic queries
Oracle basic queriesOracle basic queries
Oracle basic queries
 
06 Php Mysql Connect Query
06 Php Mysql Connect Query06 Php Mysql Connect Query
06 Php Mysql Connect Query
 
DBIx-DataModel v2.0 in detail
DBIx-DataModel v2.0 in detail DBIx-DataModel v2.0 in detail
DBIx-DataModel v2.0 in detail
 
Drupal Development (Part 2)
Drupal Development (Part 2)Drupal Development (Part 2)
Drupal Development (Part 2)
 
The Art of Transduction
The Art of TransductionThe Art of Transduction
The Art of Transduction
 
Why is crud a bad idea - focus on real scenarios
Why is crud a bad idea - focus on real scenariosWhy is crud a bad idea - focus on real scenarios
Why is crud a bad idea - focus on real scenarios
 
Intermediate PHP
Intermediate PHPIntermediate PHP
Intermediate PHP
 

Último

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 

Managing category structures in relational databases

  • 1. Managing category structures in relational databases An introduction to numbered sets Antoine Osanz <antoine@ivt.com.au>
  • 2.
  • 3. Hierarchical Data – A tree Home Theatre Projectors Surround Sound Sub Woofers Satellites Speakers
  • 4.
  • 5. function getPathDetails( $categorySerial , $pathDetails = array ()) { $sql = &quot;SELECT parentSerial, title FROM adjacentList WHERE categorySerial = '&quot; . $categorySerial . &quot;' LIMIT 0,1&quot; ; $result = $this ->dbase->query( $sql ) ; if ( $result ->numRows() == 1 ) { $row = $result ->fetchAssoc( $result ) ; $pathDetails [] = $row [ 'title' ] ; if ( $row [ 'parentSerial' ] > 0) { $pathDetails = $this ->getPathDetails( $row [ 'parentSerial' ], $pathDetails ) ; } return ( $pathDetails ) ; } else { return ( $pathDetails ) ; } } Projectors: getPathDetails(2); [0] => Projectors [1] => Home Theatre Satellites: getPathDetails(6); [0] => Satellites [1] => Speakers [2] => Surround Sound [3] => Home Theatre‏ Path details: Recursive Function
  • 6. SELECT level1.title AS lev1Title, level2.title as lev2Title, level3.title as lev3Title, level4.title as lev4Title FROM adjacentList AS level1 LEFT JOIN adjacentList AS level2 ON (level2.parentSerial = level1.categorySerial)‏ LEFT JOIN adjacentList AS level3 ON (level3.parentSerial = level2.categorySerial)‏ LEFT JOIN adjacentList AS level4 ON (level4.parentSerial = level3.categorySerial)‏ WHERE level4.categorySerial = '6'; +--------------+-------------------+--------------+------------+ | lev1Title | lev2Title | lev3Title | lev4Title | +--------------+-------------------+--------------+------------+ | Home Theatre | Surround Sound | Speaker | Satellites | +--------------+-------------------+--------------+------------+ Path details: Self Joins
  • 7.
  • 8. The tree again… Home Theatre Projectors Surround Sound Sub Woofers Satellites Speakers
  • 10. A Numbered Tree Home Theatre Projectors Surround Sound Sub Woofers Satellites Speakers 2 3 4 1 12 5 10 7 9 8 6 11
  • 12. function getPathDetails( $categorySerial , $pathDetails = array ()) { $sql = &quot;SELECT parent.title FROM productCategories AS node, productCategories AS parent WHERE parent.leftNode <= node.leftNode AND parent.rightNode >= node.rightNode AND node.categorySerial = '&quot; . $categorySerial . &quot;' ORDER BY parent.leftNode DESC&quot; ; $result = $this ->dbase->query( $sql ) ; $pathDetails = array () ; while ( $row = $result ->fetchAssoc( $result )) { $pathDetails [] = $row [ 'title' ] ; } return ( $pathDetails ) ; } Projectors: getPathDetails(2); [0] => Projectors [1] => Home Theatre Satellites: getPathDetails(6); [0] => Satellites [1] => Speakers [2] => Surround Sound [3] => Home Theatre Path details: Revisited
  • 13. function getDescendants( $categorySerial ) { $sql = &quot;SELECT node.title FROM productCategories AS node, productCategories AS parent WHERE node.leftNode BETWEEN parent.leftNode AND parent.rightNode AND parent.categorySerial = '&quot; . $categorySerial . &quot;' AND node.categorySerial != '&quot;. $categorySerial .&quot;' ORDER BY node.leftNode ASC&quot; ; $result = $this ->dbase->query( $sql ) ; $pathDetails = array () ; while ( $row = $result ->fetchAssoc( $result )) { $pathDetails [] = $row [ 'title' ] ; } return ( $pathDetails ) ; } Speakers: getDescendants(5); [0] => Satellites Surround Sound: getDescendants(3); [0] => Speakers [1] => Satellites [2] => Sub Woofers Getting Category Descendants
  • 14. START TRANSACTION SELECT @myLeft := leftNode FROM productCategories WHERE categorySerial = '$parentSerial' LIMIT 0,1 UPDATE productCategories SET rightNode = rightNode + 2 WHERE rightNode > @myLeft UPDATE productCategories SET leftNode = leftNode + 2 WHERE leftNode > @myLeft INSERT INTO productCategories SET leftNode = @myLeft + 1, rightNode = @myLeft + 2, title = 'NEW CATEGORY', parentSerial = '$parentSerial' COMMIT Adding Nodes
  • 15. START TRANSACTION SELECT @myLeft := IF(MAX(rightNode) IS NULL, 0, MAX(rightNode)) FROM productCategories WHERE parentSerial = '0' LIMIT 0,1 INSERT INTO productCategories SET leftNode = @myLeft + 1, rightNode = @myLeft + 2, title = 'NEW CATEGORY', parentSerial = '$parentSerial' COMMIT Adding Primary Nodes
  • 16. START TRANSACTION SELECT @myLeft := leftNode, @myRight := rightNode, @myWidth := rightNode - leftNode + 1 FROM productCategories WHERE categorySerial = '$categorySerial' LIMIT 0,1&quot;; DELETE FROM productCategories WHERE leftNode BETWEEN @myLeft AND @myRight UPDATE productCategories SET rightNode = rightNode - @myWidth WHERE rightNode > @myRight UPDATE productCategories SET leftNode = leftNode - @myWidth WHERE leftNode > @myLeft COMMIT Delete a node & its children
  • 17.
  • 18.
  • 19. Thankyou Special thanks to Arjen Lentz, Jonathan Oxer and Mike Hellyer for edumacating me. Questions?