sql - CONSTRAINT by checking value in other table -
i have items, categories , items_category table this:
table items +----+-------+ | id | title | +----+-------+ table categories +----+------+------+ | id | name | main | +----+------+------+ table items_categories +---------+-------------+ | item_id | category_id | +---------+-------------+
i need make constraint item can have 1 main category, otherwise, amount of main = false categories. initial thought set unique(item_id, category_id)
limiting item 1 category.
then thought having column on items_categories table called main exact duplicate of main column referenced category_id column, this:
table items_categories +---------+-------------+------+ | item_id | category_id | main | +---------+-------------+------+ unique(item_id, category_id, main)
but not 100% normalized data , want avoid if possible.
what need partial index:
create unique index main_category_unique on items_categories (item_id) main = true;
Comments
Post a Comment