views:

252

answers:

2

Hi,

Would ENUM('1','2','3','4','5') be a sensible datatype for a product rating which must be between 1 and 5?

Thanks!

+5  A: 

Yes, that would be an appropriate data type since it enforces your domain.

If you are going to add (or do any other mathematical operation) them together, however, a numeric data type might be better.

Daniel A. White
Indeed, or if you intended to average them. TINYINT is probably more appropriate, just make sure you constrain the values in your application.
Jordan
Danny King
@Jordan, my thoughts exactly. ENUM really shines if you have a fixed list of texts, and there is no numerical connotation
Roland Bouman
heh thanks Jordan, you beat me to it
Danny King
@Danny King - yes, definitely. The stars is a matter of presentation. but as far as data is concerned you are quite literally 'counting starts'. So an integer type is completely appropriate.
Roland Bouman
+5  A: 

I suggest using

TINYINT UNSIGNED NOT NULL

or, for better ANSI/SQL compatibility, use:

SMALLINT NOT NULL

With an integer type, it is much easier to do calculations. ENUM is not bad, but there is a potential to mess up because it's kind of a dual string/int type (beneath the covers, it's an int, but from the outside, it's a string). And indeed, suppose you do feel the need to go to 3 stars, or 10 stars or so, the migration will be much less painful.

Roland Bouman