Hierarchial
Hierarchial
Relational Databases
In RDBMS, R is for
Relational. What's all
this hierarchal
nonsense?
People
Implement using 3
Normal forms.
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
Employee
Employ
( 0,N )
Types
Can't express attribute
similarities or differences
Employee Types of similar types.
Electrician Does Electrical Work
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
type
for hierarchical mapping. ( T,E )
Specific - Generic
Grouping of similar entity types.
Similarities and differences are subtype A Subtype B Subtype C
defined. ( P,E )
(sub)type.
ERD - Hierarchical Constraints
C1 Property
{T} Total Coverage
Animals
{P} Partial Coverage ( C1, C2 ) Coverage Properties
C2 Property
Carnivore Herbivore
{E} Exclusive Coverage
{O} Overlapping Coverage
ERD - Entity type Groupings
Entity types having equal Animals
attributes are grouped together.
( T, O )
Similarities and differences are
defined.
Animals
id animalcode weight Carnivores Herbivores
Bear-1 bear 500
Sheep-2 sheep 100
Wolf-3 wolf 120
Deer-4 deer 240
Puma-5 puma 200
Carnivores Herbivores
id animalcode weight favoritePrey id animalcode weight favoriteVegi
Bear-1 bear 500 salmon Bear-1 bear 500 berries
Wolf-3 wolf 120 sheep Sheep-2 sheep 100 grass
Puma-5 puma 200 deer Deer-5 deer 240 grass
exist.
Some entities will map Avians Mammals Marsupials
to most of the
hierarchical attributes
but not all.
Careful consideration
required to minimize
platypus affect.*
*If practical, G/H redesign can eliminate most “Platypuses”.
ERD – Entity type Relationships
Complex Relationships Mauls
(0,n)
Animals
sub types
(0,n)
(1,n) (1,n)
Carnivores Fears Herbivores
Fears
carnivoreid animalid
Deer-4 Wolf-3
Deer-4 Puma-5 Maulings
Deer-4 Bear-1 carnivoreid animalid Mauling-date
Sheep-2 Wolf-3 Bear-1 Wolf-3 01/15/08
Sheep-2 Puma-5 Bear-1 Deer-4 07/12/07
Bear-1 Bear-6 Wolf-3 Sheep-2 09/22/07
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:
( 1, 1 ) ( 0, n ) Animal
Animals is a Types
Animals ( 0, n )
( T, O )
has
( 1, 1 )
Carnivores Herbivores
Animal ( 1, 1 ) ( 0, n ) Attribute
Attributes
is a Types
EAV Table - DDL
CREATE TABLE Animaltypes(
animalcode VARCHAR( 20 ) PRIMARY KEY,
description TEXT NOT NULL DEFAULT '' );
Animals
( T, O )
( 1, 1 ) ( 0, n ) Animal
Animals is a Types
Carnivores Herbivores
(NA) Table - DDL
CREATE TABLE Animaltypes(
animalcode VARCHAR( 20 ) PRIMARY KEY,
description TEXT NOT NULL DEFAULT '' );
( 1, 1 ) ( 0, n ) Animal
is a Types
Animals
( T, O )
( 0, 1 ) Animals ( 0, 1 )
Carnivores Herbivores
is a is a
( 1, 1 ) ( 1, 1 )
Carnivores Herbivores
(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 ),
( 1, 1 ) ( 0, n ) Animal
Animals is a Types
Animals
( T, O )
( 1, 1 ) ( 0, n )
Carnivores is a
Carnivores Herbivores ( 1, 1 ) ( 0, n )
Herbivores is a
(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 ));
Animals
( T, O )
( 1, 1 ) ( 0, n ) Animal
Animals is a Types
Carnivores Herbivores
(NA – EAV) Table - DDL
CREATE TABLE Animaltypes(
animalcode VARCHAR( 20 ) PRIMARY KEY,
description TEXT NOT NULL DEFAULT '' );
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
Ya, what's
all this
hierarchal
nonsense?