【问题标题】:database relations issue数据库关系问题
【发布时间】:2012-06-23 20:49:13
【问题描述】:

我有一个关于我的数据库设计及其实现的问题,希望有人能帮助我吗?

我有一个产品 -> 零售商场景,其中适用以下规则:

产品 - 许多零售商 产品 - 1 个品牌

零售商 - 许多产品

价格可能因零售商而异

所以我有 4 张桌子

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,

这很好用,因为您可以将产品与零售商关联起来,而并非所有零售商都提供所有产品等。每个产品都有一个固定的品牌。

我的问题来自品牌:

零售商可以提供 1 个或多个品牌,但不一定提供所有品牌?我在执行此操作时遇到问题?

所以我创建了一个 Retailer_Brand 表

retailer_brand - (retailer_Id, Brand_ID)

即使您已指定零售商到品牌的链接,我仍然可以在零售商产品表中输入与零售商无关的品牌的产品。我是否遗漏了诸如检查约束之类的内容,还是我的架构错误?

谢谢

罗伯

*编辑更多细节*

我仍然不确定它是否能满足我的需求。

也许如果我添加以下示例它会澄清。

我有一个我们可以提供的产品设置列表

例如 名称 描述 品牌 电视 32 英寸索尼 电视 64 英寸索尼 电视 20 英寸索尼 电视 64 英寸三星 电视 32 英寸三星 电视32寸松下

零售商 Uberhardware - 可以销售所有品牌的电视 SonyRetailer - 只允许销售索尼产品(所有产品) PanasonicRetailer - 仅限 Panasonic 产品

然后我需要设置一个新零售商: 凤凰零售 - 不允许销售索尼产品

我希望能够轻松地限制/启用每个零售商的不同品牌?

编辑 2

我已经实施了建议的备用键设计,但我仍然能够输入不正确的日期,请参阅下面的数据设置和预期结果,但是当我预计有些失败时,零售商产品表中的所有输入都会成功?

产品
ProductID BrandID
1 1
2 2

品牌
品牌ID
1
2

Reatiler
1
2

RetailerBrand
RetailerID BrandID
1 1
2 1
2 2
3 1

RetailerProduct
RetaileID Brand ProductID 预期 1 1 1 好的 1 2 2 失败 2 1 1 正常 2 2 2 好的 3 2 2 失败

【问题讨论】:

  • 按原样在 sql server 2008 上工作;
  • 现在一切正常,只需要找出我的数据建模器在创建链接时出错的地方。

标签: database database-design


【解决方案1】:
  • 备用键 (AK) -- 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

【讨论】:

  • 谢谢,看起来它可以完成这项工作。您是否有更多关于您设置的 AK 的详细信息,这只是一个约束还是一个关系?
  • @user1340969; AK 只是一个unique constraint(带索引)——DB 自动为唯一约束创建索引是很常见的。
  • 这和外键有区别吗?还是它仍然是外键但也是唯一约束?
  • @user1340969 它是Product 上的唯一约束参见un_product,它是RetailerProduct 上的外键参见fk2_retprd
  • 对不起,另一个问题,如果 product_id 每个产品都是唯一的,那么是否需要唯一约束?
【解决方案2】:

让我看看我是否做对了。

产品

product_id
name
description
etc

品牌

brand_id
name
etc

零售商

retailer_id
name
etc

关系表 brand_prod_ret

retailer_id
product_id
brand_id
price
etc

例如,Uber 硬件零售商销售电视:LG、索尼、三星等。

Uberhardware 进入 零售商

电视进入产品

您的 product_idbrand_prod_ret 表中的 retailer_idbrand_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

当然还有每个价格。

现在您可以准确地知道零售商目前正在销售的品牌

【讨论】:

  • 感谢您的快速回复,这将使我能够跟踪已售出的品牌,但我想要了解的更多的是哪些产品是可用的。例如我的产品 1. 电视品牌是索尼,2 - 电视是三星,电视 3 - 松下等。我需要能够说 uberhardware 可以供应索尼,松下但不是索尼,sonysales 只能销售索尼品牌的产品?
  • 啊,我现在明白了。让我重新制定我的答案
  • @user1340969 完成,您现在可以获得所有品牌的 uberhardware 销售
  • 谢谢,我可以看到您在那里做了什么,这对于查看零售商销售哪些品牌是有意义的,但我不确定它是否能让我轻松添加新零售商并将品牌与该零售商关联?我在最初的 Q 中添加了更多的 cmets?
【解决方案3】:

是否有任何理由需要品牌表?

我们有很多关系 产品——品牌 零售商-retailer_product 品牌 - 零售商品牌

看来您可以将品牌信息保留在retailer_product 表中。然后每一个输入的新品牌都可以进行pk链接。或者只有一个零售商到品牌查找表来强制零售商协会的约束,b/f 一个新的retailer_product 可以输入

【讨论】:

    【解决方案4】:

    IMO 在您当前的结构中,retailer_brand 表并不是绝对必要的,因为您始终可以通过在 retailer_product => 产品 => 品牌关系中导航来确定零售商全部或部分库存的品牌。

    我能否要求对您的业务领域进行一些说明,这可能会影响您的数据建模? (我在零售商有一些经验),例如

    品牌通常是特定于零售商的(即供应商生产的一系列产品完全是为一个零售商打上品牌的)。您可以考虑将供应商表添加到您的模型中。

    零售商连锁店内的商店通常不会储存所有产品。您可能需要在模型中考虑这一点。

    我不确定您是面向消费者还是面向供应商的业务,但您可能需要对成本价格和销售价格进行建模。

    价格应包括起始日期/终止日期

    等等

    【讨论】:

    • 嗨,感谢您的回复。我是从经销商的角度来看的,我们选择了不同品牌的产品,并且我们获得了销售产品的佣金。我确实在产品下记录了佣金,但不想混淆这个问题。我们处理的每个零售商都提供不同的品牌,例如零售商 a 将库存品牌 1、2、3 零售商 b 将仅库存品牌 1。我希望能够在数据库中执行这些规则,以便我可以轻松地将品牌添加到零售商。那么如果零售商 b 开始进货品牌 2 以及这些产品呢?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-07
    • 1970-01-01
    • 1970-01-01
    • 2011-11-16
    • 2010-09-14
    相关资源
    最近更新 更多