![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
#3
| |||
| |||
|
|
[snip] |
#4
| |||
| |||
|
|
On Oct 5, 4:24*am, -CELKO- <jcelko... (AT) earthlink (DOT) net> wrote: [snip] Joe If I may ask, is this method of implementing RI for Exclusive Subtypes one of your original concepts (like the Nested Sets), or is it someone else's or general practice or whatever ? |
#5
| |||
| |||
|
|
The classic scenario calls for a root class with all the common attributes and then specialized sub-classes under it. As an example, let's take the class of Vehicles and find an industry standard identifier (VIN), and add two mutually exclusive sub-classes, Sport utility vehicles and sedans ('SUV', 'SED'). CREATE TABLE Vehicles (vin CHAR(17) NOT NULL PRIMARY KEY, vehicle_type CHAR(3) NOT NULL CHECK(vehicle_type IN ('SUV', 'SED')), UNIQUE (vin, vehicle_type), ..); Notice the overlapping candidate keys. I then use a compound candidate key (vin, vehicle_type) and a constraint in each sub-class table to assure that the vehicle_type is locked and agrees with the Vehicles table. Add some DRI actions and you are done: CREATE TABLE SUVs (vin CHAR(17) NOT NULL PRIMARY KEY, vehicle_type CHAR(3) DEFAULT 'SUV' NOT NULL CHECK(vehicle_type = 'SUV'), UNIQUE (vin, vehicle_type), FOREIGN KEY (vin, vehicle_type) REFERENCES Vehicles(vin, vehicle_type) ON UPDATE CASCADE ON DELETE CASCADE, ..); CREATE TABLE Sedans (vin CHAR(17) NOT NULL PRIMARY KEY, vehicle_type CHAR(3) DEFAULT 'SED' NOT NULL CHECK(vehicle_type = 'SED'), UNIQUE (vin, vehicle_type), FOREIGN KEY (vin, vehicle_type) REFERENCES Vehicles(vin, vehicle_type) ON UPDATE CASCADE ON DELETE CASCADE, ..); I can continue to build a hierarchy like this. For example, if I had a Sedans table that broke down into two-door and four-door sedans, I could a schema like this: CREATE TABLE Sedans (vin CHAR(17) NOT NULL PRIMARY KEY, vehicle_type CHAR(3) DEFAULT 'SED' NOT NULL CHECK(vehicle_type IN ('2DR', '4DR', 'SED')), UNIQUE (vin, vehicle_type), FOREIGN KEY (vin, vehicle_type) REFERENCES Vehicles(vin, vehicle_type) ON UPDATE CASCADE ON DELETE CASCADE, ..); CREATE TABLE TwoDoor (vin CHAR(17) NOT NULL PRIMARY KEY, vehicle_type CHAR(3) DEFAULT '2DR' NOT NULL CHECK(vehicle_type = '2DR'), UNIQUE (vin, vehicle_type), FOREIGN KEY (vin, vehicle_type) REFERENCES Sedans(vin, vehicle_type) ON UPDATE CASCADE ON DELETE CASCADE, ..); CREATE TABLE FourDoor (vin CHAR(17) NOT NULL PRIMARY KEY, vehicle_type CHAR(3) DEFAULT '4DR' NOT NULL CHECK(vehicle_type = '4DR'), UNIQUE (vin, vehicle_type), FOREIGN KEY (vin, vehicle_type) REFERENCES Sedans (vin, vehicle_type) ON UPDATE CASCADE ON DELETE CASCADE, ..); The idea is to build a chain of identifiers and types in a UNIQUE() constraint that go up the tree when you use a REFERENCES constraint. Obviously, you can do variants of this trick to get different class structures. If an entity doesn't have to be exclusively one subtype, you play with the root of the class hierarchy: CREATE TABLE Vehicles (vin CHAR(17) NOT NULL, vehicle_type CHAR(3) NOT NULL CHECK(vehicle_type IN ('SUV', 'SED')), PRIMARY KEY (vin, vehicle_type), ..); Now start hiding all this stuff in VIEWs immediately and add an INSTEAD OF trigger to those VIEWs. |
#6
| |||
| |||
|
|
Is there a reference on the web or book that fleshes this concept out? *Thanks much. |
#7
| |||
| |||
|
#8
| |||
| |||
|
|
The common approach is to just cram everything into one table with lots of nullable attributes to hide the conflation of fact types, then leave thewhole mess under the carpet so that the application code has to figure outthe real business model at run-time, every time |
#9
| |||
| |||
|
#10
| ||||||||||
| ||||||||||
|
|
Now you are obligated to actually post better techniques and explain why they are better.. |
|
I am not bothered by increasing the number of indexes. |
|
Sybase (nee WATCOM) |
|
You have made the assumption that references are done by more indexes. SQL implements *the constraints like *FOREIGN KEY (vin, vehicle_type) * REFERENCES Vehicles(vin, vehicle_type) by building pointer chains under the covers, so each compound key actually appears once in the schema. |
|
CREATE TABLE SUVs (vin CHAR(17) NOT NULL PRIMARY KEY, -- Index on (vin) for SUV.PK vehicle_type CHAR(3) DEFAULT 'SUV' NOT NULL CHECK(vehicle_type = 'SUV'), UNIQUE (vin, vehicle_type), -- Dupe Index [1] on (vin, vehicle_type) FOREIGN KEY (vin, vehicle_type) -- no suggestion that this FK is anadditional index REFERENCES Vehicles(vin, vehicle_type) ON UPDATE CASCADE ON DELETE CASCADE, ..); |
|
CREATE TABLE Vehicles (vin CHAR(17) NOT NULL PRIMARY KEY, -- Index on (vin) for Vehicle.PK vehicle_type CHAR(3) NOT NULL CHECK(vehicle_type IN ('SUV', 'SED')), UNIQUE (vin, vehicle_type), -- Dupe Index [2] on (vin, vehicle_type) ..); |
|
so each compound key actually appears once in the schema. |
|
Teradata uses a hashing algorithm. |
|
RDBMS companies have been working on PK-FK joins for a few decades, so they are pretty good at it now. |
|
What do you have against the Nested Sets? |
![]() |
| Thread Tools | |
| Display Modes | |
| |