首页 > 解决方案 > 如何将用户绑定到具有不同角色的不同组织、大学、公司?

问题描述

          USERS

 1. GFER: SUPERADMIN

Foundation(VAG):
  2. John: Incharge
  3. Jessi: IT Head

University(MIT):
 4: Bill: Adminstrator
 5: Gates: Prinicipal investigator
 6: Donald: Researcher

Organizations(YE):
 6. Jason: Head of R&D
 7. Johnny: Researcher

没有用户,大学、组织或公司就无法存在。总会存在一个创建或拥有它的用户。每个用户也会有一个个人资料。

架构如下所示:

用户

- email
- name

角色:

 - id
 - name

大学(简介):

 - name
 - short history

组织机构(简介):

  - name
  - logo of the brand

基金会(简介):

  - name
  - Adminstrative contacts

我如何知道用户绑定到哪个表(基金会、组织或大学)以及它在该组(基金会、组织或大学)中的角色?我想像这样创建一个桥接表:

  Group:
    - id
    - name
    - type(University, Foundation, Organization)
  
 Group_members:
   - id
   - roleId
   - groupId
   - userId

但问题是我无法创建一个groups表格,因为大学、基金会和组织的数据彼此完全不同。所以我必须为每个人创建一个单独的表。我怎么解决这个问题?

标签: mysqlsqldatabasedatabase-designschema

解决方案


-- User USR exists.
--
user {USR}
  PK {USR}
-- Role ROL exists.
--
role_ {ROL}
   PK {ROL}

Xorg是大学、组织或基金会的通用术语。鉴别TYP器用于区分这三者。

-- Xorg XOG, of type TYP, named XNM was created
-- (is owned) by user USR.
--
xorg {XOG, TYP, USR, XNM, ...common_cols}
  PK {XOG}
  SK {XOG, TYP}

CHECK TYP in {'U', 'O', 'F'}

FK {USR} REFERENCES user {USR}
-- University (xorg) XOG, of xorg-type TYP = 'U', exists.
--
university {XOG, TYP, ...university_specific_cols}
        PK {XOG}

CHECK TYP = 'U'

FK {XOG, TYP} REFERENCES xorg {XOG, TYP}
-- Organization (xorg) XOG, of xorg-type TYP = 'O', exists.
--
organization {XOG, TYP, ...organization_specific_cols}
          PK {XOG}

CHECK TYP = 'O'

FK {XOG, TYP} REFERENCES xorg {XOG, TYP}
-- Foundation (xorg) XOG, of xorg-type TYP = 'F', exists.
--
organization {XOG, TYP, ...foundation_specific_cols}
          PK {XOG}

CHECK TYP = 'F'

FK {XOG, TYP} REFERENCES xorg {XOG, TYP}
-- User USR is member of xorg XOG, of xorg-type TYP,
-- in role ROL.
--
user_xorg {USR, XOG, TYP, ROL}
       PK {USR, XOG}

      FK1 {XOG, TYP} REFERENCES
     xorg {XOG, TYP}

      FK2 {USR} REFERENCES user  {USR}
      FK3 {ROL} REFERENCES role_ {ROL}

笔记:

All attributes (columns) NOT NULL

PK = Primary Key
AK = Alternate Key   (Unique)
SK = Proper Superkey (Unique)
FK = Foreign Key

关于子类型的一句话。实现子类型约束的正确方法是使用断言 ( CREATE ASSERTION),但它在主要数据库中仍然不可用。我正在使用FKs,并且与所有其他替代方法一样,它并不完美。人们争论很多,关于 SO 和 SE-DBA,什么更好。我鼓励您也检查其他方法。


推荐阅读