简体   繁体   English

表格设计问题,用于规范共享相似数据的多个类别

[英]table design issue for normalizing multiple catogories sharing similar data

I'm little confused in a table design. 我对表格设计有点困惑。

Method 1) With first method i can select all phone number from phone_book table and filter out employee phone numbers by specifying type. 方法1)使用第一种方法,我可以从phone_book表中选择所有电话号码,并通过指定类型过滤掉员工电话号码。 Method 2) In second method i can select all by joining two table and can retrieve employee phone numbers. 方法2)在第二种方法中,我可以通过连接两个表来选择全部,并且可以检索员工的电话号码。

I've same issue with addresses. 地址也有相同问题。 I've address of employees, customers, staffs and others. 我有员工,客户,员工和其他人的地址。 I've googled and still cant choose which is right method. 我已经用谷歌搜索,但仍然无法选择哪种方法是正确的。 Which is the right method and why the other not and any other better design for this ? 哪一种是正确的方法?为什么不这样做,以及对此有何其他更好的设计?

Method 1 方法1

employee
id | name 
1      e


customer
id | name
1     c


phone_book
type | fk_id |     phone
  e      1         123123123
  c      1         451323123

Method 2 方法二

employee
id | name 
1      e


customer
id | name
1     c


employee_phone_book
emp_id  |     phone
   1         1231233434
   2         6273343423


customer_phone_book
cus_id  |  phone
   1       5231233434
   2       1251233434

Edit: Employee and customers can have multiple phone numbers. 编辑:员工和客户可以有多个电话号码。 And same with the address details. 并与地址详细信息相同。

employee
id | name | phone_id
1      e    1


customer
id | name | phone_id
1     c     2


phone_book
phone_id |     phone
1         123123123
2         451323123

For me the reference to phone should be in the employee and customer tables (FK to the phone_book table) 对我来说,电话的引用应该在employeecustomer表中(FK到phone_book表)

UPDATE: If customer/employee may have multiple phones it would be better to define many-to-many relations via additional table eg 更新:如果客户/员工可能有多部电话,最好通过附加表来定义多对多关系,例如

customer_phone 
phone_id |     customer_id (reference to the customer table)
1              1
2              1

If employee and customers are having multiple phone numbers than your phone_book should look something like this. 如果员工和客户拥有多个电话号码,则您的phone_book应该看起来像这样。 you need to create one is to many relation table structure. 您需要创建一个对许多关系表的结构。

phone_book
phone_id | EmpOrCustomer | EmpOrCustomerId | phone
1          1               1                  123123123
2          1               1                  45132XXXX
3          2               4                  45132XXXX

In the above table structure, you can add two additional column which tells you its employee or customer, another which tells what is the employee or customer id. 在上面的表结构中,您可以添加另外两个列,告诉您其雇员或客户,另一列告诉您什么是雇员或客户ID。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM