2012-06-12 45 views
0

我有嚴格的帳戶需要跟蹤。這些賬戶都是有領域,如非常相似:SQL表組織

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 

或者,我應該有每個賬戶類型的單獨表格?請記住,還有其他表將員工映射到這些帳戶。如果我將帳戶分成不同的類型,我還需要分割地圖。 IE:

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 

回答

1

我會去爲一個表的做法,與owner, location, currency,額外的列。它會讓你的生活更輕鬆。

如果太大,可以按類型對其進行分區。

+0

這就是我的想法。但我必須在「正確的方式」之間取得平衡,並且以邏輯/簡單的方式執行此操作 – Peaches491

-1

在你列出的兩個選項中,我肯定會選擇第一個。對於大多數應用程序來說都會很好,並且手動查詢表格會更簡單。 (第二個建議的設計複製了三組帳戶表中的大量信息)。

然而,根據您的需要,一個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 
+0

您現在有一個問題,即在其中一個子表中強制執行一行 – gbn

1

經典,你也許還可以利用這個super鍵/亞型模式,以確保只有一個所有者,位置,與貨幣公共列

  • 一個表和一個類型列
  • 對PK +型獨特超級鍵(A,B或C)柱
  • 三個PK +鍵的子表。這是你的子表添加特定的列在子類型約束
  • 檢查約束來限制A,B或C現在

,在這種情況下我會考慮爲簡化冗餘列雖然

實例: