Sql server 2008 如何设计工作人员角色表
我需要数据库设计方面的帮助 到目前为止,我有三张桌子: TblDepartments -------------- DeptID (PK) DeptName TblSections ----------- SectionID (PK) DeptID (FK) SectionName TblWorkers ---------- WorkerID (PK) WorkerName TBL部门 -------------- 部门主任(主键) 系名 TblSections ----------- 分区ID(主键) 部门主任(FK) 节名 工友 ---------- WorkerID(主键) 工作名称 部门和部门之间存在1:N关系(一个部门可能有多个部门,一个部门属于一个部门) 现在,员工可以在部门级别或部门级别担任角色(即在部门的所有部门中担任相同的角色) 我不确定应该如何定义角色表。我有这样的定义: TblRoles -------- WorkerID (PK)(FK) DeptID (PK)(FK) SectionID (PK)(FK) RoleDesc TblRoles -------- 工作ID(主键)(FK) 工业贸易署署长(九龙)(九龙) 分区ID(主键)(FK) 角色扮演者 但我不喜欢这个解决方案,我觉得它是错误的。(DeptID或SectionID必须为null,并且SectionID仍然依赖于DeptID)Sql server 2008 如何设计工作人员角色表,sql-server-2008,database-design,Sql Server 2008,Database Design,我需要数据库设计方面的帮助 到目前为止,我有三张桌子: TblDepartments -------------- DeptID (PK) DeptName TblSections ----------- SectionID (PK) DeptID (FK) SectionName TblWorkers ---------- WorkerID (PK) WorkerName TBL部门 -------------- 部门主任(主键) 系名 TblSections ----------- 分区
有没有更好的方法来定义角色表?首先,您需要一个只包含角色的表,因为您不能有重复的数据
roles
-----
roleId
name
一名员工可以担任多个角色吗
workerRole
----------
workerId
roleId
如果工作者只有一个角色,只需将其添加到工作者表中即可
worker
------
workerId
name
roleId
如果角色可以属于某个部门,则每个部门对应一个表:
departmentRole sectionRole
-------------- -----------
departmentId sectionId
roleId roleId
如果部门和部门的结构几乎相同,则可以使用自联接
Create Table Departments
(
DeptId ... not null Primary Key
, ParentDeptId null References Departments ( DeptId )
, Name ... not null Unique
)
在此结构中,ParentDeptId为空的部门是部门,ParentDeptId为非空的部门是部门。那么,您的角色表就是直截了当的:
Create Table Roles
(
WorkerId ... not null References Workers ( WorkerId )
, DeptId ... not null References Departments ( DeptId )
, Description ... not null
)
另一种选择是创建一个表来捕获公司的层次结构
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
)