【问题标题】:SQL Table OrganizationSQL 表组织
【发布时间】:2012-06-12 21:36:53
【问题描述】:

我有很多需要跟踪的帐户。这些帐户都非常相似,具有以下字段:

account_number, date_opened, current_expenditure etc. 

现在,这些帐户分为三种类型(我们将它们称为 A、B、C 类型) 这些帐户类型中的每一种都需要至少一个其他字段,该字段对其类型是唯一的。例如:

Type A: account_number, date_opened, current_expenditure, owner
Type B: account_number, date_opened, current_expenditure, location
Type C: account_number, date_opened, current_expenditure, currency

我的问题是我是否应该将这些组合成一个大表,其中一列表示帐户类型(将不相关的字段留空):

Table 1: accounts
Table 2: accts_emp_maps

Account Columns: 
account_number, type, date_opened, current_expenditure, owner, location, currency

或者,我应该为每种帐户类型分别创建一个表格吗?请记住,还有其他表将员工映射到这些帐户。如果我将帐户拆分为不同的类型,我也需要拆分地图。即:

Table 1: A_accounts
Table 2: A_accts_emp_maps
Table 3: B_accounts
Table 4: B_accts_emp_maps
Table 5: C_accounts
Table 6: C_accts_emp_maps

【问题讨论】:

    标签: sql database-design


    【解决方案1】:

    我会选择一张表的方法,带有owner, location, currency,额外的列。它会让你的生活更轻松。

    如果太大,可以按类型分区。

    【讨论】:

    • 我就是这么想的。但我必须在“正确的方式”和合乎逻辑/简单的方式之间取得平衡
    【解决方案2】:

    通常,您可能会为此使用超级键/子类型模式来确保只有所有者、位置、货币之一

    • 一个表有一个公共列和一个类型列
    • PK + 类型(A、B 或 C)列上的唯一超级键
    • 三个子表,键为 PK + 类型。您可以在此处添加特定列
    • 检查子类型约束的约束以限制子表中的 A、B 或 C

    现在,在这种情况下,为了简单起见,我会考虑使用冗余列

    例子:

    【讨论】:

      【解决方案3】:

      在您列出的两个选项中,我肯定会选择第一个。这对大多数应用程序来说都很好,并且手动查询表会更简单。 (第二个提议的设计在三组帐户表中重复了大量信息。

      但是,根据您的需要,more normalized 数据库设计可能会更好,如下所示:

      Table: accounts
      ===============
      number, type, date_opened, current_expenditure
      
      Table: account_owners
      =====================
      account_number, owner
      
      Table: account_currencies
      =========================
      account_number, currency
      
      Table: account_locations
      ========================
      account_number, location
      

      【讨论】:

      • 您现在遇到了在其中一个子表中强制执行一行的问题
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多