4

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

EDIT:

Actually, what general advice should I consider when deciding on a data type?

2
  • General advice? Always pick the right one :) Commented Oct 30, 2010 at 8:51
  • You mean the one on the right? :P Commented Oct 30, 2010 at 8:59

2 Answers 2

5

If you want to restrict it to these 3 values, then indeed ENUM might be the best.

If however, there is a possibility that in future, more values might be needed, then TINYINT UNSIGNED is probably a better solution.

Sign up to request clarification or add additional context in comments.

4 Comments

Yup. Even with millions of records, the amount of data saved by using an ENUM over an INT/TINYINT is minuscule. Usually, better use the latter
why would ENUM be better than int or tinyint? Is it because of the restriction of data that can be stored (i.e. better data integrity)? Does it have anything to do with storage requirements? Or speed? Or something else?
enum values are stored as indexes and require 2 bytes (smallint unsigned) hence you can have a maximum of 65,535 enum elements.
Yes, the main advantage of ENUM here, is restricting the set of allowed values (since we don't have column constraints in MySQL yet). Performance-wise, TINYINT will probably be faster.
1

Using enum with fixed set is advisable. If you want to extend then it is 'Alter' basically schema change which should be avoided.

Find better idea about choosing datatype

and get comparison of enum with data types

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.