2017-08-25 95 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

这有道理吗?