如何在数据库中建模这么多关系?

时间:2015-02-14 20:37:02

标签: sql-server

我们有两个表ManagementPlan,它告诉我们必须使用哪种类型的产品模型。基于这个型号,在治疗期间将这种类型的特定产品分配给患者。我们如何模拟多种关系在Product表和ManagementPlan表之间

MangamentPlan(
  PlanID(PK),
  DiagnosisID(FK),
  PhysicianID(FK),
  PMCModelID,
  Objective,
  Description,
  DateTime
)

Product(
  PMCProductID(PK),
  ManuProductID(FK),
  ManufacturerID(FK),
  PMCModelID,
  Manufacturer model,
  Features description,
  PurchaseDate,
  Storage Location
)

2 个答案:

答案 0 :(得分:1)

添加联结表:

ManagementPlanProduct(PlanID(PK, FK(ManagementPlan)), PMCProductID(PK, FK(Product)))

答案 1 :(得分:0)

您需要一个联结表

ManagementPlanProduct (
    PlanID (PK, FK)
    PMCProductID (PK, FK)
)
CREATE TABLE ManagementPlanProduct (
    PlanID int NOT NULL,
    PMCProductID int NOT NULL,
    CONSTRAINT PK_mpp PRIMARY KEY CLUSTERED (PlanID, PMCProductID)
);

ALTER TABLE ManagementPlanProduct
ADD CONSTRAINT FK_mpp_mp
FOREIGN KEY (PlanID) REFERENCES ManagementPlan (PlanID)
ON DELETE CASCADE;

ALTER TABLE ManagementPlanProduct
ADD CONSTRAINT FK_mpp_p
FOREIGN KEY (PMCProductID) REFERENCES Product (PMCProductID)
ON DELETE CASCADE;

您还可以在联结表中添加其他列,例如数量,添加日期,排序顺序等。