0

データベースの設計について助けが必要です。

これまでのところ、3 つのテーブルがあります。

TblDepartments
--------------
部門ID (主キー)
部署名

TblSections
-----------
セクション ID (PK)
部門 ID (外部キー)
セクション名

TblWorkers
----------
ワーカー ID (PK)
ワーカー名

部門と課の間には 1 対 N の関係があります (1 つの部門には複数の部門があり、1 つの部門は 1 つの部門に属します)。

これで、作業者はセクション レベルまたは部門レベルでロールを持つことができます (つまり、部門のすべてのセクションで同じロールを持つことができます)。

Roles テーブルをどのように定義すればよいかわかりません。私はこの定義を持ってきました:

TblRoles
--------
WorkerID (PK)(FK)
部門ID (PK)(FK)
セクション ID (PK)(FK)
RoleDesc

しかし、私はこの解決策が好きではなく、間違っていると感じています。(DeptID または SectionID のいずれかが null である必要があり、SectionID は DeptID に依存します)。

Roles テーブルを定義するより良い方法はありますか?

4

2 に答える 2

0

セクションと部門の構造がほぼ同じである場合は、自己結合を使用できます

Create Table Departments 
    ( 
    DeptId ... not null Primary Key
    , ParentDeptId null References Departments ( DeptId )
    , Name ... not null Unique
    )

この構造では、null の ParentDeptId を持つ部門は部門であり、null 以外の ParentDeptId を持つ部門はセクションです。次に、ロールテーブルは簡単です:

Create Table Roles
    (
    WorkerId ... not null References Workers ( WorkerId )
    , DeptId ... not null References Departments ( DeptId )
    , Description ... not null
    )

もう 1 つの選択肢は、会社の階層をキャプチャするテーブルを作成することです。

Create Table Departments 
    ( 
    DeptId ... not null Primary Key
    , Name ... not null Unique
    )

Create Table Sections
    (
    SectionId ... not null Primary Key
    , Name ... not null Unique
    )

次の表で、SectionId が null の場合は部門を表し、SectionId が null でない場合は明らかにセクションを表します。

Create Table CompanyAreas
    (
    Id ... not null Primary Key
    , DeptId ... not null References Departments ( DeptId )
    , SectionId ... null References Sections ( SectionId )
    , Unique ( DeptId, SectionId )
    )

Create Table WorkerRoles
    (
    CompanyAreaId ... not null References CompanyAreas ( Id )
    , WorkerId ... not null References Workers ( WorkerId )
    , Description ... not null
    )
于 2013-06-04T15:03:46.033 に答える