数据库设计:借贷资产融资

时间:2016-09-30 10:26:22

标签: sql database-design

在我的数据库中,我有3个关于不同类型资产的表: house 和地面定位建筑物股票

另一张表是关于借用

现在我必须链接资产和借用。规则是: - 一个借款只能与一个资产挂钩。 - 一项资产可以选择性地与一项或多项借款挂钩。

我从这个数据库设计开始:

CREATE TABLE CUSTOMER
(
  CUSTOMER_ID INT NOT NULL,
  [...] 

  CONSTRAINT PK_CUS_ID PRIMARY KEY (CUS_ID)
)

CREATE TABLE ASSET1
(
  ASSET1_ID   INT NOT NULL,
  CUSTOMER_ID INT NOT NULL,
  [...]

  CONSTRAINT PK_ASSET1_ID PRIMARY KEY (ASSET1_ID),  
  CONSTRAINT FK_ASSET1_CUSTOMER FOREIGN KEY (CUSTOMER_ID) REFERENCES CUSTOMER (CUSTOMER_ID) ON DELETE NO ACTION ON UPDATE NO ACTION
)

CREATE TABLE ASSET2
(
  ASSET2_ID   INT NOT NULL,
  CUSTOMER_ID INT NOT NULL,
  [...]

  CONSTRAINT PK_ASSET2_ID PRIMARY KEY (ASSET2_ID),  
  CONSTRAINT FK_ASSET2_CUSTOMER FOREIGN KEY (CUSTOMER_ID) REFERENCES CUSTOMER (CUSTOMER_ID) ON DELETE NO ACTION ON UPDATE NO ACTION
)

CREATE TABLE ASSET3
(
  ASSET3_ID   INT NOT NULL,
  CUSTOMER_ID INT NOT NULL,
  [...]

  CONSTRAINT PK_ASSET3_ID PRIMARY KEY (ASSET3_ID),  
  CONSTRAINT FK_ASSET3_CUSTOMER FOREIGN KEY (CUSTOMER_ID) REFERENCES CUSTOMER (CUSTOMER_ID) ON DELETE NO ACTION ON UPDATE NO ACTION
)

CREATE TABLE BORROWING
(
  BORROWING_ID INT NOT NULL,
  CUSTOMER_ID  INT NOT NULL,
  ASSET1_ID    INT     NULL,
  ASSET2_ID    INT     NULL,
  ASSET3_ID    INT     NULL,
  [...]

  CONSTRAINT PK_BORROWING_ID PRIMARY KEY (BORROWING_ID),

  CONSTRAINT FK_BORROWING_CUSTOMER FOREIGN KEY (CUSTOMER_ID) REFERENCES CUSTOMER (CUSTOMER_ID) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT FK_BORROWING_ASSET1   FOREIGN KEY (ASSET1_ID  ) REFERENCES ASSET1   (ASSET1_ID  ) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT FK_BORROWING_ASSET2   FOREIGN KEY (ASSET2_ID  ) REFERENCES ASSET2   (ASSET2_ID  ) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT FK_BORROWING_ASSET3   FOREIGN KEY (ASSET3_ID  ) REFERENCES ASSET3   (ASSET3_ID  ) ON DELETE NO ACTION ON UPDATE NO ACTION,

  CONSTRAINT CHK_BORROWING CHECK 
  (
      ((ASSET1_ID IS     NULL) AND (ASSET2_ID IS     NULL) AND (ASSET3_ID IS     NULL))
   OR ((ASSET1_ID IS NOT NULL) AND (ASSET2_ID IS     NULL) AND (ASSET3_ID IS     NULL))
   OR ((ASSET1_ID IS     NULL) AND (ASSET2_ID IS NOT NULL) AND (ASSET3_ID IS     NULL))
   OR ((ASSET1_ID IS     NULL) AND (ASSET2_ID IS     NULL) AND (ASSET3_ID IS NOT NULL))
  )
)

它的工作和参照完整性似乎没问题。

但是我已经知道将来我必须在我的数据库中添加更多资源,CHK_BORROWING将变成怪物。

我的问题:是否有更好,更清洁,更简单的解决方案?

谢谢

1 个答案:

答案 0 :(得分:0)

根据Walter Mitty的建议,解决方案是Class Table Inheritance。

  

如果您使用了Class Table继承,则可以创建一个表,让我们来   称之为ASSETS_GENERIC。每种资产,无论何种类型,都会有   在此表中输入。现在你可以在两者之间建立一个有限的联系   以通常的方式借用和Assets_generic(参考文献)。现在   你在ASSETS_GENERIC和每个ASSETx之间建立联系   表。