Search code examples
database-designnullablemany-to-one

When modeling "Many To One" relation, how can I deal with record from "Many" side not belonging to any of the records on "One" side?


I have a a list of products each of which that can belong to a product category.

I can make two tables:

product(id, product_name, category_id);
category(id, category_name);

Some products do not yet have category defined or do not belong to any defined category. How do I deal with that?

I can make category_id nullable, or category_name nullable, or both. Or I can create a "not_yet_assigned" value as part of category_name.

Is there a good solution to this?


Solution

  • Making the Product.Category_id nullable will force to use outer joins to see all your products with their Category.Category_Name, so I would avoid that, and I would create a Category called Uncategorized or similar.
    Or perhaps one Uncategorized (which would be the default) and one Other for those Products that do not belong to any other category.