Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > Databases General > Re: Naming conv...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 16 of 17 Topic 3149 of 3295
Post > Topic >>

Re: Naming conventions for special database objects

by --CELKO-- <jcelko212@[EMAIL PROTECTED] > Apr 30, 2008 at 08:38 AM

>> That's not the kind of flags I was talking about; say we need to track
expenses and some expense types have an additional field [sic: column] to
be required in the form [sic: form? like on the input, non-DB side of the
house?]. I might add a column in the expense_types table to indicate
whether a datum is required. <<

If you want it to be required, why not use a DEFAULT and a NOT NULL
constraint?

>> The same way, if we write a social network where kids register their
quigzmo collection, the "races" table might have a has_tail column -- to
avoid asking tail lengths for races that have no tail. <<

What the heck is a "quigzmo"? Hey, remember I am old!  I still say
"hip hugger bell bottoms" and not "low erise flares" and use an abacus
for my check book.

>>  And with a clever trick we might change has_tail in
minimum_tail_length, where accepted values are 1 and NULL... I'm not sure
I like that <<

If I understand what a "quigzmo" (gotta google that!), then that would
a good choice.  Try this line of reasoning:
1) If a "quigzmo" can be modified to add a tail, then a length of zero
would make sense.
2) If a "quigzmo" cannot be modified to add a tail, then a length of
NULL makes sense.

>> A way to avoid the has_tail flag might be to explicitly state the
subset of tailed races... with

CREATE TABLE tailed_races
(race_code VARCHAR(10) PRIMARY KEY REFERENCES Races);

but I think it might not be convenient to grow tables like that.  <<

The classic scenario calls for a root class with all the common
attributes and then specialized sub-cl***** 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-cl*****, S****t
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 SUV
(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.
 




 17 Posts in Topic:
Naming conventions for special database objects
Philipp Post <Post.Phi  2008-04-27 13:13:06 
Re: Naming conventions for special database objects
--CELKO-- <jcelko212@[  2008-04-28 07:28:09 
Re: Naming conventions for special database objects
"Carl Kayser" &  2008-04-28 11:12:23 
Re: Naming conventions for special database objects
--CELKO-- <jcelko212@[  2008-04-28 13:00:10 
Re: Naming conventions for special database objects
Philipp Post <Post.Phi  2008-04-29 05:01:58 
Re: Naming conventions for special database objects
Marco Mariani <marco@[  2008-04-29 14:27:31 
Re: Naming conventions for special database objects
Philipp Post <Post.Phi  2008-04-29 06:09:15 
Re: Naming conventions for special database objects
Marco Mariani <marco@[  2008-04-29 16:11:07 
Re: Naming conventions for special database objects
--CELKO-- <jcelko212@[  2008-04-29 08:22:47 
Re: Naming conventions for special database objects
--CELKO-- <jcelko212@[  2008-04-29 08:41:10 
Re: Naming conventions for special database objects
Marco Mariani <marco@[  2008-04-30 11:13:44 
Re: Naming conventions for special database objects
Philipp Post <Post.Phi  2008-04-29 11:08:57 
Re: Naming conventions for special database objects
Marco Mariani <marco@[  2008-04-30 11:15:33 
Re: Naming conventions for special database objects
Philipp Post <Post.Phi  2008-04-29 11:16:55 
Re: Naming conventions for special database objects
--CELKO-- <jcelko212@[  2008-04-29 14:42:25 
Re: Naming conventions for special database objects
--CELKO-- <jcelko212@[  2008-04-30 08:38:46 
Re: Naming conventions for special database objects
Marco Mariani <marco@[  2008-04-30 18:39:22 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Dec 5 9:21:57 CST 2008.