[英]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.