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

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -