r/DatabaseHelp • u/NickBourbaky • Dec 07 '23
Normalizing a relation without losing constraints
Consider a relation with these attributes: year, form, category_id, tax_category_id, line_no, name. The relation has these functional dependencies:
- {tax_category_id} -> {year}
- {tax_category_id} -> {form}
- {tax_category_id} -> {line_no}
- {tax_category_id} -> {name}
- {year, form, line_no} -> {name}
- {year, form, category_id} -> {line_no}
- {year, form, category_id} -> {tax_category_id}
We can normalize this relation like this:
TaxCategory: tax_category_id, year, form, line_no, name
CategoryToTaxCategory: category_id, tax_category_id
But we would lose the constraint that, there is only one tax category associated with a category for a given year, and form ({year, form, category_id} -> {tax_category_id})
One solution would be removing the surrogate key (tax_category_id) and use this decomposition:
TaxCategory: year, form, line_no, name
CategoryToTaxCategory: year, form, category_id, line_no
But Django doesn't allow a primary key with multiple attributes.
Are those the only solutions?
View all comments
Show parent comment
1
u/NickBourbaky Dec 08 '23
It adds a constraint but doesn't define it as the primary key. There would still be the automatic primary key, which is used when using models.ForeignKey.
I would prefer something like:
But no way to do that with Django, although 2) can be done through a custom Constraint.