Search code examples
sqldatabase-designsubtypesupertype

super-type subtype discriminator in Primary key and design simplicty?


Referencing this great answer by PerformanceDBA:

The referenced answer approaches the problem of supertype subtype by :

  1. using a Discriminator in the base-type ( which could be constrained by using FK and lookup table for TYPE)

  2. creating a simple FUNCTION to check exclusivity which gets called using CHECK constraint in the subtype.

My question : if the discriminator is part of the primary key of the base-type and a User Defined Function is USED to check for exclusivity , would a design like this break any relational design or would it make the design simpler ( given that the PK is not multi valued of course) ?

Here is an example for illustration:

ProductID Description
120001 This product is Car ( starts with 120)
150001 This Product is Boat (starts with 150)
160001 This Product is Jet (starts with 160)

can call a function that checks for the ID of product and insert this into the desired table ( Jet table , Boat Table or Car table ) based on the ID?

if that is the case , can I get rid of the Discriminator column and the look up table too ?

is there any shortcoming of this approach ?

UPDATE to illustrate the two design approaches :

Illustration of Question


Solution

  • is there any shortcoming of this approach ?

    Yes.

    You have broken 1NF:

    First Normal Form
    Each domain (column) must be Atomic, that is, indivisible
    with regard to the SQL Platform (Datatypes).

    Your ProductID is a conglomeration thing that is made up from two atomic things:
        ProductType CHAR(1) -- {C, B, J, ...}
    and some number within that, say
        ProductNo INT.

    At the point of your table (which is not described) in the data hierarchy, the PK is an ordinary Relational composite key:
        ( ProductType, ProductNo ).
    not:
        ProductID.

    Now you don't need a Subtype cluster in that table.  

    But I suspect you need exclusivity pivoting on some other concept: if so, you need to post a model of all the tables related to this issue.