How to I represent the following datamodel in sql tables.
I have 3 entities, company, productcategory and product.
Business model is that Company can have product category1-N and each category can have many products.
The trick is that products are shared across companies under different categories. Product categories are not shared. Each company has its own categories.
for example,
product1 belongs to category1 under company1
product1 belongs to category2 under company2
I’m thinking of having the following tables. Only relevant Id fields are shown below.
Company
CompanyId
ProductCategory
ProductCategoryId
CompanyId
ParentCategoryId (To support levels)
Product
ProductId
ProductCategoryXProduct
ProductCategoryId
ProductId
This way I can query for all product categories for a product and filter by company to get the specific category structure for its products. This may be different for another company even if the product is the same.
Will this cover it? is there a better approach?
Looks like a fine 3NF design that fits what you have described.
Note that as your data set will grow, this design will start slowing down (mostly due to the required joins), so when the time comes you may want to denormalize some of these tables for faster reads.