Mysql: enum confusion

ぐ巨炮叔叔 提交于 2019-12-06 12:39:51

You should really make 3 tables, assuming that an employee can have multiple interests. (Your current design limits each employee to 1 interest.) Something like this:

Employee (emp)
-------
id
name

Interest
-------
id
description

Employee_Interest
--------
employeeID
interestID

Regarding the edit, I'd say the enum is the better of your 2 examples as it limits you to predetermined allowable values. But many would argue that you should make a lookup table (Role with id and description) even for that

There's a third option, creating an additional table for holding the interest values.

INTERESTS

  • interest_id, int, primary key
  • interest_value, string/varchar, unique constraint (to stop duplicates)

Many-to-Many Relationship?


However, if you want to support an employee having multiple interests you'll need a third table. This is a many-to-many relationship - the third table would sit between the EMPLOYEES and INTERESTS tables, and have foreign key relationships with both.

EMPLOYEE_INTERESTS

  • employee_id, primary key, foreign key to EMPLOYEES.id
  • interest_id, primary key, foreign key to INTERESTS.interest_id

One-to-Many Relationship?


If an EMPLOYEES record can only ever have one INTEREST, then you only need to update the EMPLOYEES.interest column to have a foreign key relationship with the INTERESTS table.

EMPLOYEES

  • interest_id, primary key, foreign key to INTERESTS.interest_id
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!