Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use enum or query a table in my database?

In my database I have tables that define types for example

Table: Publication Types


ID | Type
----------
1  | Article
2  | Abstract
3  | Book
....

Which is related through the ID key to a publication tables which has the field TypeID.

I then create a PublicationTable data table my .NET application which I want to filter based on the publication type. For example the following function gives me the number of publications for a specific author and publication type.


    Public Function countPublications(ByVal authorID As Integer, _ 
                                      ByVal publicationType As Integer) As Integer

        Dim authPubs As New PublicationsDataSet.tblPublicationsDataTable
        authPubs = Me.getAuthorsPublications(authorID)

        Dim dv As New DataView(authPubs)
        dv.RowFilter = "status='published' AND type='" + _ 
                       publicationType.ToString + "'"

        Return dv.Count

    End Function

To call this function to get a count of articles by an author of a specific type, I could

  1. call the function with two integers

    countPublications(authorID, 1)

  2. setup an enum so that I can write

    countPublications(authorID, pubType.Article)

    or

  3. somehow use the publication type table to filter the publication data set but I haven't got my head around how to do this.

What other approaches should I consider.

Thanks

like image 774
Azim J Avatar asked Oct 31 '08 00:10

Azim J


People also ask

Should we use enum in database?

Enum types are more convenient for coding. For infrequent releases, or if you often have new/deleted/changed values, use a database table. For static sets of values, or if you release code all the time, use an enum.

When should an enum be used?

You should use enum types any time you need to represent a fixed set of constants. That includes natural enum types such as the planets in our solar system and data sets where you know all possible values at compile time—for example, the choices on a menu, command line flags, and so on.

What is the correct uses of enum in MySQL?

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 is enum stored in database?

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. These numbers are associated with the order in which you define values in the enum.


1 Answers

if publication types are essentially static, enums are fine

there is arguably little difference between embedding

inner join lookuptable lt on lt.id = (int)myenum.sometype 

in a query and adding

inner join lookuptable lt on lt.name = "somehardcodeddescription"

they're both embedded constants, the former just has a well-defined type behind it

alternately you could use

inner join lookuptable lt on lt.name = myenum.sometype.ToString

i prefer the former

if, on the other hand, new lookup types may be added after the code is deployed, then an enum will quickly become outdated;

but if there is core set of static enum values that the code needs and the rest don't matter then the former solution is still fine

as usual, "it depends" ;-)

like image 188
Steven A. Lowe Avatar answered Oct 21 '22 07:10

Steven A. Lowe