Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enum storage in Database field

Tags:

database

enums

Is it best to store the enum value or the enum name in a database table field?

For example should I store 'TJLeft' as a string or a it's equivalent value in the database?

Public Enum TextJustification
  TJLeft
  TJCenter
  TJRight
End Enum

I'm currently leaning towards the name as some could come along later and explicitly assign a different value.

Edit -

Some of the enums are under my control but some are from third parties.

like image 320
user79755 Avatar asked Jul 06 '26 04:07

user79755


1 Answers

Another reason to store the numeric value is if you're using the [Flags] attribute on your enumeration in cases where you may want to allow for multiple enumeration values. Say, for example you want to let someone pick what days of the week that they're available for something...

[Flags] 
public enum WeekDays     
{
   Monday=1,
   Tuesday=2,
   Wednesday=4,
   Thursday=8,
   Friday=16
}

In this case, you can store the numeric value in the db for any combination of the values (for example, 3 == Monday and Tuesday)

like image 77
Ken Avatar answered Jul 08 '26 05:07

Ken