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 11 '23
So that I can get rid of the surrogate key. Or maybe there is a possible decomposition that keeps the constraint?
I can do:
With a unique constraint on (category_id, year, form), but then CategoryToTaxCategory wouldn't be normalized because of the functional dependencies {tax_category_id} -> {year} and {tax_category_id} -> {form}