Hello i am facing the following…
Two DB tables (mysql myisam tables – no foreign key constraints) ‘MASTER’ and ‘CHILD’. Rows in ‘CHILD’ db table reference a single row (1:M) in the ‘MASTER’ table using the parent_id *column*.
The problem is that the parent_id column has 2 “types” of values:
- A NON-ZERO value referencing a row in the ‘MASTER’ table
- A ZERO value meaning that the row is not referencing the ‘MASTER’ table. In this case rows with ZERO values in the parent_id column ‘actually mean’ something else => represent different type of data (stored in the same table? i.e storing apples and oranges in the same table when the two might be best to be stored in different table)
I am confused. Is this simply a bad database design? Is it common practice to differentiate ‘what rows in a DB table represent’ using different values in a column which is used to reference ‘parent rows’ in another table.
If you are not confused and can help, please do 🙂
When you are discussing what rows in a table “actually mean”, you are crossing the line from logical data modeling into conceptual data modeling, the semantics of the data. In general, storing rows of two different types with regard to actual meaning in one table is a bad idea.
It’s a bad idea from the point of view of speed, because most of your queries are going to involve extra disk io to weed out rows that don’t pertain to the current query.
It’s a bad idea from the point of view of producing bug free code, because it you forget to weed out rows that don’t pertain, you’ll get wrong results.
It’s a bad idea from the point of view of flexibility, because data that is inappropriately bound together is harder to alter when the requirments change.
It’s bad design.
Having said that, there are optional FK fields in well designed databases. In such cases, the rows that opt out of the relationship contain NULL, not zero in the cell. However, even in such cases, the semantics of the remaining columns isn’t affected by whether the relationship is present or absent.