9

我有一个包含“人”、“公司”、“商店”等表的数据库。其中许多表必须有“联系信息”。在 Database design - Similar Contact Information for multiple entity 这是我的数据库架构:

数据库联系方式

因此,我制作了一个中间表“联系人”,作为将“联系人信息”链接到每个表的最简单方法。
我的问题:这样做并有一个只有一行的表是一个好习惯吗?

4

3 回答 3

8

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

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 是分机。

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.
于 2013-10-28T19:34:56.947 回答
3

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

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

于 2013-10-28T18:17:38.307 回答
2

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

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
于 2014-05-14T23:39:06.517 回答