1

在这个数据库中,有时员工只在一个司法管辖区,有时不止一个。例如,Bob 的司法管辖区将是北美、南美和非洲。而简斯将只是北美。但是,每个员工都会有一个部门。

由于某些员工只会分配一个管辖区,而其他员工可能会分配更多管辖区,这是实施管辖区的最佳方式吗?或者有没有更有效的方法?

我将使用连接语句并按部门选择我的查询。

employees
-----------------
userID (primary key)
deptID (foreign key and NOT NULL references departments)
firstName
lastName
jurisdiction1 (foreign key references jurisdictions)
jurisdiction2 (foreign key references jurisdictions)
jurisdiction3 (foreign key references jurisdictions)
jurisdiction4 (foreign key references jurisdictions)
jurisdiction5 (foreign key references jurisdictions)
jurisdiction6 (foreign key references jurisdictions)
jurisdiction7 (foreign key references jurisdictions)

jurisdictions
-------------------
jurID primary key
jurisdictionName


departments
--------------------
deptID primary key
departmentName
4

2 回答 2

2

您的方案中的管辖表设计不正确。您需要一个中间表来存储用户和区域之间的关系。像这样。

employee table
--------------
user_id
first_name
depart_id
j_id

jurisdiction table
-----------------
j_id
region

employee_jurisdiction
------------------
id
user_id 
j_id
(Set INDEX as no duplicate for user_id+j_id)     
于 2012-12-13T06:51:41.890 回答
1

在这种情况下,最好有一个新表jurisdictions,并且EmployeesJurisdictions

Jurisdictions

  • Id,
  • JurisdictionName.

UsersJurisdictions

  • EmployeeId外键引用Employees(EmployeeId)
  • JurisdictionId一个外键引用jurisdictions(jurisdictionId)
于 2012-12-13T06:48:20.323 回答