35

I have a number of enums in my application which are used as property type in some classes.

What is the best way to store these values in database, as String or Int?

FYI, I will also be mapping these attribute types using fluent Nhibernate.

Sample code:

public enum ReportOutputFormat
{
    DOCX,
    PDF,
    HTML
}

public enum ReportOutputMethod
{
    Save,
    Email,
    SaveAndEmail
}

public class ReportRequest
{
    public Int32 TemplateId
    {
        get { return templateId; }
        set { templateId = value; }
    }
    public ReportOutputFormat OutputFormat
    {
        get { return outputFormat; }
        set { outputFormat = value; }
    }

    public ReportOutputMethod OutputMethod
    {
        get { return outputMethod; }
        set { outputMethod = value; }
    }
}

3 Answers 3

49

The implementation is easy in both cases, and performance differences should be minor.

Therefore, go for the meaning : the Strings are more meaningful than numbers, so use the String.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 I have worked with millions of records. Only then I realised meaning was a lot more important than "compactness".
Sometimes performance is more important, lest the meaning be lost. So pick your poison and determine which has more value.
IMHO if the enum goes into the database you have to force a number with the enumeration declaration. I've seen it too many times that typos are fixed, names are changed, so querying on text makes it much harder. But you need to explicitly number the enum, otherwise you might get into trouble when someone inserts or deletes an enum (and IMHO if it is a value that was stored in the database you should still leave it but commented out. Storing enums as string in the database is a no no for me (as you might also use another development language/environment later)
29

Both have advantages. If you store them by their Integer value, then you must be careful editing your enumeration later on in your project. If somehow the integer values for the enumeration elements get redefined, all your data will be corrupted. But it will be the fastest/smallest datatype to use.

If you use the string representation you'll not have the redefined value problem for as long as you don't change the name of the elements in the enumeration. However strings take up more space in your database and are probably a bit more costly in use.

At the end, there's no definitive winner I guess.

Edit

Using the Integer value might be the best way. You can overcome the 'redefined value' problem for the enumeration elements, by setting the value for each element yourself. Indeed a good suggestion from Kevin.

5 Comments

I can't believe you'd even consider half-recommending an approach that could easily result in data corruption! Just out of an imagined concern for compactness? If the compactness concern is that strong, add an int field to the enum! NEVER rely on the ordinal. Read Effective Java.
I really don't see where I'm recommending anything to be honest. I'm only stating advantages and disadvantages. Though you have a point, not wanting to rely on the ordinal value, you might want to turn that 'fire' in your comment down a notch ;)
@KevinBourrillion What does "add an int field to the enum" mean?
@John he means assigning specific values to elements of the enumeration. See learn.microsoft.com/en-us/dotnet/csharp/programming-guide/… for examples. That way, you leave nothing to chance.
I'm with @pyrocumulus, if an enum is stored in the database, make sure it got its integer value predefined in the enumeration, if its only for in-memory use, then it doesn't matter. Storing the stringvalue gives you problems when you had a typo or something like that.
1

I can think of only 1 reason to go for Int is when it is a high throughput system, where you are dealing with hundred thousand records daily which is rare is most cases and you want to save some memory and do faster comparion. I'd say to go for Strings. Strings are more readable and easier to deal with IMO.

Cons of INT

  • not readable
  • requires mental map of enums to int, suppose you working with 15 states of enum you need to know what 13 means when you debug/code
  • requires parsing to string if it is exposed via API

Cons of String

  • takes more space than INT

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.