Right I am actually using SQL Server 2005, however this is a general question about table design that is probably relevant to any database engine so I am going to ask it here since this is the busiest DB forum on the site.
To start with I make no bones about the fact that the area I am really expert in is C/C++ programming on a variety of micro controls and computers.
And the problem is this, what is the best (or correct) way to store a number of discrete states in a database table, for instance
DOWNLOAD_READY
DOWNLOAD_FLAGGE D_TO_START
DOWNLOAD_IN_PRO GRESS
DOWNLOAD_COMPLE TE
The thing is that programmaticall y speaking use of strings is something that we (C/C++ programmers) avoid where possible, they are just not efficient so we like to use numbers. However we also like to avoid magic numbers, that is numbers in the code that appear without there meaning being clear, so in this case I would use a enum
enum download_state {
DOWNLOAD_READY
DOWNLOAD_FLAGGE D_TO_START
DOWNLOAD_IN_PRO GRESS
DOWNLOAD_COMPLE TE
};
In C/C++ this is a way of associating the text that the programmer sees with a number, the compiler automatically assigns numbers (starting at 0) to each item in the enum so the programmer can use the symbols defined to make the code make sense and avoid magic numbers but once compiled the computer is actually using numbers which are efficient for it.
Back to database tables, suppose I wanted to store the same information in a column of a table (and I will want to get able to sort on it or at least pick out entries at the DOWNLOAD_READY state)?
Should that column be text but this seems to have the issue of probably being less efficient and also not really limiting the entries to valid values.
Or should the column be a number, I can probably constrain the number into the right range if I wish, however the table then contains a column of numbers whose meaning is not immediately obvious, i.e. it contains magic numbers.
I am aware that some SQLs actually contain an enum type solving the problem in more or less the same way as the C/C++ programmer would but I believe that is not a standard SQL type.
So there is my question, what is the best/correct standard way to store this type of data in an SQL database?
To start with I make no bones about the fact that the area I am really expert in is C/C++ programming on a variety of micro controls and computers.
And the problem is this, what is the best (or correct) way to store a number of discrete states in a database table, for instance
DOWNLOAD_READY
DOWNLOAD_FLAGGE D_TO_START
DOWNLOAD_IN_PRO GRESS
DOWNLOAD_COMPLE TE
The thing is that programmaticall y speaking use of strings is something that we (C/C++ programmers) avoid where possible, they are just not efficient so we like to use numbers. However we also like to avoid magic numbers, that is numbers in the code that appear without there meaning being clear, so in this case I would use a enum
enum download_state {
DOWNLOAD_READY
DOWNLOAD_FLAGGE D_TO_START
DOWNLOAD_IN_PRO GRESS
DOWNLOAD_COMPLE TE
};
In C/C++ this is a way of associating the text that the programmer sees with a number, the compiler automatically assigns numbers (starting at 0) to each item in the enum so the programmer can use the symbols defined to make the code make sense and avoid magic numbers but once compiled the computer is actually using numbers which are efficient for it.
Back to database tables, suppose I wanted to store the same information in a column of a table (and I will want to get able to sort on it or at least pick out entries at the DOWNLOAD_READY state)?
Should that column be text but this seems to have the issue of probably being less efficient and also not really limiting the entries to valid values.
Or should the column be a number, I can probably constrain the number into the right range if I wish, however the table then contains a column of numbers whose meaning is not immediately obvious, i.e. it contains magic numbers.
I am aware that some SQLs actually contain an enum type solving the problem in more or less the same way as the C/C++ programmer would but I believe that is not a standard SQL type.
So there is my question, what is the best/correct standard way to store this type of data in an SQL database?
Comment