Saturday, March 11, 2017

Strings vs integers for storing data

What would be considered the best practise when it comes to storing datatypes to the database?

For instance, you have a support ticket feature in which a ticket can have the following statuses: open, closed, review.

Would you store it as a string?

+----+----------------+--------+
| id |     ticket     | status |
+----+----------------+--------+
|  1 | example ticket | open   |
|  2 | example ticket | closed |
+----+----------------+--------+

Store the value as an integer, so: 1 => open, 2 => closed and 3 => review or as a string.

+----+--------+
| id | status |
+----+--------+
|  1 | 1      |
|  2 | 2      |
+----+--------+

If you would save it as an integer, would you create an extra table to resolve the name of that status (lets say support_ticket_states table)?

+----+----------------+--------+
| id |     ticket     | status |
+----+----------------+--------+
|  1 | example ticket | 1      |
|  2 | example ticket | 2      |
+----+----------------+--------+

Or would you do that on the client side, for example:

if ($ticket->status == 1) {
    echo 'Open';
}

What would be considered the best option?



via Melvin Koopmans

Advertisement