I have a number of columns that only need to store a few values (e.g. 0, 1, 2, 3). What datatype should I pick for such a case? I feel like I should pick something like ENUM('0', '1', '2'). Would int be better (despite it being less restrictive)? Should I consider something else (like tinyint)?
我有许多列只需要存储一些值(例如0,1,2,3)。我应该为这种情况选择什么数据类型?我觉得我应该选择像ENUM('0','1','2')这样的东西。 int会更好(尽管限制较少)?我应该考虑别的东西(比如tinyint)吗?
EDIT:
Actually, what general advice should I consider when deciding on a data type?
实际上,在决定数据类型时,我应该考虑哪些一般建议?
2 个解决方案
#1
5
If you want to restrict it to these 3 values, then indeed ENUM might be the best.
如果你想将它限制在这3个值,那么ENUM可能是最好的。
If however, there is a possibility that in future, more values might be needed, then TINYINT UNSIGNED is probably a better solution.
但是,如果将来可能需要更多的值,那么TINYINT UNSIGNED可能是更好的解决方案。
#2
1
Using enum with fixed set is advisable. If you want to extend then it is 'Alter' basically schema change which should be avoided.
建议使用带固定集的枚举。如果你想扩展那么它是'Alter'基本上应该避免的模式改变。
Find better idea about choosing datatype
找到更好的选择数据类型的想法
and get comparison of enum with data types
并获得枚举与数据类型的比较
#1
5
If you want to restrict it to these 3 values, then indeed ENUM might be the best.
如果你想将它限制在这3个值,那么ENUM可能是最好的。
If however, there is a possibility that in future, more values might be needed, then TINYINT UNSIGNED is probably a better solution.
但是,如果将来可能需要更多的值,那么TINYINT UNSIGNED可能是更好的解决方案。
#2
1
Using enum with fixed set is advisable. If you want to extend then it is 'Alter' basically schema change which should be avoided.
建议使用带固定集的枚举。如果你想扩展那么它是'Alter'基本上应该避免的模式改变。
Find better idea about choosing datatype
找到更好的选择数据类型的想法
and get comparison of enum with data types
并获得枚举与数据类型的比较