Reputation: 1084
I have a table that holds rows representing votes. The fields are an id for the vote itself, an id for the thing that the vote is for or against and a direction field that holds "up" or down". Would it be better to use 0 and 1 or 1 and 2 for "up" and "down"? Is it always better or worse to use string or integers in this way?
I have heard that integers can make certain table operations faster, but using integers in a context like this where the real thing that you're trying to represent is a direction seems to introduce unwanted complexity when it comes to coding. If I go with integers, I'll need to remember which number represents which direction and my code just won't be self-documenting.
Any thought?
Upvotes: 0
Views: 1060
Reputation: 72636
In your case storing and "UP" and "DOWN" as an integer wouldn't be a bad idea, you can store the enumeration on a specific table that holds the possibles vote status.
You can create a table called votes with an integer ID and a String vote like the following :
votes
+------+------+
| id | vote |
+------+------+
| 0 | UP |
| 1 | DOWN |
And then reference the ID of the vote when needed with a field called for example : vote_id
; i would prefer this approach rather than an enumerator because it is more flexible an scalable, for example if some day for some reason you want to add another type of vote you can do it easily and without touching your application code, but the implementation will be simple as create a new record in the votes table.
Upvotes: 0
Reputation: 1239
Well I would say it should be decided on case to case basis.
Enums can come in very handy when you are very very sure that these are the few values for an attribute.
Yes ofcourse you will be putting some extra code in php in the case you suggested for referencing the actual value , but if you are sure that there wont be a right or left direction to be added in your data list then go with ENUM.
Upvotes: 1