What is the best way to store enumerations?
For example, I have an entity Person that has a property Sex. The first option that I can think of is a table that stores the enumeration values and names (1 is for Male and 2 is for Female for example) and then in the Person table to store the int values. The second option is to have an enumeration in the code with a corresponding int values and store in the Person table the int values. The last option is to have again enumeration in the code and store the string value. For example, Person with sex ‘Male’.
Which approach is better and when?
Enums in code are cool and handy, but you can break your database-integrity when changing the enum.
So in general: use an enum in the database when relational integrity is needed. We use an explicit mapped enum in code to deal with this; and enforce integrity in database. Like:
When to use an enum only in code?
F.e. Bitmasking! So you can store a bunch of settings that only make sense in your application efficiently in your database. Integrity doesn’t really care, because the database doesn’t have anything to do with this.