首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >具有类型和子类型的数据库设计

具有类型和子类型的数据库设计
EN

Stack Overflow用户
提问于 2015-08-11 00:21:08
回答 1查看 1.2K关注 0票数 2

我正在尝试设计一个具有超级类型/子类型的数据库。我有以下用户:

  • 基本用户
  • 业务用户
  • 管理员(超级用户)

所有三个用户都共享一个公共表(用户),其中存储姓名、电子邮件、密码等字段。但是,业务用户有一个单独的表(业务),其中存储业务字段(如business_name、business_license、business_email等)。我的问题是,我的业务用户自己被分成了5个或更多个类别。

我的业务用户是这样分裂的:

  1. 画家
  2. 车辆详细说明
  3. 抽签员
  4. 服务技术人员:
    1. 发动机技师
    2. 传动技术人员
    3. 计算机技术人员
    4. 电气技师

  1. 销售代表
    1. 实体位置销售代表
    2. 互联网销售代表
    3. 销售经理

有一点要提到的是,我希望将所有这些业务用户存储在某个表中,这样就可以轻松地添加和删除位置,而不会在应用程序级别上造成很大的混乱。

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

EN

回答 1

Stack Overflow用户

发布于 2015-08-11 03:35:18

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

用户和类型

代码语言:javascript
代码运行次数:0
运行
复制
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);

业务和用户

代码语言:javascript
代码运行次数:0
运行
复制
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);  

业务子类型

代码语言:javascript
代码运行次数:0
运行
复制
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);

根据您的应用程序需求,我建议您进行适当的去规范化。对于非常小的、非常低工作量的项目,我会在一个表中创建一个平面结构。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31931222

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档