简体   繁体   English

对于0到3之间的值,我应该使用哪种数据类型

[英]What datatype should I use for values between 0 to 3

My field in the database will contain only 3 possible values: 0, 1 and 2. So, I'm wondering what datatype should I use? 我在数据库中的字段将仅包含3个可能的值:0、1和2。所以,我想知道应该使用哪种数据类型? I found this and it seems that I should use tinyint(1), but that has range from -127 to 126, so am wondering is that my choice or is there actually something better? 我发现了这一点 ,似乎应该使用tinyint(1),但是它的范围是-127到126,所以想知道是我的选择还是实际上有更好的选择?

tinyint(1) unsigned可能是适合的选择

Not recommended but... I was initially thinking if space is really that big of an issue this may have been worth exploring. 不推荐,但是...我最初是在考虑空间是否真的是一个大问题,这可能值得探讨。

If you can live with NULL, 0, 1 instead of 0,1,2, then boolean/bit may work as well. 如果您可以使用NULL,0、1而不是0,1,2,则布尔值/位也可以使用。

However, looks like boolean and tinyint are practically the same. 但是,看起来布尔值和tinyint实际上是相同的。 And tinyint has the smallest footprint of all datatypes in mysql. 并且tinyint在mysql中的所有数据类型中具有最小的占用空间。 See link . 链接

If you wish to make sure that the number entered is only 0, 1, or 2 then it would be best to add a CHECK constraint to the table. 如果您希望确保输入的数字仅为0、1或2,则最好在表中添加CHECK约束。

ALTER TABLE
    tableName
ADD CONSTRAINT 
    checkConstain
CHECK (columnName BETWEEN 0 and 2)

You can see an example of this working on SQL Fiddel 您可以看到在SQL Fiddel上进行此操作的示例

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

相关问题 我应该使用什么数据类型在MySQL中为我的应用程序存储time()? - What datatype should I use to store time() in MySQL for my application? 我应该为列使用哪种数据类型? [MySQL的] - What datatype should I use for my column? [MySQL] 我的票证表应使用哪种数据类型? - What datatype should I use for my Tickets table? 我应该使用哪种数据类型在MySQL上存储较长的加密消息? - What datatype should I use to store long encrypted messages on MySQL? 我应该将EAV值放在数据类型表中吗? - Should I place EAV values in a datatype table? 我应该使用什么列类型来存储MySQL中0到1之间的值(比如最多5个小数位)? - What column type should I use to store values between 0 and 1 (say up to 5 decimal places) in MySQL? 我应该使用哪种数据类型映射到 boolean - which datatype i should use for mapping to boolean 我想在公司数据库中存储公制单位 - 我应该使用哪种数据类型? - I want to store metric units in a database - what datatype should I use? 我应该使用什么日期时间数据类型在MySQL中插入March2016之类的日期? - What date time datatype i should use to insert date like March2016 in mysql? 我应该为Java应用程序的MySql服务器数据库中的Aadhaar卡号使用哪种数据类型? - What datatype should I use for Aadhaar card number in MySql server database for JAVA application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM