2011-06-24 62 views
0

我試圖設計一個寄售商店的POS系統的模式。我有一個主項目的表和多個實體,它們將其作爲外鍵引用幷包含不同的屬性。項目表包含所有項目通用的所有信息,無論其類型如何。引用任何給定項目的實體爲該給定項目類型提供特定屬性。例如,「拆分」項目需要詢價,而商店項目需要成本價格。SQL模式實體設計問題

雖然這種設計在技術上可行,在項目表強制只有一個項目,我希望能夠保持任何給定的項目僅由這些實體之一引用。我擔心的是,我不希望「拆分」項目也被我認爲是「商店」項目。有什麼辦法可以通過模式設計來實施嗎?

的ERD是在這裏:http://randywestergren.com/pos-erd.pdf

有問題的表是phppos_items,phppos_items_entity_split,phppos_items_entity_store,提前等

謝謝!

+0

您正在使用什麼數據庫? Is是對象關係,關係型,nosql? – Nate

回答

1

好像你確定項目是否是一個「分裂」或由相關行存在「存儲」型。相反,如何將一個類型字段添加到items表中?它不會阻止其他相關行實際存在,但它會讓您明確知道這是一個錯誤。

1

創建副表:

 
typed_items (poorly named) 
{ 
    PK: item_key - this points to the item 
    PK: int item_type_id - this type code specified which 'type' of item, ie: store, split 

// optional, depending on your key design: 
//  1...n FK: 
//  split_item_id, 
//  store_item_id, 
//  etc.., if you reference the items PK in the sub-item-type-detail tables, this 
//  isnt nessicary. if you are using a synthetic key on the sub-item-type-detail 
//  tables and no key relationship exists you can use this table as a linking table, 
//  otherwise its not nessicary 
} 

由於類型項目鍵和類型descriminator ID是主鍵的一部分,你知道你的模式不會允許一個項目是屬於2個不同的子項類型 - 細節表。

如果你想(偏執型)的完整性,現在可以在確保與常量類型descriminator值與用於該類型在這個表中存在配對項目的關鍵這些類型詳細信息表添加約束。

+0

對不起,遲到的迴應。好點,你是對的,我需要能夠告訴哪種類型的物品是哪種類型。 你覺得一個實體屬性值的設計?將會有一個屬性表和一個連接表,它爲任何給定的項目及其值定義一個屬性。 – rwestergren