Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing enum values in database

Tags:

FYI: I explicitly mean SQL Server 2000-8 and C#. So DBMSs with enum support like MySql is not the subject of my question.

I know this question has been asked multiple times in SO. But still, I see in answers that different approaches are taken to store enum values in db.

  1. Save enum as int in db and extract the enum value (or enum description attribute using reflection) in code:
    this is the approach I usually use. The problem is when I try to query from database in SSMS, the retrieved data is hard to understand.

  2. Save enum as string (varchar) in db and cast back to int in code.
    Actually, this might the best solution. But (don't laugh!) it doesn't feel right. I'm not sure about the cons. (Except more space in db which is usually acceptable) So anything else against this approach?

  3. Have a separate table in db which is synchronized with code's enum definition and make a foreign key relationship between your main table and the enum table.
    The problem is when another enum value should be added later, Both code and db need to get updated. Also, there might be typos which can be a pain!

So in general when we can accept the overhead on db in 2nd solution, What would be the best way to store enum values in db? Is there a general definite design pattern rule about this?
Thanks.

like image 943
Kamyar Avatar asked May 25 '11 16:05

Kamyar


People also ask

Can you store an enum in database?

First of all, in order to save enum values in a relational database using JPA, you don't have to do anything. By default, when an enum is a part of an entity, JPA maps its values into numbers using the ordinal() method. What it means is that without customizations JPA stores enum value as numbers.

How is enum stored in SQL?

Save enum as int in db and extract the enum value (or enum description attribute using reflection) in code: this is the approach I usually use. The problem is when I try to query from database in SSMS, the retrieved data is hard to understand. Save enum as string (varchar) in db and cast back to int in code.

What is enum value in database?

An ENUM is a string object with a value chosen from a list of permitted values that are enumerated explicitly in the column specification at table creation time.

How are enum values stored?

A standard enum is usually implemented as an int32, the compiler will handle your enum as a synonym of int32 . Once a list of values is created for a enumeration those values are stored as literals against their display name(access name given at the time of declaration of enum).


1 Answers

There is no definite design rule (that I know of), but I prefer approach #1.

  1. Is the approach I prefer. It's simple, and enums are usually compact enough that I start remembers what the numbers mean.
  2. It's more readable, but can get in the way of refactoring or renaming your enumeration values when you want to. You lose some freedom of your code. All of the sudden you need to get a DBA involved (depending on where/how you work) just to change an enumeration value, or suffer with it. Parsing an enum has some performance impact as well since things like Locale come into play, but probably negligible.
  3. What problem does that solve? You still have unreadable numbers in a table somewhere, unless you want to add the overhead of a join. But sometimes, this is the correct answer too depending on how the data is used.

EDIT: Chris in the comments had a good point: If you do go down the numeric approach, you should explicitly assign values so you can re-order them as well. For example:

public enum Foo {      Bar = 1,      Baz = 2,      Cat = 9,      //Etc... } 
like image 180
vcsjones Avatar answered Sep 18 '22 12:09

vcsjones