Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type to use for "Status" columns in a sql table

I have a (dummy) table structure as follows:

 ticket   id: int(11) PK   name: varchar(255)   status: ????????? 

The question is, what data type should I use for status? Here are my options, as I see them:

  1. varchar representing the status - BAD because there's no integrity
  2. enum representing the status - BAD because to change the value, I'd have to alter the table, and then any code with dropdowns for the values, etc etc etc
  3. int FK to a status table - GOOD because it's dynamic, BAD because it's harder to inspect by sight (which may be useful)
  4. varchar FK to a status table - GOOD because it's dynamic, and visible on inspection. BAD because the keys are meaningful, which is generally frowned upon. Interestingly, in this case it's entirely possible for the status table to have just 1 column, making it a glorified enum

Have I got an accurate read of the situation? Is having a meaningful key really that bad? Because while it does give me goosebumps, I don't have any reason for it doing so...

Update: For option 4, the proposed structure would be status: char(4) FK, to a status table. So,

OPEN => "Open"

CLOS => "Closed"

"PEND" => "Pending Authorization"

"PROG" => "In Progress

What's the disadvantage in this case ? The only benefit I can see of using int over char in this case is slight performance.

like image 473
XwipeoutX Avatar asked Aug 26 '11 13:08

XwipeoutX


People also ask

What is the data type for status in SQL?

Solution 1. Sure ... "status" being either active or inactive (two values of potential status value ... 0 = inactive, 1 = active (to be typical)).

What are the data types of columns in SQL?

The data type of a column defines what value the column can hold: integer, character, money, date and time, binary, and so on.

How do I select a column type in SQL?

You can get the MySQL table columns data type with the help of “information_schema. columns”. SELECT DATA_TYPE from INFORMATION_SCHEMA. COLUMNS where table_schema = 'yourDatabaseName' and table_name = 'yourTableName'.


1 Answers

I would go with number 4, but I'd use a char(x) column. If you're worried about performance, a char(4) takes up as much space (and, or so one would think, disk i/o, bandwidth, and processing time) as an int, which also takes 4 bytes to store. If you're really worried about performance, make it a char(2) or even char(1).

Don't think of it as "meaningful data", think of it as an abbreviation of the natural key. Yes, the data has meaning, but as you've noticed that can be a good thing when working with the data--it means you don't always have to join (even if to a trivially small table) to extract meaning from the database. And of course the foreign key constraint ensures that the data is valid, since it must be in the lookup table. (This can be done with CHECK constraints as well, but Lookup tables are generally easier to manage and maintain over time.)

The downside is that you can get caught up with trying to find meaning. char(1) has a strong appeal, but if you get to ten or more values, it can get hard to come up with good meaningful values. Less of a problem with char(4), but still a possible issue. Another downside: if the data is likely to change, then yes, your meaningful data ("PEND" = "Pending Authorization") can lose its meaning ("PEND" = "Forward to home office for initial approval"). That's a poor example; if codes like that do change, you're probably much better off refactoring your system to reflect the change in business rules. I guess my point should be, if it's a user-entered lookup value, surrogate keys (integers) will be your friend, but if they're internally defined and maintained you should definitely consider more human-friendly values. That, or you'll need post-em notes on your monitor to remind you what the heck Status = 31 is supposed to mean. (I've got three on mine, and the stickum wears out every few months. Talk about cost to maintain...)

like image 52
Philip Kelley Avatar answered Oct 05 '22 19:10

Philip Kelley