Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
Managing category structures in relational databases An introduction to numbered sets Antoine Osanz  <antoine@ivt.com.au>
Parent-Child relationships Very common in web applications Categorise content Manage organisation structures Hierarchical Data
Hierarchical Data – A tree Home Theatre Projectors Surround Sound Sub Woofers Satellites Speakers
Common Category Storage “ Adjacency List Model” Stores serial of the parent
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 Recursive functions are confusing Self joins only good depth is known How else can I organise my categories?
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
Less queries to read Better handling of large data sets Simpler/More readable code Why Bother?
Category Manager Access Permissions Organisation Structures Current Applications
Thankyou Special thanks to Arjen Lentz, Jonathan Oxer and Mike Hellyer for edumacating me. Questions?

More Related Content

What's hot

6. list
6. list6. list
Chap 3php array part1
Chap 3php array part1Chap 3php array part1
Chap 3php array part1
monikadeshmane
 
7. tuples, set &amp; dictionary
7. tuples, set &amp; dictionary7. tuples, set &amp; dictionary
7. tuples, set &amp; dictionary
PhD Research Scholar
 
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
hannonhill
 
Webmontag Berlin "coffee script"
Webmontag Berlin "coffee script"Webmontag Berlin "coffee script"
Webmontag Berlin "coffee script"
Webmontag Berlin
 
4.1 PHP Arrays
4.1 PHP Arrays4.1 PHP Arrays
4.1 PHP Arrays
Jalpesh Vasa
 
Data structure in perl
Data structure in perlData structure in perl
Data structure in perl
sana mateen
 
Syntactic sugar in Postgre SQL
Syntactic sugar in Postgre SQLSyntactic sugar in Postgre SQL
Syntactic sugar in Postgre SQL
Antony Abramchenko
 
Syntactic sugar in postgre sql
Syntactic sugar in postgre sqlSyntactic sugar in postgre sql
Syntactic sugar in postgre sql
Antony 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
 
PHP array 2
PHP array 2PHP array 2
PHP array 2
Mudasir Syed
 
Sorting arrays in PHP
Sorting arrays in PHPSorting arrays in PHP
Sorting arrays in PHP
Vineet Kumar Saini
 
Developing applications for performance
Developing applications for performanceDeveloping applications for performance
Developing applications for performance
Leon Fayer
 
Oops in php
Oops in phpOops in php
dcs plus Catalogue 2015
dcs plus Catalogue 2015dcs plus Catalogue 2015
dcs plus Catalogue 2015
dcs plus
 
Intoduction to php arrays
Intoduction to php arraysIntoduction 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
Leon Fayer
 
Array in php
Array in phpArray in php
Array in php
ilakkiya
 
Everything About PowerShell
Everything About PowerShellEverything About PowerShell
Everything About PowerShell
Gaetano Causio
 
PHP 101
PHP 101 PHP 101
PHP 101
Muhammad Hijazi
 

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

Intro to The PHP SPL
Intro to The PHP SPLIntro to The PHP SPL
Intro to The PHP SPL
Chris Tankersley
 
Views notwithstanding
Views notwithstandingViews notwithstanding
Views notwithstanding
Srikanth Bangalore
 
Introduction to Perl - Day 2
Introduction to Perl - Day 2Introduction to Perl - Day 2
Introduction to Perl - Day 2
Dave Cross
 
Drupal Lightning FAPI Jumpstart
Drupal Lightning FAPI JumpstartDrupal Lightning FAPI Jumpstart
Drupal Lightning FAPI Jumpstart
guestfd47e4c7
 
Graph Databases
Graph DatabasesGraph Databases
Graph Databases
Josh 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
 
Drupal7 dbtng
Drupal7  dbtngDrupal7  dbtng
Drupal7 dbtng
Nicolas Leroy
 
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
 
Intermediate Perl
Intermediate PerlIntermediate Perl
Intermediate Perl
Dave Cross
 
Php My Sql
Php My SqlPhp My Sql
Php My Sql
mussawir20
 
JQuery Basics
JQuery BasicsJQuery Basics
JQuery Basics
Alin Taranu
 
Exploiting Php With Php
Exploiting Php With PhpExploiting Php With Php
Exploiting Php With Php
Jeremy 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 queries
PRAKHAR JHA
 
06 Php Mysql Connect Query
06 Php Mysql Connect Query06 Php Mysql Connect Query
06 Php Mysql Connect Query
Geshan 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 Transduction
David 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 scenarios
Divante
 
Intermediate PHP
Intermediate PHPIntermediate PHP
Intermediate PHP
Bradley Holt
 

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

Flame Atomic Emission Spectroscopy.-pptx
Flame Atomic Emission Spectroscopy.-pptxFlame Atomic Emission Spectroscopy.-pptx
Flame Atomic Emission Spectroscopy.-pptx
VaishnaviChavan206944
 
Multimodal Embeddings (continued) - South Bay Meetup Slides
Multimodal Embeddings (continued) - South Bay Meetup SlidesMultimodal Embeddings (continued) - South Bay Meetup Slides
Multimodal Embeddings (continued) - South Bay Meetup Slides
Zilliz
 
Leading Bigcommerce Development Services for Online Retailers
Leading Bigcommerce Development Services for Online RetailersLeading Bigcommerce Development Services for Online Retailers
Leading Bigcommerce Development Services for Online Retailers
SynapseIndia
 
Mega MUG 2024: Working smarter in Marketo
Mega MUG 2024: Working smarter in MarketoMega MUG 2024: Working smarter in Marketo
Mega MUG 2024: Working smarter in Marketo
Stephanie Tyagita
 
FIDO Munich Seminar Workforce Authentication Case Study.pptx
FIDO Munich Seminar Workforce Authentication Case Study.pptxFIDO Munich Seminar Workforce Authentication Case Study.pptx
FIDO Munich Seminar Workforce Authentication Case Study.pptx
FIDO Alliance
 
Top keywords searches on home and garden
Top keywords searches on home and gardenTop keywords searches on home and garden
Top keywords searches on home and garden
riannecreativetwo
 
STKI Israeli IT Market Study v2 August 2024.pdf
STKI Israeli IT Market Study v2 August 2024.pdfSTKI Israeli IT Market Study v2 August 2024.pdf
STKI Israeli IT Market Study v2 August 2024.pdf
Dr. Jimmy Schwarzkopf
 
TribeQonf2024_Dimpy_ShiftingSecurityLeft
TribeQonf2024_Dimpy_ShiftingSecurityLeftTribeQonf2024_Dimpy_ShiftingSecurityLeft
TribeQonf2024_Dimpy_ShiftingSecurityLeft
Dimpy Adhikary
 
Jacquard Fabric Explained: Origins, Characteristics, and Uses
Jacquard Fabric Explained: Origins, Characteristics, and UsesJacquard Fabric Explained: Origins, Characteristics, and Uses
Jacquard Fabric Explained: Origins, Characteristics, and Uses
ldtexsolbl
 
Blue Screen Of Death | Windows Down | Biggest IT failure
Blue Screen Of Death | Windows Down | Biggest IT failureBlue Screen Of Death | Windows Down | Biggest IT failure
Blue Screen Of Death | Windows Down | Biggest IT failure
Dexbytes Infotech Pvt Ltd
 
Project Delivery Methodology on a page with activities, deliverables
Project Delivery Methodology on a page with activities, deliverablesProject Delivery Methodology on a page with activities, deliverables
Project Delivery Methodology on a page with activities, deliverables
CLIVE MINCHIN
 
The learners analyze the various sectors of ICT and evaluate the potential ca...
The learners analyze the various sectors of ICT and evaluate the potential ca...The learners analyze the various sectors of ICT and evaluate the potential ca...
The learners analyze the various sectors of ICT and evaluate the potential ca...
maricrismontales
 
UiPath Community Day Amsterdam presentations
UiPath Community Day Amsterdam presentationsUiPath Community Day Amsterdam presentations
UiPath Community Day Amsterdam presentations
UiPathCommunity
 
The Maritime Security. OSINT [EN] .pdf
The Maritime Security. OSINT [EN]   .pdfThe Maritime Security. OSINT [EN]   .pdf
The Maritime Security. OSINT [EN] .pdf
Snarky Security
 
Getting Started with Azure AI Studio.pptx
Getting Started with Azure AI Studio.pptxGetting Started with Azure AI Studio.pptx
Getting Started with Azure AI Studio.pptx
Swaminathan Vetri
 
Increase Quality with User Access Policies - July 2024
Increase Quality with User Access Policies - July 2024Increase Quality with User Access Policies - July 2024
Increase Quality with User Access Policies - July 2024
Peter Caitens
 
TrustArc Webinar - Innovating with TRUSTe Responsible AI Certification
TrustArc Webinar - Innovating with TRUSTe Responsible AI CertificationTrustArc Webinar - Innovating with TRUSTe Responsible AI Certification
TrustArc Webinar - Innovating with TRUSTe Responsible AI Certification
TrustArc
 
FIDO Munich Seminar FIDO Automotive Apps.pptx
FIDO Munich Seminar FIDO Automotive Apps.pptxFIDO Munich Seminar FIDO Automotive Apps.pptx
FIDO Munich Seminar FIDO Automotive Apps.pptx
FIDO Alliance
 
Webinar: Transforming Substation Automation with Open Source Solutions
Webinar: Transforming Substation Automation with Open Source SolutionsWebinar: Transforming Substation Automation with Open Source Solutions
Webinar: Transforming Substation Automation with Open Source Solutions
DanBrown980551
 
Generative AI technology is a fascinating field that focuses on creating comp...
Generative AI technology is a fascinating field that focuses on creating comp...Generative AI technology is a fascinating field that focuses on creating comp...
Generative AI technology is a fascinating field that focuses on creating comp...
Nohoax Kanont
 

Recently uploaded (20)

Flame Atomic Emission Spectroscopy.-pptx
Flame Atomic Emission Spectroscopy.-pptxFlame Atomic Emission Spectroscopy.-pptx
Flame Atomic Emission Spectroscopy.-pptx
 
Multimodal Embeddings (continued) - South Bay Meetup Slides
Multimodal Embeddings (continued) - South Bay Meetup SlidesMultimodal Embeddings (continued) - South Bay Meetup Slides
Multimodal Embeddings (continued) - South Bay Meetup Slides
 
Leading Bigcommerce Development Services for Online Retailers
Leading Bigcommerce Development Services for Online RetailersLeading Bigcommerce Development Services for Online Retailers
Leading Bigcommerce Development Services for Online Retailers
 
Mega MUG 2024: Working smarter in Marketo
Mega MUG 2024: Working smarter in MarketoMega MUG 2024: Working smarter in Marketo
Mega MUG 2024: Working smarter in Marketo
 
FIDO Munich Seminar Workforce Authentication Case Study.pptx
FIDO Munich Seminar Workforce Authentication Case Study.pptxFIDO Munich Seminar Workforce Authentication Case Study.pptx
FIDO Munich Seminar Workforce Authentication Case Study.pptx
 
Top keywords searches on home and garden
Top keywords searches on home and gardenTop keywords searches on home and garden
Top keywords searches on home and garden
 
STKI Israeli IT Market Study v2 August 2024.pdf
STKI Israeli IT Market Study v2 August 2024.pdfSTKI Israeli IT Market Study v2 August 2024.pdf
STKI Israeli IT Market Study v2 August 2024.pdf
 
TribeQonf2024_Dimpy_ShiftingSecurityLeft
TribeQonf2024_Dimpy_ShiftingSecurityLeftTribeQonf2024_Dimpy_ShiftingSecurityLeft
TribeQonf2024_Dimpy_ShiftingSecurityLeft
 
Jacquard Fabric Explained: Origins, Characteristics, and Uses
Jacquard Fabric Explained: Origins, Characteristics, and UsesJacquard Fabric Explained: Origins, Characteristics, and Uses
Jacquard Fabric Explained: Origins, Characteristics, and Uses
 
Blue Screen Of Death | Windows Down | Biggest IT failure
Blue Screen Of Death | Windows Down | Biggest IT failureBlue Screen Of Death | Windows Down | Biggest IT failure
Blue Screen Of Death | Windows Down | Biggest IT failure
 
Project Delivery Methodology on a page with activities, deliverables
Project Delivery Methodology on a page with activities, deliverablesProject Delivery Methodology on a page with activities, deliverables
Project Delivery Methodology on a page with activities, deliverables
 
The learners analyze the various sectors of ICT and evaluate the potential ca...
The learners analyze the various sectors of ICT and evaluate the potential ca...The learners analyze the various sectors of ICT and evaluate the potential ca...
The learners analyze the various sectors of ICT and evaluate the potential ca...
 
UiPath Community Day Amsterdam presentations
UiPath Community Day Amsterdam presentationsUiPath Community Day Amsterdam presentations
UiPath Community Day Amsterdam presentations
 
The Maritime Security. OSINT [EN] .pdf
The Maritime Security. OSINT [EN]   .pdfThe Maritime Security. OSINT [EN]   .pdf
The Maritime Security. OSINT [EN] .pdf
 
Getting Started with Azure AI Studio.pptx
Getting Started with Azure AI Studio.pptxGetting Started with Azure AI Studio.pptx
Getting Started with Azure AI Studio.pptx
 
Increase Quality with User Access Policies - July 2024
Increase Quality with User Access Policies - July 2024Increase Quality with User Access Policies - July 2024
Increase Quality with User Access Policies - July 2024
 
TrustArc Webinar - Innovating with TRUSTe Responsible AI Certification
TrustArc Webinar - Innovating with TRUSTe Responsible AI CertificationTrustArc Webinar - Innovating with TRUSTe Responsible AI Certification
TrustArc Webinar - Innovating with TRUSTe Responsible AI Certification
 
FIDO Munich Seminar FIDO Automotive Apps.pptx
FIDO Munich Seminar FIDO Automotive Apps.pptxFIDO Munich Seminar FIDO Automotive Apps.pptx
FIDO Munich Seminar FIDO Automotive Apps.pptx
 
Webinar: Transforming Substation Automation with Open Source Solutions
Webinar: Transforming Substation Automation with Open Source SolutionsWebinar: Transforming Substation Automation with Open Source Solutions
Webinar: Transforming Substation Automation with Open Source Solutions
 
Generative AI technology is a fascinating field that focuses on creating comp...
Generative AI technology is a fascinating field that focuses on creating comp...Generative AI technology is a fascinating field that focuses on creating comp...
Generative AI technology is a fascinating field that focuses on creating comp...
 

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. Parent-Child relationships Very common in web applications Categorise content Manage organisation structures Hierarchical Data
  • 3. Hierarchical Data – A tree Home Theatre Projectors Surround Sound Sub Woofers Satellites Speakers
  • 4. Common Category Storage “ Adjacency List Model” Stores serial of the parent
  • 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. Too much effort Recursive functions are confusing Self joins only good depth is known How else can I organise my categories?
  • 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. Less queries to read Better handling of large data sets Simpler/More readable code Why Bother?
  • 18. Category Manager Access Permissions Organisation Structures Current Applications
  • 19. Thankyou Special thanks to Arjen Lentz, Jonathan Oxer and Mike Hellyer for edumacating me. Questions?