Re: Design question regarding data typing
Date: 24 Feb 2006 11:55:47 -0800
Message-ID: <1140810947.141505.17940_at_i40g2000cwc.googlegroups.com>
Bob Hairgrove wrote:
> On 24 Feb 2006 09:39:43 -0800, "Jacob JKW" <jacobcdf_at_yahoo.com> wrote:
>
> >Sure. I was trying to explain that one column of my Insitutions table
> >was the foreign key InstitutionTypeId which indexes the name (and
> >description) of the type of institution (which in this simplification
> >can be either "school" or "bank"). I have this in addition to separate
> >Banks and Schools tables. The reason for using the InstitutionType
> >descriptor (or as I gather it's called from Roy's post above,
> >"discriminant") is so that if I want summary instituition data from
> >Insitutions I can easily figure out the institution type without having
> >to scan for each InstutionId foreign key of interest in Banks Schools.
> >And that's the kludge that's causing me such consternation. :-( It just
> >reeks of bad form.
>
> Yes, InstitutionTypeId is redundant data if you already have separate
> tables as described. I don't see the need for it unless for some
> reason you want to avoid the join or subquery which would be necessary
> without it should you want to group or sort by schools or banks. A
> simple natural join of two tables on their primary key columns is
> going to perform very fast in every RDBMS that I know of ... you'll
> never have to do a full table scan for that.
OK. But I don't actually see how I could accomplish this with a "simple
natural join". To me it seems I'd have to use a union. Let's say I've
got this:
CREATE TABLE buildings (
building_id VARCHAR(100) NOT NULL, building_color VARCHAR(100) NOT NULL, building_type VARCHAR(100) NOT NULL,
- other columns... CONSTRAINT pk_buildings PRIMARY KEY (building_id));
CREATE TABLE schools (
building_id VARCHAR(100) NOT NULL,
school_name VARCHAR(100) NOT NULL,
- other columns... CONSTRAINT pk_schools PRIMARY KEY (building_id), CONSTRAINT uk_schools UNIQUE (school_name), CONSTRAINT fk_schools FOREIGN KEY (building_id) REFERENCES buildings (building_id));
CREATE TABLE banks (
building_id VARCHAR(100) NOT NULL,
BIC VARCHAR(10) NOT NULL,
- other columns... CONSTRAINT pk_banks PRIMARY KEY (building_id), CONSTRAINT uk_banks UNIQUE (BIC), CONSTRAINT fk_banks FOREIGN KEY (building_id) REFERENCES buildings (building_id));
Now let's say all I'm looking for is a table of building_id's, building_color's, and building_type's. That's just:
SELECT building_id, building_color, building_type FROM buildings;