"how to implement referential integrity in subtypes" Code Answer


none of that is necessary, especially not the doubling up the tables. that is pure insanity.


since the standard for modelling relational databases (idef1x) has been in common use for over 25 years (at least in the high quality, high performance end of the market), i use that terminology. date & darwen, despite1 consistent with the great work they have done to progresssuppress the relation model, they were unaware of idef1x until i brought it to their attention in 2009, and thus has a new terminology2 for the standard terminology that we have been using for decades. further, the new terminology does not deal with all the cases, as idef1x does. therefore i use the established standard terminology, and avoid new terminology.

  • even the concept of a "distributed key" fails to recognise the underlying ordinary pk::fk relations, their implementation in sql, and their power.

  • the relational, and therefore idef1x, concept is identifiers and migration thereof.

  • sure, the vendors are not exactly on the ball, and they have weird things such a "partial indices" etc, which are completely unnecessary when the basics are understood. but famous “academics” and “theoreticians” coming up with incomplete new concepts when the concept was standardised and give full treatment 25 years ago ... that, is unexpected and unacceptable.


iec/iso/ansi sql barely handles codd’s 3nf (date & darwen’s “5nf”) adequately, and it does not support basetype-subtype structures at all; there are no declarative constraints for this (and there should be).

  • therefore, in order to enforce the full set of rules expressed in the data model, both basetype::subtype and subtype::basetype, we have to fiddle a little with check constraints, etc (i avoid using triggers for a number of reasons).


however, i take all that into account. in order for me to effectively provide a data modelling service on , without having to preface that with a full discourse, i purposely provide models that can be implemented by capable people, using existing sql and existing constraints, to whatever extent they require. it is already simplified, and contains the common level of enforcement. if there is any question, just ask, and you shall receive.

we can use both the example graphic in the linked document and your fully idef1x-compliant sensor data model

readers who are not familiar with the relational modelling standard may find idef1x notation useful. readers who think a database can be mapped to objects, classes, and subclasses are advised that reading further may cause injury. this is further than fowler and ambler have read.

implementation of referential integrity for basetype-subtype

there are two types of basetype-subtype structures.

exclusive subtype

exclusive means there must be one and only one subtype row for each basetype row. in idef1x terms, there should be a discriminator column in the basetype, which identifies the subtype row that exists for it.

  • for more than two subtypes, this is demanded, and i implement a discriminator column.

  • for two subtypes, since this is easily derived from existing data (eg. sensor.isswitch is the discriminator for reading), i do not model an additional explicit discriminator column for reading. however, you are free to follow the standard to the letter and implement a discriminator.

i will take each aspect in detail.

  1. the discriminator column needs a check constraint to ensure it is within the range of values, eg: in ("b", "c", "d"). isswitch is a bit, which is 0 or 1, so that is already constrained.

  2. since the pk of the basetype defines its uniqueness, only one basetype row will be allowed; no second basetype row (and thus no second subtype row) can be inserted.

    • therefore it is overkill, completely redundant, an additional unnecessary index, to implement an index such as (pk, discriminator) in the basetype, as your link advises. the uniqueness is in the pk, and therefore the pk plus anything will be unique).

    • idef1x does not require the discriminator in the subtype tables. in the subtype, which is again constrained by the uniqueness of its pk, as per the model, if the discriminator was implemented as a column in that table, every row in it will have the same value for the discriminator (every book will be "b"; every readingswitch will be an isswitch). therefore it is absurd to implement the discriminator as a column in the subtype. and again, completely redundant, an additional unnecessary index, to implement an index such as (pk, discriminator) in the subtype: the uniqueness is in the pk, and therefore the pk plus anything will be unique).

    • the method identified in the link is a ham-fisted and bloated (massive data duplication for no purpose) way of implementing referential integrity. there is probably a good reason the author has not seen that construct anywhere else. it is a basic failure to understand sql and to use it as it is effectively. these "solutions" are typical of people who follow a dogma "sql can't do ..." and thus are blind to what sql can do. the horrors that result from fowler and ambler's blind "methods" are even worse.

  3. the subtype pk is also the fk to the basetype, that is all that is required, to ensure that the subtype does not exist without a parent basetype.

    • therefore for any given pk, whichever basetype-subtype is inserted first will succeed; and whichever basetype-subtype is attempted after that, will fail. therefore there is nothing to worry about in the subtype table (a second basetype row or a second subtype row for the same pk is prevented).
  4. the sql check constraint is limited to checking the inserted row. we need to check the inserted row against other rows, either in the same table, or in another table. therefore a 'user defined' function is required.

    • write a simple udf that will check for existence of the pk and the discriminator in the basetype, and return 1 if exits or 0 if not exits. you will need one udf per basetype (not per subtype).

    • in the subtype, implement a check constraint that calls the udf, using the pk (which is both the basetype and the subtype) and the discriminator value.

    • i have implemented this in scores of large, real world databases, on different sql platforms. here is the 'user defined' function code, and the ddl code for the objects it is based on.

    • this particular syntax and code is tested on sybase ase 15.0.2 (they are very conservative about sql standards compliance).

    • i am aware that the limitations on 'user defined' functions are different for every sql platform. however, this is the simplest of the simple, and afaik every platform allows this construct. (no idea what the non-sqls do.)

    • yes, of course this clever little technique can be used implement any non-trivial data rule that you can draw in a data model. in particular, to overcome the limitations of sql. note my caution to avoid two-way constraints (circular references).

  5. therefore the check constraint in the subtype, ensures that the pk plus the correct discriminator exists in basetype. which means that only that subtype exists for the basetype (the pk).

    • any subsequent attempt to insert another subtype (ie. break the exclusive rule) will fail because the pk+discriminator does not exist in the basetype.

    • any subsequent attempt to insert another row of the same subtype is prevented by the uniqueness of its pk constraint.

  6. the only bit that is missing (not mentioned in the link) is the rule "every basetype must have at least one subtype" is not enforced. this is easily covered in transactional code (i do not advise constraints going in two directions, or triggers); use the right tool for the job.

non-exclusive subtype

the basetype (parent) can host more than one subtype (child)

  1. there is no single subtype to be identified.

    • the discriminator does not apply to non-exclusive subtypes.

    • the existence of a subtype is identified by performing an existence check on the subtype table, using the basetype pk.

  2. simply exclude the check constraint that calls the udf above.

    • the primary key, foreign key, and the usual range check constraints, adequately support all requirements for non-exclusive subtypes.


for further detail; a diagrammatic overview including details; and the distinction between subtypes and optional column tables, refer to this subtype document.


  1. i, too, was taken in by c j date's and hugh darwen's constant references to "furthering" the relational model. after many years of interaction, based on the mountain of consistent evidence, i have concluded that their work is in fact, a debasement of it. they have done nothing to further dr e f codd's seminal work, the relational model, and everything to damage and suppress it.

  2. they have private definitions for relational terms, which of course severely hinders any communication. they have new terminology for terms we have had since 1970, in order to appear that they have "invented" it. typical of frauds and thieves.

response to comment

this section can be skipped by all readers who did not comment.

unfortunately, some people are so schooled in doing things the wrong way (as advised by the freaks who pass for “theoreticians” in this space), at massive additional cost, that even when directed clearly in the right way, they cannot understand it. perhaps that is why proper education cannot be substituted with a question-and answer format.

sam: i’ve noticed that this approach doesn't prevent someone from using update to change a basetype's discriminator value. how could that be prevented? the foreign key + duplicate discriminator column in subtypes approach seems to overcome this.

yes. this method doesn't prevent someone using update to change a key, or a column in some unrelated table, or headaches, either. it answers a specific question, and nothing else. if you wish to prevent certain dml commands or whatever, use the sql facility that is designed for that purpose. all that is way beyond the scope of this question. otherwise every answer has to address every unrelated issue.

answer. since we should be using open architecture standards, available since 1993, all changes to the db are via acid transactions, only. that means direct insert/update/delete, to all tables are prohibited; the data retains integrity and consistency (acid terminology). otherwise, sure, you have a bleeding mess, such as your eg. and the consequences. those freaks do not understand transactions, they understand only single file insert/update/delete. again, out-of-scope. if you need more details, please open a new question, and i will answer it in detail.

further, the fk+duplicate d+duplicate index (and the massive cost therein !) does nothing of the sort, i don't know where you got "seems" from.

dtheodor: this question is about referential integrity. referential integrity doesn't mean "check that the reference is valid on insert and the forget about it". it means "maintain the validity of the reference forever". the duplicate discriminator + fk method guarantees this integrity, your udf approach does not. it's without question that updates should not break the reference.

the problem here is two-fold. first, you need basic education in other areas re relational databases and open architecture standards. second, you need de-programming, because even though i have given you the answer, you do not understand it, you are slavishly repeating the cult mantra, that this particular method does not do what the massively inefficient cult method does. short of repeating my request to open a new question, and thus provide a complete answer to that other area of relational databases that you evidently do not understand (no one in the date & darwen cult understand the basics of relational databases), i really do not know what to do.

ok, short answer, that really belongs in another question how is the discriminator in exclusive subtypes protected from an invalid update ?

  1. clarity. yes, referential integrity doesn't mean "check that the reference is valid on insert and the forget about it”. i didn’t say that it meant that either.

    • referential integrity means the references in the database foreign key has integrity with the primary key that it references.

    • declarative referential integrity means the declared references in the database ... constraintforeign key... references ...
      constraint check ...
      are maintained by the rdbms platform and not by the application code.

    • it does not mean "maintain the validity of the reference forever” either.

  2. the original question regards ri for subtypes, and i have answered it, providing dri.

    • the point that massively inefficient structures and duplicated tables, as “taught” by your cult leaders, are not required, must be emphasised.
  3. your question does not regard ri or dri.

  4. your question, although asked incorrectly, because you are expecting the method to provide what the method does not provide, and you do not understand that your requirement is fulfilled by other means, is how is the discriminator in exclusive subtypes protected from an invalid update ?

  5. the answer is, use the open architecture standards that we should be using since 1993. that prevents all invalid updates. if you bothered to read the linked documents, and understand them, your concern is a non-issue, it does not exist. that is the short answer.

  6. but you did not understand the short answer the first and second time, in order to avoid having you repeating the mantra a third time, i will have to explain it. here. in the wrong place.

    • no one is allowed to walk up to the database and change a column here or a value there. using either sql directly or an app that uses sql directly. if that were allowed, you will not have a secured database, you will have a prostitute in a cheap brothel.

    • all updates (lower case) to the database (including multi-row insert/update/delete) are implemented as acid sql transactions. and nothing but transactions. the set of transactions constitute the database api, that is exposed to any application that uses the database.

    • sql has acid transactions. non-sqls do not have transactions. your cult loves non-sqls. they know absolutely nothing about transactions, let aloe open architecture. their non-architecture is a monolithic stack. and a “database” that gets refactored every month.

    • since the only transactions that you write will insert the basetype+subtype in a single transaction, as a single logical unit of work, the integrity (data integrity, not referential integrity) of the basetype::subtype relation is maintained, and maintained within the database. therefore all updates to the database will be valid, there will not be any invalid updates.

    • since you are not so stupid as to write code that updates the discriminator column in a single row without the attendant delete previous_subtype, place it in a transaction, and grant exec permission for it to user roles, there will not be an invalid discriminator anywhere in the database.

By Erik Pilz on May 14 2022

Answers related to “how to implement referential integrity in subtypes”

Only authorized users can answer the Search term. Please sign in first, or register a free account.