--CELKO-- wrote:
>>> 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?].
Yes, I mentioned fields in the context of building a dynamic input form.
> 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?
Because the additional data is required only for some kind of expenses.
I might have an "expense_types" table, and a "expenses" table. Then I
use expenses_types to build the form.
In editing an expense, whenever the type changes, some widgets might
come up or disappear.
>>> 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"?
I made it up, sorry. I figured a kind of plastic monster where every
specimen is unique in colors and personalization (so that kids buy
several almost identical ones) but come in different "races" and each
race can have some features, or not.
When registering a kid's collection, I want to ask for the color of the
tail if and only if the race comes with a tail at all.
(the primary key is conveniently given by the factory, each one has a
guaranteed unique name printed on the bottom)
> 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.
Yes. I understand and already use this pattern in some places, to the
point I would avoid tools that don't sup****t compound foreign keys.
> 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.
But it doesn't seem to me that the form generation code would be more
readable than the version with some boolean column, at least not without
a strong library for generating dynamic SQL.
> 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.
Especially when it comes to business rules that can change very often
for unfathomable (to me) reasons, on small applications as opposed to
the ones that drive air****ts, I feel the burden of a tiny weeny boolean
column can preferable to a more complex schema.
Still, the very fact that I asked this question about design is because
every time I do some of the things you might despise, I ask myself if
it's appropriate, and try to keep a balance between pragmatism,
conceptual correctness and deadlines :)
Thank you for the clear answers.


|