views:

110

answers:

4

I have a table a with primary key id and a table b that represents a specialized version of a (it has all the same characteristics to track as a does, plus some specific to its b-ness--the latter are all that are stored in b). If I decide to represent this by having b's primary key be also a foreign key to a.id, what's the proper terminology for b in relation to a?

A real world example might be a person table with student and teacher add-on tables. A student might also be a teacher (a TA for example) but they're both the same person.

I would call it a 'child table' of a but I already use that as a synonym for 'detail table', like lines on a purchase order, for example.

+1  A: 

Ok this is sort of off topic but first things first, why does B have all of A's columns? It should only have the added columns, ESPECIALLY if you are referencing A with a foriegn key.

"Add on" records are usually called "Detail(s)"

For example, lets say my Table A is "Cars" my Table B would be "CarDetails"

Neil N
Sorry, I wasn't clear with my wording, I'm going to edit it.
Kev
It's not a details table, though, because the FK *is* the PK. It's a 1:1 relationship, not a M:1.
Kev
wether its 1:1 or 1:M its still a details table.
Neil N
A: 

As Neil N said, you shouldn't have the columns in both places if you're referencing table A in table B through a foreign key.

What your describing sounds a bit like a parallel to inheritance in object oriented programming. Personally, I don't use any specific naming convention in this case. I name A what it is and I name B what it is. For example, I might have:

CREATE TABLE People
(
     people_id      INT             NOT NULL,
     first_name     VARCHAR(40)     NOT NULL,
     last_name      VARCHAR(40)     NOT NULL,
     ...
     CONSTRAINT PK_People PRIMARY KEY CLUSTERED (people_id)
)
GO

CREATE TABLE My_Application_Users
(
     people_id          INT             NOT NULL,
     user_name          VARCHAR(20)     NOT NULL,
     security_level     INT             NOT NULL,
     CONSTRAINT PK_My_Application_Users PRIMARY KEY CLUSTERED (people_id),
     CONSTRAINT UI_My_Application_Users_user_name UNIQUE (user_name)
)
GO

This is just an example, so please don't tell me that my name columns are too long or too short or that they should allow NULLs, etc. ;)

Tom H.
I don't have them both. `b` just has what's specific to `b`, plus it's FK/PK back to `a` for the more general information.
Kev
Yes, it's very much like OOP inheritance.
Kev
+3  A: 

Your design sounds like Concrete Table Inheritance.

I'd call table B a concrete table that extends table A.

The relationship is one-to-one.


Other answers have suggested storing only the columns specific to the extended table. This design would be called Class Table Inheritance.

Bill Karwin
Would it be improper to shorten it to calling `b` simply an *extension of `a`*? (I.e., does that have some other meaning in an SQL table context?)
Kev
Yes, Class Table Inheritance is the one. I at first made it sound like Concrete but corrected that after Neil's answer made me realize I hadn't described it clearly.
Kev
Sure, your alternative wording is fine. For more information on how to use these patterns well, get Martin Fowler's book PofEAA.
Bill Karwin
PostgreSQL supports a form table inheritance which does this, but it's not SQL-standard by any means.
fennec
@fennec: There is a table-inheritance syntax in standard SQL, but PostgreSQL doesn't follow the standard. Also, there are multiple cases where PostgreSQL's implementation causes anomalies so I don't recommend using it.
Bill Karwin
Yeah, I looked into PostgreSQL's table inheritance and the workarounds it looked like it would need for what I wanted to do seemed to be more headache than it was worth. Extension tables seemed to have almost all the benefits and none of the drawbacks.
Kev
A: 

what's the proper terminology for b in relation to a?

Table B is a child of Table A (the parent), because in order for a record to exist in the child, it must first exist in the parent.

Tables should be modeled based on either having one-to-many or many-to-one relationships depending on the context, and of those options they can be either optional or required. Tables that link two sets of lists together will relate to other tables in a many-to-one fashion for every table involved. For example, users, groups, and user_groups_xref - the user_groups_xref can support numerous specific user instances of a user records, and the same relationship to the groups table.

There's no point in one-to-one relationships - these should never be allowed to exist because it should only be one table.

OMG Ponies
There is a point. A `person` might not be a `student`--why bother having columns for student data in `person` then? And if you have several types of specializations, your tables would quickly come to have a lot of columns and usually be sparse. This is much more organized. That or if you go the Concrete route as in Bill's link, you end up with data duplication and have to worry about sync issues.
Kev
@Kev: That's what a type code column (with a foreign key) is for. Or the relationship could be modeled similar to the user/group. It all depends on business rules.
OMG Ponies
Not if a `person` is able to be more than one specialized type.
Kev