我有一个关于我的数据库设计和它的实现的问题,我希望有人能帮助我?
我有一个产品->零售商场景,根据以下规则适用:
产品-许多零售商产品-1品牌
零售商-许多产品
每个零售商的价格都可能发生变化。
所以我有四张桌子
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,这是很好的工作,因为你可以将产品与批发商联系在一起,而不是所有零售商都提供所有的产品等等,每个产品都有一个固定的品牌。
我的问题来自于品牌:
零售商可以提供一个或更多品牌,但不一定是所有品牌?我要解决这个问题吗?
所以我创建了一个Retailer_Brand表
retailer_brand - (retailer_Id, Brand_ID)即使您指定了零售商的品牌链接,我仍然可以在零售商产品表中输入一个与零售商无关的品牌的产品。我是缺少了检查约束之类的东西,还是我的模式错了?
谢谢
抢夺
*编辑更多细节*
我仍然不确定它是否给了我所需要的。
也许,如果我添加下面的例子,它将澄清。
我有一份我们可以提供的产品设置清单。
例如,名为Desc牌电视32寸索尼电视64寸索尼电视20寸索尼电视64寸三星电视32寸三星电视32寸松下
零售商Uberhardware可以销售所有品牌的电视SonyRetailer -只允许销售索尼产品(所有产品) PanasonicRetailer -松下产品仅限
然后又来了一家我需要设立的新零售商:菲尼克斯零售公司不允许销售索尼产品。
我希望能够轻松地限制/启用每个零售商的不同品牌?
编辑2
我已经实现了建议的备用密钥设计,但我仍然能够输入不正确的日期,请参见下面的数据设置和预期的结果,然而,当我期望一些失败时,所有进入零售商产品表的功能都会成功吗?
产品
ProductID BrandID
1 1
2 2
品牌
BrandID
1
2
Reatiler
1
2
RetailerBrand
RetailerID BrandID
1 1
2 1
2 2
3 1
RetailerProduct
RetaileID品牌ProductID预期1 1 1 OK 1 2失败2 1 1 OK 2 2 OK 3 2 2失败
发布于 2012-06-21 16:35:03
Product上的唯一约束(带索引)允许(ProductID, BrandID)作为FK.从RetailerProduct引用。

create table Product (
ProductID integer not null
, BrandID integer not null
);
alter table Product add constraint pk_product primary key (ProductID);
alter table Product add constraint un_product unique (ProductID, BrandID);
create table Brand (
BrandID integer not null
);
alter table Brand add constraint pk_brand primary key (BrandID);
create table Retailer (
RetailerID integer not null
);
alter table Retailer add constraint pk_retailer primary key (RetailerID);
create table RetailerBrand (
RetailerID integer not null
, BrandID integer not null
);
alter table RetailerBrand add constraint pk_retbra primary key (RetailerID, BrandID);
create table RetailerProduct (
RetailerID integer not null
, ProductID integer not null
, BrandID integer not null
);
alter table RetailerProduct add constraint pk_retprd primary key (RetailerID, ProductID, BrandID);
alter table RetailerProduct add constraint fk1_retprd
foreign key (ProductID, BrandID) references Product (ProductID, BrandID);
alter table RetailerProduct add constraint fk2_retprd
foreign key (RetailerID, BrandID) references RetailerBrand (RetailerID, BrandID);编辑
插入一些数据
insert into Brand (BrandID) values (1) , (2);
insert into Product (ProductID, BrandID) values (1,1), (2,2);
insert into Retailer (RetailerID) values (1) , (2);
insert into RetailerBrand (RetailerID, BrandID) values (1,1), (2,1), (2,2), (3,1);测试
insert into RetailerProduct (RetailerID, BrandID, ProductID) values (1,1,1); -- OK
insert into RetailerProduct (RetailerID, BrandID, ProductID) values (1,2,2); -- FAIL
insert into RetailerProduct (RetailerID, BrandID, ProductID) values (2,1,1); -- OK
insert into RetailerProduct (RetailerID, BrandID, ProductID) values (2,2,2); -- OK
insert into RetailerProduct (RetailerID, BrandID, ProductID) values (3,2,2); -- FAIL发布于 2012-06-21 15:27:08
让我看看我是否做对了。
产品
product_id
name
description
etc品牌
brand_id
name
etc零售商
retailer_id
name
etc关系表brand_prod_ret
retailer_id
product_id
brand_id
price
etc例如,Uberhardware零售商销售电视机: LG、索尼、三星等。
Uberhardware进入零售商表
TV进入Product表
brand_prod_ret表中的product_id与retailer_id和brand_id匹配。
进入品牌,你有LG,索尼,三星等。
进入brand_prod_ret你有
TVs' ID - Sony's id -Uberhardware id
TVs' ID - Samsung's id - Uberhardware id
TVs' ID - LG's id - Uberhardware id当然,每一个价格。

现在你可以确切地知道这家零售商目前销售的是哪个品牌。
发布于 2012-06-21 15:38:55
是否有任何理由,品牌表是需要这样的能力?
我们与产品有很多关系-品牌零售商- retailer_product品牌-零售商品牌
似乎您可以将品牌信息保存在retailer_product表中。然后进入的每一个新品牌都可以pk链接。或者只有一个零售商要品牌查找表以强制执行零售商协会b/f的约束,可以输入一个新的retailer_product。
https://stackoverflow.com/questions/11141205
复制相似问题