What is the best way to represent constants (enumerations) in a database (INT vs VARCHAR)?

what is the best solution in terms of performance and reading / good coding style (Java enumeration) (a fixed set of constants) at the database level with respect to an integer (or any data type in general) vs string representation.

Warning. There are several database systems that support Enums directly, but for this you will need to encode the Database Enum-Definition in synchronization with the business-level implementation. In addition, this data type may not be available for all database systems, and may also differ in the syntax => I am looking for a lightweight solution that is easy to manage and available for all database systems. (So ​​my question is only addressed to the Number vs String view.)

The numerical representation of constants seems to me very efficient for storage (for example, consumes only two bytes as a whole) and, most likely, very fast in terms of indexing, but difficult to read ("0" versus "1"), etc.). .

The String view is more readable (persistence is β€œon” and β€œoff” compared to β€œ0” and β€œ1”), but it consumes a lot of memory and is most likely also slower with respect to indexing.

My questions, have I missed some important aspects? What do you suggest to use to represent the enumeration at the database level.

Many thanks!

+5
source share
2 answers

In most cases, I prefer to use a short alphanumeric code and then have a lookup table with extended text. If necessary, I build an enumeration table in the program dynamically from the database table.

, , , , , , - , , . , , , "SA", "RE", "SV" "LY", . . , . , , , , . . , , , , . , . , : - , - .

. , "Layaway". , , , - , , "Layaway sale" , "Lay-away". , , , . , , , - .

, . , : "if whatcode =" A " whatcode =" C " whatcode =" X ", ...". , - , . : ", , " - , , "tax_related" true false , . , - , if/ , , - . , , - , , , , , , , -, .

, , - , , , - , . true/false // /. ( , , , , , , "" ..)

, . - , - , .

+3

, . ordinal() , , , .

, , , , , , String.

+1

All Articles