只有几个特定值的最佳数据类型是什么?

时间:2020-12-25 16:56:50

I have some values like these:

我有一些这样的价值观:

  • PHP
  • PHP
  • CSS
  • CSS
  • HTML
  • HTML
  • JavaScript
  • JavaScript

I want to know is that possible to define some specific values for a column as valid values? (everything else would be invalid)

我想知道是否可以将列的某些特定值定义为有效值?(其他一切都是无效的)


I know, I can implement a tagging-system the other way. Creating a separated table which contains valid tags and checks everything for validating.

我知道,我可以用另一种方式实现标记系统。创建一个单独的表,其中包含有效的标记,并检查一切以进行验证。

But actually that tagging-system is just as an example. All I'm trying to do is defining some constant values for a column.

但实际上标签系统只是一个例子。我要做的就是为一列定义一些常数值。

2 个解决方案

#1


2  

You could use an ENUM Type and define your constants when creating the table:

您可以使用ENUM类型并在创建表时定义常量:

http://dev.mysql.com/doc/refman/5.7/en/enum.html

http://dev.mysql.com/doc/refman/5.7/en/enum.html

I would only use it if it is a fixed list of some values. Like "male", "female".

如果它是某个值的固定列表,我将只使用它。像“男”、“女”。

#2


2  

A typical way of doing this uses a foreign key relationship.

一种典型的方法是使用外键关系。

You can define a table of values like this:

您可以定义如下的值表:

create table skills (
    skillId int auto_increment primary key,
    skill varchar(255) unique
);

insert into table_of_values (value)
    select 'PHP' union all
    select 'CSS' union all
    . . . ;

Then you can validate using a foreign key. Say:

然后可以使用外键进行验证。说:

create table PeopleSkills (
    PeopleSkillsId int auto_increment primary key,
    PeopleId int,
    Skill varchar(255),
    constraint fk_PeopleSkills_PeopleId foreign key (PeopleId) references People(PeopleId),
    constraint fk_PeopleSkills_Skills foreign key (Skill) references skills(skill)
);

Note: This example uses strings for the foreign key reference. I don't actually advocate that approach -- but that is the direct answer to your question. In practice, the constraint can use the id rather than the name:

注意:这个示例使用字符串作为外键引用。我并不提倡这种方法——但这是对你问题的直接回答。在实践中,约束可以使用id而不是名称:

create table PeopleSkills (
    PeopleSkillsId int auto_increment primary key,
    PeopleId int,
    SkillId int, 
    constraint fk_PeopleSkills_PeopleId foreign key (PeopleId) references People(PeopleId),
    constraint fk_PeopleSkills_SkillId foreign key (SkillId) references Skills(skillId)
);

#1


2  

You could use an ENUM Type and define your constants when creating the table:

您可以使用ENUM类型并在创建表时定义常量:

http://dev.mysql.com/doc/refman/5.7/en/enum.html

http://dev.mysql.com/doc/refman/5.7/en/enum.html

I would only use it if it is a fixed list of some values. Like "male", "female".

如果它是某个值的固定列表,我将只使用它。像“男”、“女”。

#2


2  

A typical way of doing this uses a foreign key relationship.

一种典型的方法是使用外键关系。

You can define a table of values like this:

您可以定义如下的值表:

create table skills (
    skillId int auto_increment primary key,
    skill varchar(255) unique
);

insert into table_of_values (value)
    select 'PHP' union all
    select 'CSS' union all
    . . . ;

Then you can validate using a foreign key. Say:

然后可以使用外键进行验证。说:

create table PeopleSkills (
    PeopleSkillsId int auto_increment primary key,
    PeopleId int,
    Skill varchar(255),
    constraint fk_PeopleSkills_PeopleId foreign key (PeopleId) references People(PeopleId),
    constraint fk_PeopleSkills_Skills foreign key (Skill) references skills(skill)
);

Note: This example uses strings for the foreign key reference. I don't actually advocate that approach -- but that is the direct answer to your question. In practice, the constraint can use the id rather than the name:

注意:这个示例使用字符串作为外键引用。我并不提倡这种方法——但这是对你问题的直接回答。在实践中,约束可以使用id而不是名称:

create table PeopleSkills (
    PeopleSkillsId int auto_increment primary key,
    PeopleId int,
    SkillId int, 
    constraint fk_PeopleSkills_PeopleId foreign key (PeopleId) references People(PeopleId),
    constraint fk_PeopleSkills_SkillId foreign key (SkillId) references Skills(skillId)
);