views:

45

answers:

3

Hi,

i was wondering if there's a difference between modeling a one-to-one relation and a inheritance relation (Table-Per-Type) In both cases we add a foreing key with a UNIQUE constraint, or somethimes the FK is part of the PK

It seems to me that the table structure is the same, but maybe i'm missing something.

Thanks in advance.

+1  A: 

When it comes to modelling, a one-to-one relationship between entities indicates that they are the same relational table. "The key, the whole key and nothing but the key, so help me Codd!"

Mark Bannister
Only when the relationship is also total at both ends.
reinierpost
im lost here.. we could store two object (that are in a one-to-one relation between each other) in one single table, and we could also store them in two tables sharing the same PK.. but wich one is better? or maybe im thinking the wrong way =)
pleasedontbelong
@pleasedontbelong: from a viewpoint of relational modelling, they are the same table. However, when it comes to *implementation* of an object-oriented schema in a SQL database, you will probably be better off with two separate tables.
Mark Bannister
One reason for splitting them might be, for instance, that not all users of the database have access rights to all tables.
reinierpost
+2  A: 

There are a few permutations of this. Here are some: - Suppose A can exist only with B and B can can exist only with A. Then the relationship is one to one. - Suppose A can exist alone and B can extend it, but B cannot exist alone. Then the relationship is inheritance. - Suppose A cannot exist alone, but it can exist with either B or C. Then the relationship is inheritance.

Michael Burrows
i'd say that the second exemple is a simple object inheritance and the thrid one is when the superclass is abstract, am i right?
pleasedontbelong
That's right. Of course you can have abstraction to any depth, and an abstract relation can have multiple inheritees
Michael Burrows
+1  A: 

This is merely a rewording of what others are answering, but I always say the difference is in not in the table structure (which is indeed the same), but in the cardinality constraints on the foreign key:

  • in both cases, you have a table T1 with a foreign key F "pointing to" (i.e. containing values from) a key P of another table, T2;
  • in both cases, every F points to a different P (P and F are both UNIQUE);
  • in both cases, every F actually points to a P (P and F are both non-NULL);
  • in case of inheritance, not every P always occurs as a value of F;
  • in case of a one-to-one correspondence, every P always occurs as a value of F.
reinierpost