什么是只接受一些特定值的最佳数据类型?

时间:2016-06-08 11:54:21

标签: mysql sql sqldatatypes

我有一些像这样的值:

  • <!DOCTYPE html> <html> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> <title>Untitled 1</title> <style type="text/css"> ul.icon-menu {margin-top:29px;} li.icon-box {width: 120px; height: 120px; list-style: none; left: -47px; position: relative; margin-bottom: 3px;} li.icon-box.home {background: #e74c3c;} li.icon-box.aboutme {background: #1dd0ad;} li.icon-box.portfolio {background: #3498db;} li.icon-box.blog {background: #f1c40f;} li.icon-box.contact {background: #f39c12;} .icon-box h2{Museo500-Regular; font-size: 20px; text-shadow: 1px 1px 2px rgba(150, 150, 150, 1);} .icon-box a {display: block;} /*= TITLE BOXES --------------------------------------------------------*/ .icon-box h2 { position: absolute; top: 0; left: 0; opacity: 0; background: #E74C3C; line-height: 120px; width: 120px; -webkit-transition: all .3s; -moz-transition: all .5s; -ms-transition: all .5s; -o-transition: all .5s; transition: all .5s; border-left: 3px solid #a7382d; } .icon-box a:hover h2 { opacity: 1; left: 120px; margin: 0; text-align: center; } /*= MENU ICONS --------------------------------------------------------*/ span.icon { display: inline-block; background: url('../img/icon-sprites.png')no-repeat; width: 32px; height: 32px; margin: 43px 40px;} span.icon.home { background-position: 0px 0px;} span.icon.aboutme { background-position: -36px 0px;} span.icon.portfolio { background-position: -72px 0px;} span.icon.blog { background-position: -109px 0px;} span.icon.contact { background-position: -145px 0px;} </style> </head> <body> <div class="container"> <ul class="icon-menu"> <li class="icon-box home"> <a href="#"> <span class="icon home"></span> <h2>Home</h2> </a> </li> <li class="icon-box aboutme"> <a href="#"> <span class="icon aboutme"></span> <h2>About Me</h2> </a> </li> <li class="icon-box portfolio"> <a href="#"> <span class="icon portfolio"></span> <h2>Portfolio</h2> </a> </li> <li class="icon-box blog"> <a href="#"> <span class="icon blog"></span> <h2>Blog</h2> </a> </li> <li class="icon-box contact"> <a href="#"> <span class="icon contact"></span> <h2>Contact</h2> </a> </li> </ul> </div> </div> </body> </html>
  • PHP
  • CSS
  • HTML

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

我知道,我可以用另一种方式实现标记系统。创建一个包含有效标记的分隔表,并检查所有要验证的内容。

但实际上,标记系统就是一个例子。我试图做的就是为列定义一些常量值

2 个答案:

答案 0 :(得分:2)

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

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

如果它是某些值的固定列表,我只会使用它。像“男性”,“女性”。

答案 1 :(得分:2)

执行此操作的典型方法是使用外键关系。

您可以像这样定义一个值表:

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
    . . . ;

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

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)
);

注意:此示例使用字符串作为外键引用。我实际上并没有提倡这种方法 - 但这是你问题的直接答案。实际上,约束可以使用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)
);