I have a question regarding my database design and its implementation that i am hoping someone can help me with?
I have a product -> retailer scenario where by the following rules apply:
product – many retailers
product – 1 brand
retailer – many products
Price can change per retailers
so i have 4 tables
product - (Product_ID), product name, brand_ID(FK), details
brand - (Brand_ID), brand name
retailer - (Retailer_ID), retailer_Name, Retailer_Telephone
Retailer_Product - (Product_ID, Retailer_ID), cost,
This works fine as you can associate products with reatilers and not all retailers offer all products etc. each product has a set brand.
My issues comes based on the brand:
a retailer can offer 1 or more brands but not necessarily all brands? i am getting an issue implementing this?
So i have created a Retailer_Brand table
retailer_brand - (retailer_Id, Brand_ID)
Even if you have specified the retailer to brand link i can still enter a product into the retailer product table that is of a brand not associated with the retailer. Am i missing something like a check constraint or is my schema wrong?
Thanks
Rob
*EDIT further details *
I am still not sure if it gives me what I require.
Perhaps If I add the following example it will clarify.
I have a list of products setup that we can offer
e.g.
Name Desc Brand
TV 32 Inch Sony
TV 64 Inch Sony
TV 20 Inch Sony
TV 64 Inch Samsung
TV 32 Inch Samsung
TV 32 Inch Panasonic
Retailers
Uberhardware – Can sell all brands of tv
SonyRetailer – Is only allowed to sell Sony products (all products)
PanasonicRetailer – Panasonic Products only
Then along comes a new retailer who I need to setup:
Phoenix Retail – Is not allowed to sell Sony products
I wish to be able to easily restrict/enable the different brands per retailer?
EDIT 2
I have implemented the Alternate key design suggested but i am still able to enter incorrect date see my data setup below and the expected results however all enrtsy into the retailer product table succeed when i would expect some to fail?
Product
ProductID BrandID
1 1
2 2
Brand
BrandID
1
2
Reatiler
1
2
RetailerBrand
RetailerID BrandID
1 1
2 1
2 2
3 1
RetailerProduct
RetaileID Brand ProductID Expected
1 1 1 OK
1 2 2 FAIL
2 1 1 OK
2 2 2 OK
3 2 2 FAIL
Productallows(ProductID, BrandID)to be referenced fromRetailerProductas a FK.EDIT
Insert some data
Test