SlideShare a Scribd company logo
1 of 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?

More Related Content

What's hot

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 Hallhannonhill
 
Webmontag Berlin "coffee script"
Webmontag Berlin "coffee script"Webmontag Berlin "coffee script"
Webmontag Berlin "coffee script"Webmontag Berlin
 
Data structure in perl
Data structure in perlData structure in perl
Data structure in perlsana mateen
 
Syntactic sugar in Postgre SQL
Syntactic sugar in Postgre SQLSyntactic sugar in Postgre SQL
Syntactic sugar in Postgre SQLAntony Abramchenko
 
Syntactic sugar in postgre sql
Syntactic sugar in postgre sqlSyntactic sugar in postgre sql
Syntactic sugar in postgre sqlAntony Abramchenko
 
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()Erick Hitter
 
Developing applications for performance
Developing applications for performanceDeveloping applications for performance
Developing applications for performanceLeon Fayer
 
dcs plus Catalogue 2015
dcs plus Catalogue 2015dcs plus Catalogue 2015
dcs plus Catalogue 2015dcs plus
 
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 databaseLeon Fayer
 
Array in php
Array in phpArray in php
Array in phpilakkiya
 
Everything About PowerShell
Everything About PowerShellEverything About PowerShell
Everything About PowerShellGaetano Causio
 

What's hot (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
 

Similar to Managing category structures in relational databases

Introduction to Perl - Day 2
Introduction to Perl - Day 2Introduction to Perl - Day 2
Introduction to Perl - Day 2Dave Cross
 
Drupal Lightning FAPI Jumpstart
Drupal Lightning FAPI JumpstartDrupal Lightning FAPI Jumpstart
Drupal Lightning FAPI Jumpstartguestfd47e4c7
 
Graph Databases
Graph DatabasesGraph Databases
Graph DatabasesJosh Adell
 
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.Workhorse Computing
 
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 phpWade Womersley
 
Intermediate Perl
Intermediate PerlIntermediate Perl
Intermediate PerlDave Cross
 
Exploiting Php With Php
Exploiting Php With PhpExploiting Php With Php
Exploiting Php With PhpJeremy Coates
 
[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ć ...Mateusz Zalewski
 
Oracle basic queries
Oracle basic queriesOracle basic queries
Oracle basic queriesPRAKHAR JHA
 
06 Php Mysql Connect Query
06 Php Mysql Connect Query06 Php Mysql Connect Query
06 Php Mysql Connect QueryGeshan Manandhar
 
DBIx-DataModel v2.0 in detail
DBIx-DataModel v2.0 in detail DBIx-DataModel v2.0 in detail
DBIx-DataModel v2.0 in detail Laurent Dami
 
Drupal Development (Part 2)
Drupal Development (Part 2)Drupal Development (Part 2)
Drupal Development (Part 2)Jeff Eaton
 
The Art of Transduction
The Art of TransductionThe Art of Transduction
The Art of TransductionDavid Stockton
 
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 scenariosDivante
 

Similar to 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
 

Recently uploaded

Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
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...apidays
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 

Recently uploaded (20)

Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
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...
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 

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?