1

I am trying to figure out how to create this relationship. It is for employees that participate in an incentive program. They can participate multiple years and also be doing the same job. So it is a many to many relationship. I also need to be able to get the records by the year. So is this the best way? Is is even possible?

Employee Table:

EmpID PK
Year PK

Job Table:

JobID PK
Year  PK
4

2 回答 2

1

对于多对多关系,标准方法是创建一个单独的表(mysql 语法):

CREATE TABLE employee_job
( empl_id integer not null,
  job_id integer not null,
  foreign key (empl_id) references employee(empl_id),
  foreign key (job_id) references job(job_id)
)
于 2012-07-15T08:09:49.687 回答
0

如果你只是想存储哪个员工在哪一年参加了什么工作,可以有一个更简单的设计:

Employee        
--------
EmpID PK

Job
--------
JobID PK

Year (optional)
--------
Year  PK

Participation        
-------------
EmpID PK, FK1
JobID PK, FK2
Year PK, FK3  (the FK3 is optional)

如果您还想在单独的表中存储哪些工作在哪一年有效/可用,哪些员工在哪一年受雇(例如,如果您的员工受雇了几年但没有参与任何工作,或者如果您的工作是可用但没有人在其中工作),您可以使用您建议的更复杂的设计:

Employee        
--------
EmpID PK

Job
--------
JobID PK

Year
--------
Year  PK

Employment
----------
EmpID PK, FK 
Year PK, FK

JobActivity
-----------
JobID PK, FK 
Year PK, FK

Participation        
-------------
EmpID PK, FK1
JobID PK, FK2
Year PK, FK1, FK2 

FK1 将参考Employment(EmpID, Year),FK2 将参考JobActivity(JobID, Year)

于 2012-07-15T09:04:06.290 回答