2017-08-25 223 views
-1

我想達到以下情形:數據庫設計... MySQL的

customers 
id 
name 
.... 

orders 
id 
customers_id 
.... 

上表有一個一對多的關係。 但我希望我的客戶表格數據具有以下表格(組或客戶類型)。 現在如果只加入customer_a那麼就完成了!但如果你想添加customer_b,而添加它;它必須屬於customer_a之一的,並同時增加了customer_c它必須屬於customer_b之一的等&等等...

"importer" "agent"   "distributor" "shops/malls/clients" 
Customer_a Customer_b  customer_c  customer_d 
id   id    id    id 
name  customer_a_id customer_b_id customer_c_id 
...   name   name   name 
      ...    ...    ... 

importer購買的產品和每個importer有許多agents和每個agent有許多distributors和每個distributor有客戶端。 問題是如何在創建訂單時將上述4個表關聯到customers表? 關於如何處理這種情況的任何想法?

+0

客戶之間有什麼關係?一對多或多對多? – Shadow

+1

你的意思是'它必須屬於customer_a的一個'我沒有看到你使用'orders'表來解釋你的解釋,或許你可以從問題中刪除那部分,如果不相關的話。 'Cutomer_a'是'客戶'中的一張桌子還是一排?請閱讀[** How-to-Ask **](http://stackoverflow.com/help/how-to-ask) \t \t這裏是[** START **](http ://spaghettidba.com/2015/04/24/how-to-post-at-sql-question-on-a-public-forum/),瞭解如何提高您的問題質量並獲得更好的答案。 –

+0

我不禁感到你非常非常困惑。 :-( – Strawberry

回答

0

你可以實現你在一個單一的客戶表所需的概念客戶結構:

Create Table Customer_Type 
(
    ID Int Identity Not Null 
    , Type_Description Varchar(20) 
) 
the data will be: 
ID  Type_Description 
1   Importer 
2   Agent 
3   Distributor 
4   Client 
... 


Create Table Customer 
(
    ID Int Identity Not Null 
    , Customer_Type_ID Int 
    , Parent_Customer_ID Int 
    , Name Varchar(100) 
    ... 
) 

你然後通過Orders.Customer_ID = Customer.ID鏈接訂單給客戶,並創建你所描述的層次結構通過Customer.Parent_Customer_ID =客戶ID將客戶代理類型的客戶記錄鏈接到相應的進口商。ID

這有道理嗎?