SlideShare una empresa de Scribd logo
1 de 30
Hierarchical data models in
Relational Databases
In RDBMS, R is for
RRelational. What's all
this hierarchal
nonsense?
Richard Broersma Jr.
Mangan Inc. (Systems Integrator)
PostgreSQL Enthusiast
Preview
 Controversy of Hierarchical data in Relational Databases
 Perceptions of Reality and Data modeling
 Logical ERDs for Generalization Hierarchies
 Exploring Physical implementations of logical ERDs
Controversy
 Network and Hierarchical database are
”things of the past.”
 Relational databases should be implemented
using entities and relationships described in
relational theory.
 Should Hierarchical modeling be avoided?
Basics of RDB Modeling
 Thinking in terms of
data modeling
 Entities
 Relationships
 Entity types
 Implement using 3
Normal forms.
Employ
Companies
People
Employee
Types
( 0,1 )
( 1,N )
( 0,N )
Perceptions of Reality
 Our Perceptions
 Entities & Relationships
 Classifications
 Taxonomy
 How do the
attributes of similar
type of entities
differ
Short-fall of Entity Types
 Employee Types table
 Can't express attribute
similarities or differences
of similar types.
 Can't define relationships
between people of
related types
Employ
Employee
Types
( 0,N )
Employee Types
Electrician Does Electrical Work
Engineer Does Engineering
Manager Manages others
President Presides over a company
Welder Welds
Employ
ABC TED Welder
ABC SANDY President
ACME RON Electrician
ACME JILL Engineer
BP DAVE Manager
Conclusion: If we need to know about the extended attributes of entity types or the
relationships between entity types, Hierarchical data modeling must be implemented.
Enter - ERD for Hierarchical Data
 Generalization Hierarchy
(logical modeling):
 Defines hierarchical constraints
for hierarchical mapping.
 Grouping of similar entity types.
 Similarities and differences are
defined.
 Relationships can be created
between entities of any
(sub)type.
type
subtype A Subtype B Subtype C
Sub-subtype A
Specific-Generic
( T,E )
( P,E )
ERD - Hierarchical Constraints
 C1
Property
 {T} Total Coverage
 {P} Partial Coverage
 C2
Property
 {E} Exclusive Coverage
 {O} Overlapping Coverage
( C1
, C2
) Coverage Properties
Animals
Carnivore Herbivore
ERD - Entity type Groupings
 Entity types having equal
attributes are grouped together.
 Similarities and differences are
defined.
Animals
Carnivores Herbivores
( T, O )
Animals
id weight
Bear-1 bear 500
Sheep-2 sheep 100
Wolf-3 wolf 120
Deer-4 deer 240
Puma-5 puma 200
animalcode
Carnivores
id weight
Bear-1 bear 500 salmon
Wolf-3 wolf 120 sheep
Puma-5 puma 200 deer
animalcode favoritePrey
Herbivores
id weight
Bear-1 bear 500 berries
Sheep-2 sheep 100 grass
Deer-5 deer 240 grass
animalcode favoriteVegi
id weight
Bear-1 bear 500 salmon berries
Omnivores (implied by Overlapping)
animalcode favoritePrey favoriteVegi
ERD - Entity type Groupings
 Beware of the “platypus”!
 Valid criticisms of G/H
exist.
 Some entities will map
to most of the
hierarchical attributes
but not all.
 Careful consideration
required to minimize
platypus affect.*
Animals
Avians Mammals
( T, O )
*If practical, G/H redesign can eliminate most “Platypuses”.
Marsupials
ERD – Entity type Relationships
 Complex Relationships
are possible between
sub types
Animals
Carnivores Herbivores
( T, O )
Mauls
Fears
(0,n)
(0,n)
(1,n)(1,n)
Maulings
carnivoreid animalid Mauling-date
Bear-1 Wolf-3 01/15/08
Bear-1 Deer-4 07/12/07
Wolf-3 Sheep-2 09/22/07
Fears
carnivoreid animalid
Deer-4 Wolf-3
Deer-4 Puma-5
Deer-4 Bear-1
Sheep-2 Wolf-3
Sheep-2 Puma-5
Bear-1 Bear-6
Physical Implementations
 There are 5 physical designs for implementing
logical Generalization Hierarchies
 Each physical design varies in the G/H
features that its able to implement
 Entity-Attribute-Value table (EAV) (Relational purists favorite)
 Null-able Attributes (NA) table (Happens overtime)
 Vertical Disjunctive Partitioning (VDP) table partitioning (My favorite)
 Horizontal Disjunctive Partitioning (HDP) table (i.e. PostgreSQL Table inheritance)
 Null-able attributes – EAV hybrid Table ( Worst Design Ever – know it to avoid it )
Good Design Guidelines
 Regardless of the physical implementation:
 Always include a type column associated with the
primary key.
 This is still the best way to identify an entities or
relationships type.
 CHECK( ... ) Constraints can then be implemented based on
the entity type *
 This will prevent data corruption at the server level that could
be caused by application bugs or lazy users.
* A tree that mirrors the structure of the Generalization Hierarchy can be used in
coordination with a custom lookup function can replace lengthy check constraints.
Entity Attribute Value (EAV)
 Physical Implementation:
Animals
Carnivores Herbivores
( T, O )
Animals
Animal
Attributes
has
Attribute
Typesis a
( 0, n )
( 1, 1 )
( 1, 1 ) ( 0, n )
Animal
Typesis a
( 0, n )( 1, 1 )
EAV Table - DDL
CREATE TABLE Animaltypes(
animalcode VARCHAR( 20 ) PRIMARY KEY,
description TEXT NOT NULL DEFAULT '' );
CREATE TABLE Animals (
animal_id VARCHAR( 20 ) PRIMARY KEY,
animalcode VARCHAR( 20 ) REFERENCES Animaltypes( animalcode )
ON UPDATE CASCADE,
weight NUMERIC( 7, 2) CHECK( weight > 0 ));
CREATE TABLE Attributetypes(
attributecode VARCHAR( 20 ) PRIMARY KEY,
description TEXT NOT NULL DEFAULT '' );
CREATE TABLE Animalattributes(
animal_id VARCHAR( 20 ) REFERENCES Animals( animal_id )
ON UPDATE CASCADE ON DELETE CASCADE,
attribute VARCHAR( 20 ) REFERENCES Attributetypes( attributecode )
ON UPDATE CASCADE,
att_value VARCHAR( 1000 ) NOT NULL,
PRIMARY KEY ( animal_id, attribute ));
EAV Table - Consideration
 Good
 Provides a flexible mechanism to record the
attributes associated with any entity.
 The flexible mechanism eliminates the
possibility of “platypuses”.
 This EAV design requires almost no
consideration of the nature of the applicable
hierarchical data and requires very little time to
implement ( cookie cutter)
 Bad
 Users or Application logic becomes responsible
to ensuring that all entities of a specific type will
have the required associated attributes. (no
DDL server constraints will work)
 The EAV table doesn't provide a mechanism to
create relationships between entities of different
sub-types.
 The EAV table does nothing to provide a
grouping of related entity types.
 The EAV table uses a VARCHAR column for all
attribute values regardless if Dates, timestamps,
integers, numerics or booleans would be more
appropriate
 The isn't a way to prevent bad data-entry. For
example nothing would prevent a user from
entering 'I like peanut butter.' for the attribute
value for Birthday
Null-able Attributes (NA) Table
 Physical Implementation:
Animals
Carnivores Herbivores
( T, O )
Animals Animal
Types
is a
( 0, n )( 1, 1 )
(NA) Table - DDL
CREATE TABLE Animaltypes(
animalcode VARCHAR( 20 ) PRIMARY KEY,
description TEXT NOT NULL DEFAULT '' );
CREATE TABLE Animals (
animal_id VARCHAR( 20 ) PRIMARY KEY,
animalcode VARCHAR( 20 ) REFERENCES Animaltypes( animalcode )
ON UPDATE CASCADE,
weight NUMERIC( 7, 2) CHECK( weight > 0 ),
favoriteprey VARCHAR( 20 ) REFERENCES Animaltypes( animalcode )
ON UPDATE CASCADE
CHECK( CASE WHEN animalcode=ANY('Bear', 'Wolf', 'Puma')
THEN favoriteprey IS NOT NULL
ELSE favoriteprey IS NULL END )
favoritevegi VARCHAR( 20 ) REFERENCES Vegitypes ( Vegicode )
ON UPDATE CASCADE
CHECK( CASE WHEN animalcode=ANY('Bear', 'Deer', 'Sheep')
THEN favoritevegi IS NOT NULL
ELSE favoritevegi IS NULL END )
);
NA Table - Consideration
 Good
 The most Common Hierarchical Table I've seen.
Is that a good thing?
 Provides a flexible mechanism to record the
attributes associated with any entity.
 All attributes values can be constrained with
foreign keys.
 This NA design requires almost not
consideration of the nature of the applicable
hierarchical data. Hierarchical attributes are
added via DDL as they are encounter during
runtime.
 Bad
 The NA table doesn't provide a mechanism to
create relationships between entities of different
sub-types.
 The NA table does nothing to provide a
grouping of related entity types.
 Fewer Checks required, but too many check
constraints can still hurt INSERT performance
 Tuples in the table can get to be too big with
many-many unused nulled columns.
 The concept of null gets obscured.
(VDP) Table
 Physical Implementation:
Animals
Carnivores Herbivores
( T, O )
Animals
Animal
Typesis a
( 0, n )( 1, 1 )
Carnivores Herbivores
is ais a
is a
( 0, 1 )
( 1, 1 )( 1, 1 )
( 0, 1 )
(VDP) Table - DDL
CREATE TABLE Animals (
animal_id VARCHAR( 20 ) UNIQUE NOT NULL,
animalcode VARCHAR( 20 ) REFERENCES Animaltypes( animalcode )
ON UPDATE CASCADE,
weight NUMERIC( 7, 2) CHECK( weight > 0 ),
PRIMARY KEY ( animal_id, animalcode )
--RI to handle denormalization of sub-tables );
CREATE TABLE Carnivore (
animal_id VARCHAR( 20 ) UNIQUE NOT NULL,
animalcode VARCHAR( 20 ) NOT NULL
CHECK( animalcode = ANY( 'Bear', 'Wolf', 'Puma' )),
favoriteprey VARCHAR( 20 ) REFERENCES Animaltypes( animalcode )
ON UPDATE CASCADE,
PRIMARY KEY ( animal_id, animalcode ),
FOREIGN KEY ( animal_id, animalcode ) REFERENCES Animals( animal_id, animalcode )
ON UPDATE CASCADE ON DELETE CASCADE,
--RI to handle denormalization of animalcode );
VDP Table - Consideration
 Good
 All attributes values can be constrained with
foreign keys.
 Almost all logical ERD concepts of
Generalizations Hierarchies can be
implemented with this design.
 Allow for relationships between all levels of
subtype entitles
 Allows for entity type grouping of related entities
 Bad
 Checks only required for Entity type field, but
too many check constraints can still hurt
INSERT performance
 VDP cannot enforce overlapping when required
by entity type.
 Additional Application logic required to handle
multiple INSERTs and UPDATEs to various
(sub)type tables
 Requires some denormalization to enforce data
integrity. Referential Integrity handles this
problem.
 This design requires the designer to be well
versed in the domain that is being modeled
(HDP) Table
 Physical Implementation:
Animals
Carnivores Herbivores
( T, O )
Animals
Animal
Typesis a
( 0, n )( 1, 1 )
Carnivores
Herbivores is a
is a
is a
( 1, 1 )
( 1, 1 )
( 0, n )
( 0, n )
(HDP) Table - DDL
CREATE TABLE Animals (
animal_id VARCHAR( 20 ) PRIMARY KEY,
animalcode VARCHAR( 20 ) REFERENCES Animaltypes( animalcode )
ON UPDATE CASCADE,
weight NUMERIC( 7, 2) CHECK( weight > 0 ));
CREATE TABLE Carnivore (
favoriteprey VARCHAR( 20 ))
INHERITS( Animals );
ALTER TABLE Carnivore
ADD CONSTRAINT Cornivore_animalcode_check_iscarnivore
CHECK( animalcode = ANY( 'Bear', 'Wolf', 'Puma' ));
CREATE TABLE Herbivore (
favoritevegi VARCHAR( 20 ))
INHERITS( Animals );
ALTER TABLE Herbivore
ADD CONSTRAINT Herbivore_animalcode_check_isHerbivore
CHECK( animalcode = ANY( 'Bear', 'Deer', 'Sheep' ));
HDP Table - Consideration
 Good
 All attributes values can be constrained with
foreign keys.
 Allow for relationships between hierarchical leaf
entitles
 Allows for entity type grouping of related entities
 The application logic is simplified since all
accesses to sub-entities are to a single table.
 Bad
 Checks only required for Entity type field, but
too many check constraints can still hurt
INSERT performance
 HDP correctly implement overlapping when
required by entity type.
 No relationships can be drawn between various
levels of the G/H.
 SLOW Sequential Scans are the only way to
search the Root or Branch nodes of the
hierarchy since scans on these tables are based
on UNION ALL queries.
 Uniqueness cannot be enforced across the
hierarchy.
 This design requires the designer to be well
versed in the domain that is being modeled
(NA – EAV) Hybrid Table
 Physical Implementation:
Animals
Carnivores Herbivores
( T, O )
Animals Animal
Types
is a
( 0, n )( 1, 1 )
(NA – EAV) Table - DDL
CREATE TABLE Animaltypes(
animalcode VARCHAR( 20 ) PRIMARY KEY,
description TEXT NOT NULL DEFAULT '' );
CREATE TABLE Animals (
animal_id VARCHAR( 20 ) PRIMARY KEY,
animalcode VARCHAR( 20 ) REFERENCES Animaltypes( animalcode )
ON UPDATE CASCADE,
column1 VARCHAR( 255 ), --The application maps the attributes of each
column2 VARCHAR( 255 ), --entity type to these intentionally vague
column3 VARCHAR( 255 ), --columns. Each entity type will have a unique
column4 VARCHAR( 255 ), --mapping for column1 thru column100.
column5 VARCHAR( 255 ),
column6 VARCHAR( 255 ), --Unmapped columns not needed by an entity type
column7 VARCHAR( 255 ), --may be treated as custom fields that the users
column8 VARCHAR( 255 ), --may use any way they see fit.
-- ...
column100 VARCHAR( 255 )
);
NA – EAV Table - Consideration
 Good
 Provides a flexible mechanism to record the
attributes associated with any entity.
 The flexible mechanism eliminates the
possibility of “platypuses”.
 Users or Application logic becomes responsible
to ensuring that all entities of a specific type will
have the required associated attributes. (no
DDL server constraints will work)
 The NAEAV table doesn't provide a mechanism
to create relationships between entities of
different sub-types.
 The NAEAV table does nothing to provide a
grouping of related entity types.
 The NAEAV table uses a VARCHAR column for
all attribute values regardless if Dates,
timestamps, integers, numerics or booleans
would be more appropriate
 The isn't a way to prevent bad data-entry. For
example nothing would prevent a user from
entering 'I like peanut butter.' for the attribute
value for Birthday
 Table design concept is badly de-normalized.
 Bad
 These VARCHAR columns have no meaning.
Each entity can map a column for a completely
unrelated attribute.
 The Application mapping becomes a major
source of data corruption bugs if mapping isn't
cleanly implemented or if entity type changes
are required overtime.
 If unmapped columns are exposed to the users
as custom column, there is not way to ensure
that various users will be consistent when
implementing these columns.
Bibliography
Works Cited
Batini, Carol, Stefano Ceri, and Shamkant B. Navathe. Conceptual Database Design: an Entity-Relationship Approach.
Redwood City, California 94065: The Benjamin/Cummings Company; Inc., 1992. 1-470.
Celko, Joe. SQL FOR SMARTIES: Advanced SQL Programming. 3rd ed. San Francisco California 94111: Morgan Kaufmann
Publications, 2005. 1-838.
Questions?
Ya, what's
all this
hierarchal
nonsense?

Más contenido relacionado

La actualidad más candente

Window functions with SQL Server 2016
Window functions with SQL Server 2016Window functions with SQL Server 2016
Window functions with SQL Server 2016Mark Tabladillo
 
data Normalization.pdf
data Normalization.pdfdata Normalization.pdf
data Normalization.pdfBijayNag1
 
Introduction to SQL Antipatterns
Introduction to SQL AntipatternsIntroduction to SQL Antipatterns
Introduction to SQL AntipatternsKrishnakumar S
 
Fundamentals of Database ppt ch03
Fundamentals of Database ppt ch03Fundamentals of Database ppt ch03
Fundamentals of Database ppt ch03Jotham Gadot
 
Advanced MySQL Query Tuning
Advanced MySQL Query TuningAdvanced MySQL Query Tuning
Advanced MySQL Query TuningAlexander Rubin
 
data modeling and models
data modeling and modelsdata modeling and models
data modeling and modelssabah N
 
Parameters in Tableau
Parameters in TableauParameters in Tableau
Parameters in TableauKanika Nagpal
 
Abap data dictionary
Abap data dictionaryAbap data dictionary
Abap data dictionarySmartGokul4
 
SQL Server Index and Partition Strategy
SQL Server Index and Partition StrategySQL Server Index and Partition Strategy
SQL Server Index and Partition StrategyHamid J. Fard
 
Tableau PPT Intro, Features, Advantages, Disadvantages
Tableau PPT Intro, Features, Advantages, DisadvantagesTableau PPT Intro, Features, Advantages, Disadvantages
Tableau PPT Intro, Features, Advantages, DisadvantagesBurn & Born
 
Data Wrangling_1.pptx
Data Wrangling_1.pptxData Wrangling_1.pptx
Data Wrangling_1.pptxPallabiSahoo5
 
entity-relationship-diagram-chen-&-crow -model.ppt
entity-relationship-diagram-chen-&-crow -model.pptentity-relationship-diagram-chen-&-crow -model.ppt
entity-relationship-diagram-chen-&-crow -model.pptIRWANBINISMAILKPMGur1
 

La actualidad más candente (20)

Window functions with SQL Server 2016
Window functions with SQL Server 2016Window functions with SQL Server 2016
Window functions with SQL Server 2016
 
data Normalization.pdf
data Normalization.pdfdata Normalization.pdf
data Normalization.pdf
 
Data models
Data modelsData models
Data models
 
Introduction to SQL Antipatterns
Introduction to SQL AntipatternsIntroduction to SQL Antipatterns
Introduction to SQL Antipatterns
 
Sql server T-sql basics ppt-3
Sql server T-sql basics  ppt-3Sql server T-sql basics  ppt-3
Sql server T-sql basics ppt-3
 
Fundamentals of Database ppt ch03
Fundamentals of Database ppt ch03Fundamentals of Database ppt ch03
Fundamentals of Database ppt ch03
 
SQL Views
SQL ViewsSQL Views
SQL Views
 
Advanced MySQL Query Tuning
Advanced MySQL Query TuningAdvanced MySQL Query Tuning
Advanced MySQL Query Tuning
 
Practical Object Oriented Models In Sql
Practical Object Oriented Models In SqlPractical Object Oriented Models In Sql
Practical Object Oriented Models In Sql
 
data modeling and models
data modeling and modelsdata modeling and models
data modeling and models
 
Parameters in Tableau
Parameters in TableauParameters in Tableau
Parameters in Tableau
 
Abap data dictionary
Abap data dictionaryAbap data dictionary
Abap data dictionary
 
Partitioning
PartitioningPartitioning
Partitioning
 
Ms sql-server
Ms sql-serverMs sql-server
Ms sql-server
 
SQL Server Index and Partition Strategy
SQL Server Index and Partition StrategySQL Server Index and Partition Strategy
SQL Server Index and Partition Strategy
 
Sql Constraints
Sql ConstraintsSql Constraints
Sql Constraints
 
Tableau PPT Intro, Features, Advantages, Disadvantages
Tableau PPT Intro, Features, Advantages, DisadvantagesTableau PPT Intro, Features, Advantages, Disadvantages
Tableau PPT Intro, Features, Advantages, Disadvantages
 
Data Wrangling_1.pptx
Data Wrangling_1.pptxData Wrangling_1.pptx
Data Wrangling_1.pptx
 
entity-relationship-diagram-chen-&-crow -model.ppt
entity-relationship-diagram-chen-&-crow -model.pptentity-relationship-diagram-chen-&-crow -model.ppt
entity-relationship-diagram-chen-&-crow -model.ppt
 
DbMs
DbMsDbMs
DbMs
 

Último

DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 

Último (20)

DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 

Hierarchical data models in Relational Databases

  • 1. Hierarchical data models in Relational Databases In RDBMS, R is for RRelational. What's all this hierarchal nonsense? Richard Broersma Jr. Mangan Inc. (Systems Integrator) PostgreSQL Enthusiast
  • 2. Preview  Controversy of Hierarchical data in Relational Databases  Perceptions of Reality and Data modeling  Logical ERDs for Generalization Hierarchies  Exploring Physical implementations of logical ERDs
  • 3. Controversy  Network and Hierarchical database are ”things of the past.”  Relational databases should be implemented using entities and relationships described in relational theory.  Should Hierarchical modeling be avoided?
  • 4. Basics of RDB Modeling  Thinking in terms of data modeling  Entities  Relationships  Entity types  Implement using 3 Normal forms. Employ Companies People Employee Types ( 0,1 ) ( 1,N ) ( 0,N )
  • 5. Perceptions of Reality  Our Perceptions  Entities & Relationships  Classifications  Taxonomy  How do the attributes of similar type of entities differ
  • 6. Short-fall of Entity Types  Employee Types table  Can't express attribute similarities or differences of similar types.  Can't define relationships between people of related types Employ Employee Types ( 0,N ) Employee Types Electrician Does Electrical Work Engineer Does Engineering Manager Manages others President Presides over a company Welder Welds Employ ABC TED Welder ABC SANDY President ACME RON Electrician ACME JILL Engineer BP DAVE Manager Conclusion: If we need to know about the extended attributes of entity types or the relationships between entity types, Hierarchical data modeling must be implemented.
  • 7. Enter - ERD for Hierarchical Data  Generalization Hierarchy (logical modeling):  Defines hierarchical constraints for hierarchical mapping.  Grouping of similar entity types.  Similarities and differences are defined.  Relationships can be created between entities of any (sub)type. type subtype A Subtype B Subtype C Sub-subtype A Specific-Generic ( T,E ) ( P,E )
  • 8. ERD - Hierarchical Constraints  C1 Property  {T} Total Coverage  {P} Partial Coverage  C2 Property  {E} Exclusive Coverage  {O} Overlapping Coverage ( C1 , C2 ) Coverage Properties Animals Carnivore Herbivore
  • 9. ERD - Entity type Groupings  Entity types having equal attributes are grouped together.  Similarities and differences are defined. Animals Carnivores Herbivores ( T, O ) Animals id weight Bear-1 bear 500 Sheep-2 sheep 100 Wolf-3 wolf 120 Deer-4 deer 240 Puma-5 puma 200 animalcode Carnivores id weight Bear-1 bear 500 salmon Wolf-3 wolf 120 sheep Puma-5 puma 200 deer animalcode favoritePrey Herbivores id weight Bear-1 bear 500 berries Sheep-2 sheep 100 grass Deer-5 deer 240 grass animalcode favoriteVegi id weight Bear-1 bear 500 salmon berries Omnivores (implied by Overlapping) animalcode favoritePrey favoriteVegi
  • 10. ERD - Entity type Groupings  Beware of the “platypus”!  Valid criticisms of G/H exist.  Some entities will map to most of the hierarchical attributes but not all.  Careful consideration required to minimize platypus affect.* Animals Avians Mammals ( T, O ) *If practical, G/H redesign can eliminate most “Platypuses”. Marsupials
  • 11. ERD – Entity type Relationships  Complex Relationships are possible between sub types Animals Carnivores Herbivores ( T, O ) Mauls Fears (0,n) (0,n) (1,n)(1,n) Maulings carnivoreid animalid Mauling-date Bear-1 Wolf-3 01/15/08 Bear-1 Deer-4 07/12/07 Wolf-3 Sheep-2 09/22/07 Fears carnivoreid animalid Deer-4 Wolf-3 Deer-4 Puma-5 Deer-4 Bear-1 Sheep-2 Wolf-3 Sheep-2 Puma-5 Bear-1 Bear-6
  • 12. Physical Implementations  There are 5 physical designs for implementing logical Generalization Hierarchies  Each physical design varies in the G/H features that its able to implement  Entity-Attribute-Value table (EAV) (Relational purists favorite)  Null-able Attributes (NA) table (Happens overtime)  Vertical Disjunctive Partitioning (VDP) table partitioning (My favorite)  Horizontal Disjunctive Partitioning (HDP) table (i.e. PostgreSQL Table inheritance)  Null-able attributes – EAV hybrid Table ( Worst Design Ever – know it to avoid it )
  • 13. Good Design Guidelines  Regardless of the physical implementation:  Always include a type column associated with the primary key.  This is still the best way to identify an entities or relationships type.  CHECK( ... ) Constraints can then be implemented based on the entity type *  This will prevent data corruption at the server level that could be caused by application bugs or lazy users. * A tree that mirrors the structure of the Generalization Hierarchy can be used in coordination with a custom lookup function can replace lengthy check constraints.
  • 14. Entity Attribute Value (EAV)  Physical Implementation: Animals Carnivores Herbivores ( T, O ) Animals Animal Attributes has Attribute Typesis a ( 0, n ) ( 1, 1 ) ( 1, 1 ) ( 0, n ) Animal Typesis a ( 0, n )( 1, 1 )
  • 15. EAV Table - DDL CREATE TABLE Animaltypes( animalcode VARCHAR( 20 ) PRIMARY KEY, description TEXT NOT NULL DEFAULT '' ); CREATE TABLE Animals ( animal_id VARCHAR( 20 ) PRIMARY KEY, animalcode VARCHAR( 20 ) REFERENCES Animaltypes( animalcode ) ON UPDATE CASCADE, weight NUMERIC( 7, 2) CHECK( weight > 0 )); CREATE TABLE Attributetypes( attributecode VARCHAR( 20 ) PRIMARY KEY, description TEXT NOT NULL DEFAULT '' ); CREATE TABLE Animalattributes( animal_id VARCHAR( 20 ) REFERENCES Animals( animal_id ) ON UPDATE CASCADE ON DELETE CASCADE, attribute VARCHAR( 20 ) REFERENCES Attributetypes( attributecode ) ON UPDATE CASCADE, att_value VARCHAR( 1000 ) NOT NULL, PRIMARY KEY ( animal_id, attribute ));
  • 16. EAV Table - Consideration  Good  Provides a flexible mechanism to record the attributes associated with any entity.  The flexible mechanism eliminates the possibility of “platypuses”.  This EAV design requires almost no consideration of the nature of the applicable hierarchical data and requires very little time to implement ( cookie cutter)  Bad  Users or Application logic becomes responsible to ensuring that all entities of a specific type will have the required associated attributes. (no DDL server constraints will work)  The EAV table doesn't provide a mechanism to create relationships between entities of different sub-types.  The EAV table does nothing to provide a grouping of related entity types.  The EAV table uses a VARCHAR column for all attribute values regardless if Dates, timestamps, integers, numerics or booleans would be more appropriate  The isn't a way to prevent bad data-entry. For example nothing would prevent a user from entering 'I like peanut butter.' for the attribute value for Birthday
  • 17. Null-able Attributes (NA) Table  Physical Implementation: Animals Carnivores Herbivores ( T, O ) Animals Animal Types is a ( 0, n )( 1, 1 )
  • 18. (NA) Table - DDL CREATE TABLE Animaltypes( animalcode VARCHAR( 20 ) PRIMARY KEY, description TEXT NOT NULL DEFAULT '' ); CREATE TABLE Animals ( animal_id VARCHAR( 20 ) PRIMARY KEY, animalcode VARCHAR( 20 ) REFERENCES Animaltypes( animalcode ) ON UPDATE CASCADE, weight NUMERIC( 7, 2) CHECK( weight > 0 ), favoriteprey VARCHAR( 20 ) REFERENCES Animaltypes( animalcode ) ON UPDATE CASCADE CHECK( CASE WHEN animalcode=ANY('Bear', 'Wolf', 'Puma') THEN favoriteprey IS NOT NULL ELSE favoriteprey IS NULL END ) favoritevegi VARCHAR( 20 ) REFERENCES Vegitypes ( Vegicode ) ON UPDATE CASCADE CHECK( CASE WHEN animalcode=ANY('Bear', 'Deer', 'Sheep') THEN favoritevegi IS NOT NULL ELSE favoritevegi IS NULL END ) );
  • 19. NA Table - Consideration  Good  The most Common Hierarchical Table I've seen. Is that a good thing?  Provides a flexible mechanism to record the attributes associated with any entity.  All attributes values can be constrained with foreign keys.  This NA design requires almost not consideration of the nature of the applicable hierarchical data. Hierarchical attributes are added via DDL as they are encounter during runtime.  Bad  The NA table doesn't provide a mechanism to create relationships between entities of different sub-types.  The NA table does nothing to provide a grouping of related entity types.  Fewer Checks required, but too many check constraints can still hurt INSERT performance  Tuples in the table can get to be too big with many-many unused nulled columns.  The concept of null gets obscured.
  • 20. (VDP) Table  Physical Implementation: Animals Carnivores Herbivores ( T, O ) Animals Animal Typesis a ( 0, n )( 1, 1 ) Carnivores Herbivores is ais a is a ( 0, 1 ) ( 1, 1 )( 1, 1 ) ( 0, 1 )
  • 21. (VDP) Table - DDL CREATE TABLE Animals ( animal_id VARCHAR( 20 ) UNIQUE NOT NULL, animalcode VARCHAR( 20 ) REFERENCES Animaltypes( animalcode ) ON UPDATE CASCADE, weight NUMERIC( 7, 2) CHECK( weight > 0 ), PRIMARY KEY ( animal_id, animalcode ) --RI to handle denormalization of sub-tables ); CREATE TABLE Carnivore ( animal_id VARCHAR( 20 ) UNIQUE NOT NULL, animalcode VARCHAR( 20 ) NOT NULL CHECK( animalcode = ANY( 'Bear', 'Wolf', 'Puma' )), favoriteprey VARCHAR( 20 ) REFERENCES Animaltypes( animalcode ) ON UPDATE CASCADE, PRIMARY KEY ( animal_id, animalcode ), FOREIGN KEY ( animal_id, animalcode ) REFERENCES Animals( animal_id, animalcode ) ON UPDATE CASCADE ON DELETE CASCADE, --RI to handle denormalization of animalcode );
  • 22. VDP Table - Consideration  Good  All attributes values can be constrained with foreign keys.  Almost all logical ERD concepts of Generalizations Hierarchies can be implemented with this design.  Allow for relationships between all levels of subtype entitles  Allows for entity type grouping of related entities  Bad  Checks only required for Entity type field, but too many check constraints can still hurt INSERT performance  VDP cannot enforce overlapping when required by entity type.  Additional Application logic required to handle multiple INSERTs and UPDATEs to various (sub)type tables  Requires some denormalization to enforce data integrity. Referential Integrity handles this problem.  This design requires the designer to be well versed in the domain that is being modeled
  • 23. (HDP) Table  Physical Implementation: Animals Carnivores Herbivores ( T, O ) Animals Animal Typesis a ( 0, n )( 1, 1 ) Carnivores Herbivores is a is a is a ( 1, 1 ) ( 1, 1 ) ( 0, n ) ( 0, n )
  • 24. (HDP) Table - DDL CREATE TABLE Animals ( animal_id VARCHAR( 20 ) PRIMARY KEY, animalcode VARCHAR( 20 ) REFERENCES Animaltypes( animalcode ) ON UPDATE CASCADE, weight NUMERIC( 7, 2) CHECK( weight > 0 )); CREATE TABLE Carnivore ( favoriteprey VARCHAR( 20 )) INHERITS( Animals ); ALTER TABLE Carnivore ADD CONSTRAINT Cornivore_animalcode_check_iscarnivore CHECK( animalcode = ANY( 'Bear', 'Wolf', 'Puma' )); CREATE TABLE Herbivore ( favoritevegi VARCHAR( 20 )) INHERITS( Animals ); ALTER TABLE Herbivore ADD CONSTRAINT Herbivore_animalcode_check_isHerbivore CHECK( animalcode = ANY( 'Bear', 'Deer', 'Sheep' ));
  • 25. HDP Table - Consideration  Good  All attributes values can be constrained with foreign keys.  Allow for relationships between hierarchical leaf entitles  Allows for entity type grouping of related entities  The application logic is simplified since all accesses to sub-entities are to a single table.  Bad  Checks only required for Entity type field, but too many check constraints can still hurt INSERT performance  HDP correctly implement overlapping when required by entity type.  No relationships can be drawn between various levels of the G/H.  SLOW Sequential Scans are the only way to search the Root or Branch nodes of the hierarchy since scans on these tables are based on UNION ALL queries.  Uniqueness cannot be enforced across the hierarchy.  This design requires the designer to be well versed in the domain that is being modeled
  • 26. (NA – EAV) Hybrid Table  Physical Implementation: Animals Carnivores Herbivores ( T, O ) Animals Animal Types is a ( 0, n )( 1, 1 )
  • 27. (NA – EAV) Table - DDL CREATE TABLE Animaltypes( animalcode VARCHAR( 20 ) PRIMARY KEY, description TEXT NOT NULL DEFAULT '' ); CREATE TABLE Animals ( animal_id VARCHAR( 20 ) PRIMARY KEY, animalcode VARCHAR( 20 ) REFERENCES Animaltypes( animalcode ) ON UPDATE CASCADE, column1 VARCHAR( 255 ), --The application maps the attributes of each column2 VARCHAR( 255 ), --entity type to these intentionally vague column3 VARCHAR( 255 ), --columns. Each entity type will have a unique column4 VARCHAR( 255 ), --mapping for column1 thru column100. column5 VARCHAR( 255 ), column6 VARCHAR( 255 ), --Unmapped columns not needed by an entity type column7 VARCHAR( 255 ), --may be treated as custom fields that the users column8 VARCHAR( 255 ), --may use any way they see fit. -- ... column100 VARCHAR( 255 ) );
  • 28. NA – EAV Table - Consideration  Good  Provides a flexible mechanism to record the attributes associated with any entity.  The flexible mechanism eliminates the possibility of “platypuses”.  Users or Application logic becomes responsible to ensuring that all entities of a specific type will have the required associated attributes. (no DDL server constraints will work)  The NAEAV table doesn't provide a mechanism to create relationships between entities of different sub-types.  The NAEAV table does nothing to provide a grouping of related entity types.  The NAEAV table uses a VARCHAR column for all attribute values regardless if Dates, timestamps, integers, numerics or booleans would be more appropriate  The isn't a way to prevent bad data-entry. For example nothing would prevent a user from entering 'I like peanut butter.' for the attribute value for Birthday  Table design concept is badly de-normalized.  Bad  These VARCHAR columns have no meaning. Each entity can map a column for a completely unrelated attribute.  The Application mapping becomes a major source of data corruption bugs if mapping isn't cleanly implemented or if entity type changes are required overtime.  If unmapped columns are exposed to the users as custom column, there is not way to ensure that various users will be consistent when implementing these columns.
  • 29. Bibliography Works Cited Batini, Carol, Stefano Ceri, and Shamkant B. Navathe. Conceptual Database Design: an Entity-Relationship Approach. Redwood City, California 94065: The Benjamin/Cummings Company; Inc., 1992. 1-470. Celko, Joe. SQL FOR SMARTIES: Advanced SQL Programming. 3rd ed. San Francisco California 94111: Morgan Kaufmann Publications, 2005. 1-838.