I have a table my_table with these fields: id_a, id_b.
So this table basically can reference either an row from table_a with id_a, or an row from table_b with id_b. If I reference a row from table_a, id_b is NULL. If I reference a row from table_b, id_a is NULL.
Currently I feel this is my only/best option I have, so in my table (which has a lot more other fields, btw) I will live with the fact that always one field is NULL.
If you care what this is for: If id_a is specified, I’m linking to a “data type” record set in my meta database, that specifies a particular data type. like varchar(40), for example. But if id_b is specified, I’m linking to a relationship definition recordset that specifies details about an relationship (wheather it’s 1:1, 1:n, linking what, with which constraints, etc.). The fields are called a little bit better, of course 😉 …just try to simplify it to the problem.
Edit: If it matters: MySQL, latest version. But don’t want to constrain my design to MySQL specific code, as much as possible.
Are there better solutions?
AandBare disjoint subtypes in your model.This can be implemented like this:
Table
refsconstains all instances of bothAandB. It serves no other purpose except policing referential integrity, it won’t even participate in the joins.Note that
MySQLacceptsCHECKconstraints but does not enforce them. You will need to watch your types.You also should not delete the records from
table_aandtable_bdirectly: instead, delete the records fromrefswhich will triggerON DELETE CASCADE.