enums...

topic posted Thu, July 29, 2004 - 10:37 AM by  Josh
Share/Save/Bookmark
Advertisement
Can somebody explain to me why SQL Server doesn't suppost enums? It seems like a perfectly acceptable thing to have, and they must have left it out for a reason... what is that reason?
posted by:
Josh
Advertisement
Advertisement
  • Re: enums...

    Fri, July 30, 2004 - 4:21 PM
    Ummm... because it's a database, and anytime you'd want to use an enum, you'd be more likely to look it up in a table anyway?
    • Re: enums...

      Fri, July 30, 2004 - 11:19 PM
      Ummm... I wouldn't be more likely to use a table anyway, Garrett. Esp if it were only for a couple values. enums create less programming overhead. There is no looking up another table to see if you can put a value in the field, and there's no looking up that table again to display the value.

      Would you also say that people shouldn't store images in database because people are "more likely" not to?
  • Re: enums...

    Tue, March 29, 2005 - 9:02 AM
    Probably because it's not in the ANSI-SQL standard. But that doesn't really answer your question, because now you need to know why it's not in the ANSI-SQL standard. Even if that's not a rhetorical question, I doubt anyone could give you a straight answer except for the designeers of the ANSI-SQL standard.

    Really, instead of using enums, why don't you just use a VARCHAR() field?

    I use a VARCHAR(4) field for a lot of the places I would normally use enums in other programming languages.

    I use a lookup table like so:

    CREATE TABLE tblStatusCodes
    (
    Code VARCHAR(4)
    Desc VARCHAR(30)
    CONSTRAINT PK_tblStatusCodes PRIMARY KEY CLUSTERED (StatusCode)
    )

    Now, in the table with actual user data in it is like so:
    CREATE TABLE tblAccounts
    (
    ...
    StatusCode VARCHAR(4)
    ...
    )

    The tblAccounts.StatusCode column is a foreign key to tblStatusCodes

    If you set up the referential integrity rules, this prevents spelling errors of a code when data is being inserted and updated. Also the tblStatusCodes table can be joined in for access to the Desc column whenever you need a human readable translation of whatever the code represents, say in the your app's forms or reports.

Recent topics in "SQL Server"

Topic Author Replies Last Post
New to the group with a question Martinus 0 September 26, 2007
how to make an Update trigger capture the foreign key text value AJ 0 June 19, 2007
transactions and triggers Terry 2 December 14, 2006
ASP.NET vikram 0 October 6, 2006