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?
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 :
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.