2014-02-24 49 views
0

我們有一個數據庫,每個數據庫都有一堆類別。層次結構是這樣的:我怎樣才能構建這個數據庫?

Department 
-- Necklaces 
    -- N_Diamond 
     -- N_D_Silver 
    -- N_Silver 
-- Rings 
    -- R_Diamond 
    -- R_Gemstone 

中的每一個頂級類別(項鍊,戒指)有一個父類(系)和那些頂級類別中的每個類別只能有一個父類(即N_Diamond是N_D_Silver的父母)。

在每個類別中,都會有一個產品列表。每個產品可以屬於多個類別(一對多?)。

我該如何去組織這個數據庫?每行是產品+產品類別的產品表格?這意味着每個產品可以有多行。雖然這看起來並不很規範。

+0

我不知道子類(項鍊,戒指,N_Diamond等)是否具有屬性/字段本身,因爲這可能是「泛化/專業化」或「超類/子類」的情況,否則您只需創建參考作爲下面已經給出的答案表示。 – Edper

回答

0

那麼,使用樹形結構 表將有rows id,parent_category,child_category。 因此,一個典型的行會

1, Departments, Necklace 
2, Departments, Rings 
3, Neckless, N_Diamonds 
3, Neckless, N_Silver 
... 
10, R_Gemstone, null 

您可以通過索引正常化這一點。

現在創建一個表,其中參考上述 Product_Tablerows idcategory_idproduct_name

+3

我不確定我是否跟着,這句話:「你可以通過索引標準化這個。」你能詳細說明一下嗎? –

+0

對不起,我的意思是說,而不是重複文本N鑽石或項鍊,您可以使用屬性id,類型,描述來製作另一個名爲ProductType的表格,然後僅使用產品類型ID的外鍵進行參考 – Tremmillicious

0

可以簡單地樹產品:

Product (ProductID, Name, details) 
Department (DepartmentID, DepartmentName, ParentDepartmentID) 
ProductDepartment (DepartmentID, ProductID) 

你可以部門級別的層次結構分成不同的表,理想取決於使用。

0

我什麼模型,可以沿着這些路線:

create table dbo.Type 
(
    id int not null identity(1,1) primary key , 
) 

create table dbo.TypeAttribute 
(
    typeId int not null foreign key references dbo.Type(id) , 
    id  int not null identity(1,1) unique , 

    primary key (typeId , id) , 

) 

create table dbo.Product 
(
    id  int not null identity(1,1) unique , 
    typeId int not null foreign key references dbo.Type(id) , 

    primary key (id , typeId) , 
) 

create table dbo.ProductAttribute 
(
    productId int not null , 
    typeId  int not null , 
    attributeId int not null , 

    primary key (productId , typeId , attributeId) , 
    foreign key (productId , typeId  ) references dbo.Product  (id  , typeId) , 
    foreign key (typeId , attributeId) references dbo.TypeAttribute (typeId , id ) , 

) 
  • 我們有一組不同的Types,產品的父類。
  • 每個這樣的Type具有TypeAttributes的某個數字,該特定類型的項目的描述性屬性。
  • 我們有一個Products,其中每個被分配到一個,並且只有一個Type
  • 我們有一個關聯實體ProductAttribute,它列舉了每個獨特產品擁有的屬性,這些屬性來自該產品的可用屬性Type