views:

42

answers:

2

This is more of a curiosity at the moment, but let's picture an environment where I bill on a staunch nickle&dime basis. I have many operations that my system does and they're all billable. All these operations are recorded across various tables (these tables need to be separate because they record very different kinds of information). I also want to micro manage my accounts receivables. (Forgive me if you find inconsistencies here, as this example is not a real situation)

Is there a somewhat standard way of substituting a foreign key with something that can verify that the identifier in column X on my billing table is an existing identifier within one of many operations record tables?

One idea is that when journalizing account activity, I could reference the operation's identifier as well as the operation (specifically, the table that it's in) and use a CHECK constraint. This is probably the best way to go so that my journal is not ambiguous.

Are there other ways to solve this problem, de-facto or proprietary?

Do non-relational databases solve this problem?

EDIT:

To rephrase my initial question,

Is there a somewhat standard way of substituting a foreign key with something that can verify that the identifier in column X on my billing table is an existing identifier within one of many (but not necessarily all) operations record tables?

+2  A: 

No, there's no way to achieve this with a single foreign key column.

You can do basically one of two things:

  • in your table which potentially references any of the other x tables, have x foreign key reference fields (ideally: ID's of type INT), only one of which will ever be non-NULL at any given time. Each FK reference key references exactly one of your other data tables

or:

  • have one "child" table per master table with a proper and enforced reference, and pull together the data from those n child tables into a view (instead of a table) for your reporting / billing.

Or just totally forget about referential integrity - which I would definitely not recommend!

marc_s
I agree with what you said, except I'm seeking an alternative to foreign key for this check of integrity. Although OMG Ponies informed me that it can't be done with a CHECK so now I'm now I'm totally at a loss. I really don't want to have to add dozens of nullable FK columns to satisfy all of the possible operations.
andyortlieb
+1  A: 

An alternative is to enforce complex referential integrity rules via a trigger. However,and not knowing exactly what your design is, usually when these types of questions are asked it is to work around a bad design. Look at the design first and see if you can change it to make this something that can be handled through FKs, they are much more managable than doing this sort of thing through triggers.

If you do go the trigger route, don't forget to enforce updates as well as inserts and make sure your trigger will work properly with a set-based multi-row insert and update.

A design alternative is to havea amaster table that is parent to all your tables with the differnt details and use the FK against that.

HLGEM