简体   繁体   English

在MySQL中存储短号的最佳方法

[英]Best way to store a short number in mysql

I am trying to fix a project that someone else coded, it is a mess! 我正在尝试修复别人编码的项目,真是一团糟!

On an edit user profile page they have something like this below which will list all available radio select boxes for the options a user can select for there education level, this is just 1 item, there is many items like it so based on the answers here, I can apply it to many other features on this code. 在“编辑用户个人资料”页面上,他们下面有类似的内容,其中列出了用户可以为该学历选择的选项的所有可用单选框,这只是一项,有很多类似的内容,因此根据此处的答案,我可以将其应用于此代码的许多其他功能。

They used an array for many profile fields, it would store possible answers, A user can only choose 1 answer though in this case. 他们为许多配置文件字段使用了一个数组,它将存储可能的答案。尽管在这种情况下,用户只能选择1个答案。 So for education, there is 7 possible answers and a number corresponding with it's number in the array is stored in a mysql DB, they store this 1 number as a varchar 255 length which can't be good. 因此,对于教育来说,有7个可能的答案,并且与数组中的数字相对应的数字存储在mysql DB中,他们将这1个数字存储为varchar 255长度,这不太好。

Should I store it as a varchar with 1 for length or use enum and list the 7 options, or something else? 我应该将其存储为长度为1的varchar还是使用enum并列出7个选项或其他内容?

Education 教育

<?PHP
// Array of possible education profile selections
$arr_education[1]="No Answer";
$arr_education[2]="High school";
$arr_education[3]="Some college";
$arr_education[4]="In college";
$arr_education[5]="College graduate";
$arr_education[6]="Grad / professional school";
$arr_education[7]="Post grad";

// shows the array above as checkboxes on a form
foreach($arr_education as $ind=>$val) {  
    echo '<input type="radio" name="education" value="' .$ind. '" ' if($education==$ind){ echo "checked";}. '>' .$val. '<br>';
}



//on a users profile page there would be something like this
$education = 7; // seven would be Post Grad from our array above
echo $arr_education[$education]

//Now here is the mysql table, they chose to use a varchar(255) which seems retarded, using the above code, the max result would be 1 charachter long
education   varchar(255)

?>

Why not use TINY INT ? 为什么不使用TINY INT It suits better your needs if the answers would be < 255 :) My tests show that it's faster than varchrars in indexing and selecting. 如果答案小于255,则它更适合您的需求:)我的测试表明,在索引和选择方面,它比varchrars更快。

If this was a new project, I'd recommend using ENUM. 如果这是一个新项目,建议您使用ENUM。 It's space efficient (it's actually stored on 1 byte) and simple to use. 它节省空间(实际上存储在1个字节中)并且易于使用。 However, if you have to support legacy code then I'd recommend sticking to raw numbers and change that column into a TINYINT. 但是,如果您必须支持旧代码,那么我建议您坚持使用原始数字并将该列更改为TINYINT。

Additionally, I'd recommend replacing the numbers in your PHP source code with constants, so that you don't have to guess what the code does. 另外,我建议用常量替换PHP源代码中的数字,这样您就不必猜测代码的作用了。 For example: 例如:

define('EDU_NO_ANSWER', 0);
define('EDU_HIGH_SCHOOL', 1);
define('EDU_SOME_COLLEGE', 2);
// etc...

if ($education >= EDU_SOME_COLLEGE)
{
    // student went to college, but you didn't need that
    // comment to know that
}

For that, a varchar(1) would take up exactly as much space as a varchar(255) except if the enum ever reached double digits, a varchar(1) would break. 为此,varchar(1)会占用与varchar(255)一样多的空间,除非枚举达到两位数,则varchar(1)会中断。 A varchar field that is <= 255 max chars stores 1 byte for the length of the string, and then the string. 小于等于255个最大字符的varchar字段存储1个字节作为字符串的长度,然后存储字符串。 So for a one-digit number, varchar(255) takes 2 bytes. 因此,对于一个数字,varchar(255)需要2个字节。

Why they didn't use one of the integer classes, however, is beyond me. 为什么他们不使用整数类之一,这超出了我的范围。

如果可能的项目不会经常更改,那么您应该使用ENUM,它是最节省空间的,并且比在其他地方定义魔术数字更容易理解所有查询。

If it was me, I would create a separate table with the profiles in it, then populate the Select tag with the results of a SQL query. 如果是我,我将创建一个单独的表,其中包含配置文件,然后使用SQL查询的结果填充Select标记。 Make the index an integer (after all you never know what this will grow into..) It would also allow the enduser to change the descriptions without having to touch code (assuming they have SQL access) 将索引设置为整数(毕竟您永远不知道它会增长成什么。。)它还使最终用户无需触摸代码即可更改描述(假设他们具有SQL访问权限)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM