数据库设计 - 不同表的多个“联系信息”

时间:2013-10-28 17:49:11

标签: database database-design relational-database contacts

我有一个包含表“person”,“company”,“shop”等的数据库。这些表中的许多表必须具有“联系信息”。在Database design - Similar Contact Information for multiple entities中询问了设计此问题的可能性 现在,在我的数据库中,我可以为每个联系人数据提供多个地址,多个电话和多封电子邮件。这是我的数据库架构:

database contact information

所以,我将中间表“联系”作为将“联系信息”链接到每个表格的最简单方法。
我的问题:这样做是不错的做法,并且只有一行表?

3 个答案:

答案 0 :(得分:7)

这就是我设计数据库的方式:

address_types
    id              unsigned int(P)
    description     varchar(10) // Mailing, Physical, etc.

addresses
    id              unsigned int(P)
    line1           varchar(50) // 123 Main Street, etc.
    line2           varchar(50) // Default NULL
    city_id         unsigned int(F cities.id)
    zip             varchar(6) // 12345, A1A 1A1, etc.
    zip4            char(4) // Default NULL
    lat             decimal(10,8) // 13.12345678, etc.
    lon             decimal(11,8) // 110.12345678, etc.

cities
    id              unsigned int(P)
    state_id        unsigned int(F states.id)
    name            varchar(50) // Omaha, Detroit, Tampa, etc.

companies
    id              unsigned int(P)
    name            varchar(75) // IBM, Microsoft, RedHat, etc.
    ...

companies_addresses
    id                  unsigned int(P)
    company_id          unsigned int(F companies.id)
    address_id          unsigned int(F addresses.id)
    address_type_id     unsigned int(F address_types.id)

companies_contacts
    id                  unsigned int(P)
    company_id          unsigned int(F companies.id)
    contact_id          unsigned int(F contacts.id)
    contact_type_id     unsigned int(F contact_types.id)

companies_emails
    id                  unsigned int(P)
    company_id          unsigned int(F companies.id)
    email_id            unsigned int(F emails.id)
    email_type_id       unsigned int(F email_types.id)

contact_types
    id              unsigned int(P)
    description     varchar(10) // Home phone, Mobile phone, FAX, etc.

在北美,电话号码如下:CC-AAA-EEE-SSSS-XXXXXXX,其中CC是国家代码,AAA是区号,EEE是交换机,SSSS是电台,XXXXX是电话号码。< / p>

contacts
    id              unsigned int(P)
    country_code    varchar(3)
    area_code       varchar(3)
    exchange        varchar(3)
    station         varchar(4)
    extension       varchar(10) // Default NULL

请参阅ISO 3166-1

countries
    id              char(2) // ca, mx, us, etc.
    iso3            char(3) // can, mex, usa, etc.
    iso_num         char(3)
    name            varchar(44) // Canada, Mexico, United States, etc.

email_types
    id              unsigned int(P)
    description     varchar(10) // Personal, Work, etc.

emails
    id              unsigned int(P)
    address         varchar(255) // support@ibm.com, etc.

shops
    id              unsigned int(P)
    name            varchar(45) // Shop A, Shop B, etc.
    ...

shops_addresses
    id                  unsigned int(P)
    shop_id             unsigned int(F shops.id)
    address_id          unsigned int(F addresses.id)
    address_type_id     unsigned int(F address_types.id)

shops_contacts
    id                  unsigned int(P)
    shop_id             unsigned int(F shops.id)
    contact_id          unsigned int(F contacts.id)
    contact_type_id     unsigned int(F contact_types.id)

shops_emails
    id                      unsigned int(P)
    shop_id                 unsigned int(F shops.id)
    email_id                unsigned int(F emails.id)
    email_type_id           unsigned int(F email_types.id)

请参阅ISO 3166-2

states
    id              unsigned int(P)
    country_id      char(2)(F countries.id)
    code            varchar(2) // AL, NF, NL, etc.
    name            varchar(50) // Alabama, Newfoundland, Nuevo León, etc.

答案 1 :(得分:3)

  

我的问题:这样做是一个好习惯,并且只有一行表吗?

不是真的。看看你的图表,我不得不问:联系人真的可以链接到任意数量的人吗?如果没有,您应该使用'person'作为父表,并使其他表链接到它。

答案 2 :(得分:2)

Personaly,我不喜欢这种方法,因为我们需要为每个表和很多关联表创建任何实体。我建议一个用于联系信息的表和另一个用于地址的表。

informations
- id INT
- name VARCHAR
- value VARCHAR
- type ENUM(phone, email, url, custom)
- idcompany INT NULL
- idcontact INT NULL

addresses
- id
- address1
- address2
- district
- postcode
- idcity
- idcompany
- idcontact