【问题标题】:Database design with types and subtypes具有类型和子类型的数据库设计
【发布时间】:2015-08-11 00:21:08
【问题描述】:

我正在尝试设计一个具有超类型/子类型的数据库。我有以下用户:
- 基本用户
- 企业用户
- 管理员(超级用户)。

所有三个用户共享一个公用表(用户),其中存储了姓名、电子邮件、密码等字段。但是,业务用户有一个单独的表(业务),其中存储了业务名称、业务许可证、业务电子邮件等业务字段。我的问题是我的业务用户本身被分为 5 个或更多类别。
我的业务用户是这样划分的:

  1. 画家
  2. 车辆细节师
  3. 很多服务员
  4. 服务技术人员:
    1. 发动机技术人员
    2. 传输技术人员
    3. 计算机技术人员
    4. 电气技术人员
  5. 销售代表
    1. 实际位置销售代表
    2. 互联网销售代表
    3. 销售经理

要提一提的是,我希望将所有这些业务用户存储到某个表中,以便轻松添加和删除职位,而不会在应用程序级别造成太多麻烦。

我目前设计了这个数据库,但我对它并不满意。有更好的方法吗?

【问题讨论】:

    标签: mysql database-design subtype


    【解决方案1】:

    你的设计不错。我喜欢。将一些数据放入其中并编写一些查询。如果您愿意,可以将其正常化一点。这是您可以使用http://sqlfiddle.com/#!9/b0711/3 的 SQLFiddle,下面是语句。

    用户和类型

    create table usertypes (id int, typename varchar(100));
    insert into usertypes values (1,'Basic'), (2, 'Business'), (3, 'Super');
    
    create table users (
      id int, 
      email varchar(100), usertype int, fullname varchar(100)
    );
    insert into users values 
    (1, 'a@b.com', 1, 'Tom Basic'),
    (2, 'b@c.com', 2, 'Bill Business'),
    (3, 'c@d.com', 3, 'Charlie Super');
    
    -- USERS will have SUBTYPES in this many to many table. This will allow
    -- a user to be a part of multiple subtypes if you please. You can control
    -- that. If userid is primary key, one user can be of only one subtype
    -- If userid and usertype make up a composite primary key, a user can be
    -- of multiple types
    create table userstypes (userid int, usertype int);
    insert into userstypes values (1, 1), (2, 2), (3, 3);
    

    企业和用户

    create table businesses (
      id int,
      doing_business_as varchar(100), phone varchar(30)
    );
    insert into businesses values (1, 'Microsoft', '111-222-3333');
    insert into businesses values (2, 'Toms Hardware', '111-222-3333');
    
    -- Same as user types
    -- DANGER: if you mark a user to be of type 'Basic', nothing stops that
    -- user to have a business. You can use a trigger to allow only business
    -- user to have an entry here
    create table usersbusinesses (userid int, businessid int);
    insert into usersbusinesses values (1,2), (2, 1);  
    

    业务子类型

    create table businesstypes (id int, businesstypename varchar(100));
    insert into businesstypes values (1, 'Software'), (2, 'Hardware');
    
    create table businessestypes (businessid int, businesstypes int);
    insert into businessestypes values (1, 1), (2, 2);
    
    -- DANGER: This design allows only 1 level of subtype. If a business
    -- has a subtype, and that subtype has a sub-subtype and so on, this
    -- design will not scale. Hierarchical design will scale but may also 
    -- need performance tuning
    create table businesssubtypes (id int, businesssubtypename varchar(100));
    insert into businesssubtypes values (1, 'Garden Tools'), (2, 'Heavy Machine');
    
    create table businesstypes_subtypes (businessid int, businesssubtypeid int);
    insert into businesstypes_subtypes values (2,2);
    

    根据您的应用程序需求,我建议您进行适当的非规范化。对于非常小的、非常省力的项目,我会在一张桌子上制作一个扁平的结构。

    【讨论】:

      猜你喜欢
      • 2012-01-22
      • 2012-08-28
      • 2011-03-07
      • 2011-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-17
      • 1970-01-01
      相关资源
      最近更新 更多