Using SQL to Access Using SQL to Access Hierarchical Data StructuresHierarchical Data Structures
(Raising SQL’s Hierarchical Data Structure Processing to that of
XML’s)
By
Michael M. David
(Speaker Notes Available, Right Click in Full Screen Mode)
Copyright 2003 by Michael M David 2
Characteristics of Hierarchical Characteristics of Hierarchical StructuresStructures
1) Root nodes have only one occurrence
2) Children nodes have only one parent node
3) Parent nodes can have multiple child nodes also known as Siblings
4) Siblings and their descendents produce multiple legs
5) Children nodes can have zero to many occurrences
Copyright 2003 by Michael M David 3
Emp=3
Example Hierarchical StructureExample Hierarchical Structure
Company=1
Proj=2
Dpnd=2 Dpnd=4Dpnd=1 Dpnd=3
Proj=1 Emp=2
Emp=1
Copyright 2003 by Michael M David 4
Hierarchical Structure SemanticsHierarchical Structure Semantics
1) Sibling legs are related only by their common parent node, they are independent otherwise
2) Sibling leg occurrences are not related one-to-one,
more collectively associated 3) Selection based on one sibling node occurrence selects
all sibling occurrences from the other sibling leg
4) Selection criteria based on multiple sibling legs compares all combinations of occurrences related under the lowest common parent
5) Single unambiguous path to each node
Copyright 2003 by Michael M David 5
Emp=3 Emp=2
Hierarchical Query Semantics Hierarchical Query Semantics Examples 1 of 2
1) SELECT Emp, Proj WHERE Proj=22) SELECT Company WHERE Dpnd=1 AND Proj=23) SELECT Company WHERE Dpnd=1 AND Emp=2
Company=1
Proj=2
Dpnd=2 Dpnd=4Dpnd=1
Emp=1
Dpnd=3
Proj=1
Copyright 2003 by Michael M David 6
Emp=3 Emp=2
Hierarchical Query SemanticsHierarchical Query Semantics Examples 2 of 2 4) SELECT Emp, Proj WHERE Emp=1 OR Proj=95) SELECT Emp, Proj WHERE Emp=8 OR Proj=26) SELECT Emp, Proj WHERE Emp=1 OR Proj=2
Company=1
Proj=2
Dpnd=2 Dpnd=4Dpnd=1
Emp=1
Dpnd=3
Proj=1
Copyright 2003 by Michael M David 7
Relating Hierarchical and Relating Hierarchical and Relational Query ProcessingRelational Query Processing
Cartesian Product Based Result Sets Naturally Support Hierarchical Semantics:
1) Selection based on one sibling node occurrence selects all sibling occurrences from the other sibling leg
2) Selection based on multiple sibling legs compares all combinations of occurrences under their lowest common parent node
Copyright 2003 by Michael M David 8
Relational Working Set ofRelational Working Set of:SELECT * FROM Company, Emp, Dpnd, ProjWhere CoKey=EmpCoKey AND CoKey=ProjCoKey AND EmpKey=DpndEmpKey
Company=1 Emp=1 Dpnd=1 Proj=1
Company=1 Emp=1 Dpnd=2 Proj=1
Company=1 Emp=2 Dpnd=3 Proj=1
Company=1 Emp=2 Dpnd=4 Proj=1
Company=1 Emp=1 Dpnd=1 Proj=2
Company=1 Emp=1 Dpnd=2 Proj=2
Company=1 Emp=2 Dpnd=3 Proj=2
Company=1 Emp=2 Dpnd=4 Proj=2
Copyright 2003 by Michael M David 9
Relational Queries Produce Same Relational Queries Produce Same Result as Hierarchical 1 of 2 Result as Hierarchical 1 of 2 1) SELECT Emp, Proj … WHERE Proj=22) SELECT Company … WHERE Dpnd=4 AND Proj=23) SELECT Company ... WHERE Dpnd=1 AND Emp=2
Company=1 Emp=1 Dpnd=1 Proj=1
Company=1 Emp=1 Dpnd=2 Proj=1
Company=1 Emp=2 Dpnd=3 Proj=1
Company=1 Emp=2 Dpnd=4 Proj=1
Company=1 Emp=1 Dpnd=1 Proj=2
Company=1 Emp=1 Dpnd=2 Proj=2
Company=1 Emp=2 Dpnd=3 Proj=2
Company=1 Emp=2 Dpnd=4 Proj=2
Copyright 2003 by Michael M David 10
Relational Queries Produce Same Relational Queries Produce Same Result as Hierarchical Queries 2 of 2Result as Hierarchical Queries 2 of 2
4) SELECT Emp, Proj … WHERE Emp=1 OR Proj=95) SELECT Emp, Proj … WHERE Emp=8 OR Proj=26) SELECT Emp, Proj... WHERE Emp=1 OR Proj=2
Proj=2Dpnd=4Emp=2Company=1
Proj=2Dpnd=3Emp=2Company=1
Proj=2Dpnd=2Emp=1Company=1
Proj=2Dpnd=1Emp=1Company=1
Proj=1Dpnd=4Emp=2Company=1
Proj=1Dpnd=3Emp=2Company=1
Proj=1Dpnd=2Emp=1Company=1
Proj=1Dpnd=1Emp=1Company=1
Copyright 2003 by Michael M David 11
SQL-92 Outer Join OverviewSQL-92 Outer Join Overview
SELECT … FROM TableRef [,TableRef]…
WHERE …
TableRef is: TableName | ViewName | Join1 | Join2
Join1 is: [(]TableRef JoinType Join TableRef
JoinSpec[)]
Join2 is:[(]TableRef Natural JoinType Join
TableRef [)]
JoinType is: LEFT | RIGHT | FULL | INNER
JoinSpec is: ON JoinCond | USING (ColNameList)
Copyright 2003 by Michael M David 12
Describing Hierarchical StructuresDescribing Hierarchical Structures
Employee Structure:
Employee Over Company Related By CoKey=EmpCoKey Over Dependent Related By EmpKey=DpndEmpKey
Company Structure:
Company Over Employee Related By CoKey=EmpCoKey Over Dependent Related By EmpKey=DpndEmpKey
Company
Employee
Dependent
Employee
Company Dependent
Copyright 2003 by Michael M David 13
Modeling Hierarchical StructuresModeling Hierarchical Structures
Employee View:
SELECT * FROM Employee LEFT JOIN Company ON CoKey=EmpCoKey LEFT JOIN Dependent ON EmpKey=DpndEmpKey
Company View:
SELECT * FROM Company LEFT JOIN Employee ON CoKey=EmpCoKey LEFT JOIN Dependent ON EmpKey=DpndEmpKey
Company
Employee
Dependent
Employee
Company Dependent
Copyright 2003 by Michael M David 14
Any Data Structure Can be ModeledAny Data Structure Can be ModeledCreate View mmbook asSelect * From BookLeft Join Contents On Book=ContentsLeft Join ChapterOn Book=ChapterLeft Join Index On Book=IndexLeft Join Section On Chapter=SectionLeft Join TextOn Section=TextLeft Join Audio On Section=AudioLeft Join Scene On Section=SceneLeft Join Clip On Scene=Clip
Book
Contents Chapter Index
Section
Text Audio Scene
Clip
Copyright 2003 by Michael M David 15
Data Structure ExtractionData Structure ExtractionSelect * From BookLeft Join Contents On Book=ContentsLeft Join ChapterOn Book=ChapterLeft Join Index On Book=IndexLeft Join Section On Chapter=SectionLeft Join Text On Section=TextLeft Join Audio On Section=AudioLeft Join Scene On Section=SceneLeft Join Clip On Scene=Clip
Table Table Struct Parent No. Name Level No. 1 Book 1 0 2 Content 2 1 3 Chapter 2 1 4 Index 2 1 5 Section 3 3 6 Text 4 5 7 Audio 4 5 8 Scene 4 5 9 Clip 5 8
Copyright 2003 by Michael M David 16
Processing Hierarchical StructuresProcessing Hierarchical Structures
Company View:
SELECT * FROM Company LEFT JOIN Employee ON CoKey=EmpCoKey LEFT JOIN Dependent ON EmpKey=DpndEmpKey LEFT JOIN Project ON CoKey=ProjCoKey
• Left arg of Left Join preserved with missing right arg• Result of preceding joins fed into following join• Once missing value found, rest of current leg occurrence is null (missing)
Company
Employee
Dependent
Project
Copyright 2003 by Michael M David 17
Working Set ofWorking Set of Company Hierarchical ViewCompany Hierarchical View
SELECT * FROM Company LEFT JOIN Employee ON CoKey=EmpCoKey LEFT JOIN Dependent ON EmpKey=DpndEmpKey LEFT JOIN Project ON CoKey=ProjCoKey WHERE Proj=1
Company=1 Emp=1 Dpnd=1 Proj=1
Company=1 Emp=1 Dpnd=2 Proj=1
Company=1 Emp=2 Dpnd=3 Proj=1
Company=1 Emp=2 Dpnd=4 Proj=1
Company=1 Emp=3 Dpnd=Null Proj=1
Company=1 Emp=3 Dpnd=Null Proj=1
Copyright 2003 by Michael M David 18
Substructure SupportSubstructure SupportCREATE View EmpView as SELECT * FROM Employee LEFT JOIN Dependent
ON EmpKey=DpndEmpKey
SELECT * FROM Company LEFT JOIN EmpView ON CoKey=EmpCoKey SELECT * FROM Company LEFT JOIN Employee LEFT JOIN Dependent ON EmpKey=DpndEmpKey ON CoKey=EmpCoKey
Company
Employee
Dependent
Copyright 2003 by Michael M David 19
Hierarchical Access OptimizationHierarchical Access Optimization
Create View CoView ASSELECT * FROM CompanyLEFT JOIN Project ON CoKey=ProjCoKeyLEFT JOIN Employee ON CoKey=EmpCoKeyLEFT JOIN Dependent ON EmpKey=DpndEmpKey
Optimized View:SELECT CoName, DpndName From CoView
SELECT CoName, CoName FROM Company LEFT JOIN Employee ON CoKey=EmpCoKeyLEFT JOIN Dependent ON EmpKey=DpndEmpKey
Company
Project Employee
Dependent
Company
Employee
Dependent
Copyright 2003 by Michael M David 20
Hierarchical Data FilteringHierarchical Data FilteringSELECT * FROM CompanyLEFT JOIN Project ON CoKey=ProjectCoKey
AND ProjType=“Software”LEFT JOIN Employee ON CoKey=EmpCoKey
AND EmpStat=“Fulltime”LEFT JOIN Dependent ON EmpKey=DpndEmpKey
Company
Project Employee
Dependent
Copyright 2003 by Michael M David 21
SQL to XML MappingSQL to XML MappingSELECT * FROM EmployeeLEFT JOIN CompanyON CoKey=EmpCoKeyLEFT JOIN DependentON EmpKey=DpndEmpKeyFOR XML Element Mapping
Employee DataEmp Co DpndF DpndLMike CoA Jay RoeMike CoA Jane DoeMary CoA Sam Foe
<?xml Version=‘1.0’?><EmpView><Employee><Emp>Mike</Emp><Company>CoA</Company><Dependent><Dpndf>Jay</DpndF><DpndL>Roe</DpndL></Dependent>
:
Copyright 2003 by Michael M David 22
XML to SQL MappingXML to SQL MappingSELECT * FROM EmployeeLEFT JOIN CompanyON CoKey=EmpCoKeyLEFT JOIN DependentON EmpKey=DpndEmpKey
Employee DataEmp Co DpndF DpndLMike CoA Jay RoeMike CoA Jane DoeMary CoA Sam Foe
<?xml Version=‘1.0’?><EmpView><Employee><Emp>Mike</Emp><Company>CoA</Company><Dependent><Dpndf>Jay</DpndF><DpndL>Roe</DpndL></Dependent>
:
Copyright 2003 by Michael M David 23
SQL-Based Access of XMLSQL-Based Access of XMLSELECT * FROM EmployeeLEFT JOIN CompanyON CoKey=EmpCoKeyLEFT JOIN DependentON EmpKey=DpndEmpKey
Employee DataEmp Co DpndF DpndLMike CoA Jay RoeMike CoA Jane DoeMary CoA Sam Foe
<?xml Version=‘1.0’?><EmpView><Employee><Emp>Mike</Emp><Company>CoA</Company><Dependent><Dpndf>Jay</DpndF><DpndL>Roe</DpndL></Dependent>
:
Copyright 2003 by Michael M David 24
Element Node PromotionElement Node Promotion
SELECT X, Z FROM ViewX
ViewX modeled Structure Output Structure
X
Y
Z
X
Z
Copyright 2003 by Michael M David 25
Native XML IntegrationNative XML IntegrationSELECT * FROM ViewR LEFT JOIN ViewXN ON …
A
B C X
Y Z
A B C X Y Z
A
B C
X
Y Z
A B C XYZGet XML
Client … ViewR LEFT JOIN ViewX … Server
Relational XML
Native XML
Output XML Doc
Copyright 2003 by Michael M David 26
Joinless Native XML AccessJoinless Native XML Access
B1
A1
B2C1
C2
A1 B1 C1 A1 B1 C2 A1 B2 C1 A1 B2 C2
A2
B3B4
C3C4
A2 B3 C3 A2 B3 C4 A2 B4 C3 A2 B4 C4
SQL Engine
Copyright 2003 by Michael M David 27
Modeling Duplicate Element nodes Modeling Duplicate Element nodes Using Hierarchical StructuresUsing Hierarchical Structures
SELECT Addr1, Addr2 From Company
LEFT JOIN Department ON CoKey=DeptCoKey
LEFT JOIN Employee ON CoKey=EmpCoKey
LEFT JOIN Address AS Addr1
ON Addr1.AddrKey=DeptAddrKey
LEFT JOIN Address AS Addr2
ON Addr2.AddrKey=EmpAddrKey
Company
Department Employee
Addr2Addr1
Copyright 2003 by Michael M David 28
Modeling IDREFed Element Nodes Modeling IDREFed Element Nodes Using Hierarchical StructuresUsing Hierarchical Structures
SELECT * From Department LEFT JOIN Project AS Proj1 ON DeptKey=ProjDeptKey LEFT JOIN Employee ON DeptKey=EmpDeptKey LEFT JOIN Project AS Proj2 ON EmpKey=Proj2.IDRefEmp <Department> ….<Proj ID=“X”> … <Proj/><Proj ID=“Y”> … <Proj/><Employee IDREFS=“X,Y”> John <Employee/>
Department
Proj1 Employee
Proj2
Copyright 2003 by Michael M David 29
Symmetric Table LinkingSymmetric Table Linking
SELECT * FROM CompanyLEFT JOIN EmployeeX FULL JOIN EmployeeY Using (EmpKey)ON CoKey=EmpCoKeyLEFT JOIN DependentOn EmpKey=DpndEmpKey
• Allows flat structures to be specified in the structure
• Flat substructures are created by the Full and Inner joins
Company
EmployeeX EmployeeY
Dependent
Copyright 2003 by Michael M David 30
Variable Structures
SELECT * FROM Publisher
LEFT JOIN Book ON PubKey=BookFKey
AND PublishType=“Book”
LEFT JOIN SONG PubKey=SongFKey
AND PublishType=“SONG”
Publisher Publisher
Book Song
This dynamic structure controlled by flag in root
Copyright 2003 by Michael M David 31
SQL-Powered Hierarchical SQL-Powered Hierarchical Processing EngineProcessing Engine
If SQL can perform hierarchical processing-
it can also drive a hierarchical engine for:• More accurate hierarchical results• Increased execution and memory efficiency• Heterogeneous and disparate access• Truly seamless integrated access• Distributed processing with linear scaling• Extended Support for XML irregular structure
Copyright 2003 by Michael M David 32
SQL Hierarchical ProcessingSQL Hierarchical ProcessingSELECT * FROM CompanyLEFT JOIN EmployeeON CoKey=EmpCoKeyLEFT JOIN DependentON EmpKey=DpndEmpKey
Co Emp DpndF DpndLXYZ Mike Jay Roe Jane Doe Mary Sam FoeABC John ---- ----LMN ---- ---- ----
Mike XYZ Mary XYZ John ABC
Jay Roe Mike Jane Doe Mike Sam Foe Mary
XYZ ABC LMN
Copyright 2003 by Michael M David 33
Suggested Hierarchical Ordering Support
SELECT * FROM A
ORDER BY A.fld
LEFT JOIN B ON A=B
ORDER BY B.fld
LEFT JOIN C ON B=C
ORDER BY C.fld
• Each node can be separately ordered• Ordering B does not unorder C
A
B C
Copyright 2003 by Michael M David 34
Suggested Hierarchical Quantifier Support
SELECT * FROM Dept
LEFT JOIN Emp
ON DeptNo=EmpDeptNo
LEFT JOIN Dpnd
ON EmpNo=DpndDeptNo
Where Dept HAS ALL DpndStatus=“Active”
Dept
EMP
Dpnd
Copyright 2003 by Michael M David 35
RecapRecap
The following features allow the ANSI SQL-92 outer join complete control over processing data structures and XML:
Structure Modeling Structure Processing Joining Structures Variable Structures Symmetric Linking
Hierarchical Filtering Substructure Modeling Access Optimization Element Sharing Heterogeneous Access
Copyright 2003 by Michael M David 36
For more information refer to the book:
Advanced ANSI SQL Data Modeling and Structure
Processing
From Artech-House Publishers