你的设计不错。我喜欢。将一些数据放入其中并编写一些查询。如果您愿意,可以将其正常化一点。这是您可以使用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);
根据您的应用程序需求,我建议您进行适当的非规范化。对于非常小的、非常省力的项目,我会在一张桌子上制作一个扁平的结构。