SQL Server Developer Center > SQL Server Forums > SQL Server Compact > SQL server 2008: what is the best defined type in SQL database table for Enum type?
Ask a questionAsk a question
 

AnswerSQL server 2008: what is the best defined type in SQL database table for Enum type?

  • Thursday, October 29, 2009 8:35 PMJJChen Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    We have general questions regarding SQL database table type definition for Enum type:
    1) Should we use enum value (0, 1, 2, ...) or enum strings (shape1, shape2, ...) in Enum type column?
    2) if the first answer is enum value, should we use smallint type or tinyint?
    thx!
    • Edited byJJChen Friday, October 30, 2009 12:59 AM
    •  

Answers

  • Monday, November 02, 2009 7:02 AMChunSong Feng -MSFTMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi,

    I think it is a good option using key-value pair to store enum type data. If tingint is not enough to store number, can you use int/bigint, if the value is not stand for a number, we can use char/varchar to store the value.

    Hope this helps.


    ChunSong Feng
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked As Answer byJJChen Wednesday, November 04, 2009 4:45 PM
    •  

All Replies

  • Thursday, October 29, 2009 11:45 PMPaul Svirin Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Unfortunately there is no ENUM in MSSQL. Use Rule or a Check Constraint instead.


    ---
    Paul Svirin
    StarWind Software developer ( http://www.starwindsoftware.com )
  • Friday, October 30, 2009 12:58 AMJJChen Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Paul,
    thanks for replying.

    We know SQL server does not support enum type yet.
    Our question is what is the best practise in general to define enum type in SQL table.
    Should we use enum value or enum string?

    If using enum value, actually enum value can go from small integer range to large integer range depending on how you define
    enum value

    enum ShapeEnum
    {
        Shape1 = 0,
        Shape2 = 1,
        ...
        Shape5= 1000
    }

    if we define Shape5 value as 1000, then use tinyint (8 bits) is not enough to represent it.

  • Monday, November 02, 2009 7:02 AMChunSong Feng -MSFTMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi,

    I think it is a good option using key-value pair to store enum type data. If tingint is not enough to store number, can you use int/bigint, if the value is not stand for a number, we can use char/varchar to store the value.

    Hope this helps.


    ChunSong Feng
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked As Answer byJJChen Wednesday, November 04, 2009 4:45 PM
    •