我有一张employee表,employee有兴趣,所以表可以这样设计:
create table emp(
id int(10) not null auto_increment,
name varchar(30),
interest varchar(50),
primary key(id)
);
或者这个:
create table emp(
id int(10) not null auto_increment,
name varchar(30),
interest enum('football','basketball','music','table tennis','volleyball'),
primary key(id)
);
兴趣的数量可以在50个左右。
我应该如何设计表格?我应该使用枚举还是其他?
编辑:
感谢您的回复。
假设一个人可以是先生或女士或女士
我用 PHP 制作了一个下拉列表。
<select name="role">
<option value="Mr.">Mr.</option>
<option value="Ms">Ms</option>
<option value="Madame">Madame</option>
</select>
对于数据库部分,我可以这样做:
create table emp(
id int(10) not null auto_increment,
name varchar(30),
role varchar(50),
primary key(id)
);
或者这个:
create table emp(
id int(10) not null auto_increment,
name varchar(30),
role enum('Mr.','Ms.','Madame'),
primary key(id)
);
在这种情况下,哪个更好?
最佳答案
还有第三种选择,创建一个额外的表来保存利息值。
兴趣
interest_id
, int, primary keyinterest_value
, string/varchar, unique constraint (to stop duplicates)
多对多关系?
但是,如果您想要支持具有多种兴趣的员工,您将需要第三张表。这是一个多对多关系——第三个表将位于 EMPLOYEES 和 INTERESTS 表之间,并且与两者都有外键关系。
EMPLOYEE_INTERESTS
employee_id
,主键,EMPLOYEES.id 的外键interest_id
,主键,INTERESTS.interest_id 的外键
一对多关系?
如果一条 EMPLOYEES 记录只能有一个 INTEREST,那么您只需更新 EMPLOYEES.interest 列以与 INTERESTS 表建立外键关系。
员工
interest_id
,主键,INTERESTS.interest_id 的外键
关于sql - Mysql:枚举困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2343450/