Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Creating column of type enum

Creating column of type enum

Scheduled Pinned Locked Moved Database
databasesql-serversysadmintutorial
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    columbos14927
    wrote on last edited by
    #1

    Hello, I'm using VS2010 to create Data Base in SQL server. I want to create column that will be of type enum. For example the enum:

    public enum Equipment
    {
    Tank,
    Suit,
    Fins,
    Mask,
    Snorkel
    }

    And the column will be called DiveKit, the values in that column can be one of the Equipment enumeration members. Thanks

    L P J 3 Replies Last reply
    0
    • C columbos14927

      Hello, I'm using VS2010 to create Data Base in SQL server. I want to create column that will be of type enum. For example the enum:

      public enum Equipment
      {
      Tank,
      Suit,
      Fins,
      Mask,
      Snorkel
      }

      And the column will be called DiveKit, the values in that column can be one of the Equipment enumeration members. Thanks

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Sql Server doesn't have an Enum datatype. You can store it's value-representation (int) or it's string-representation (varchar). It wouldn't do range-checking by default. Simple example below on having range-checking by creating a table for the enum and referencing it;

      CREATE TABLE enumEquipment([Label] VARCHAR(10) PRIMARY KEY)
      INSERT INTO enumEquipment (Label) VALUES ('Tank'), ('Suit'), ('Fins'), ('Mask'), ('Snorkel');

      CREATE TABLE someTableUsingTheEnum
      (
      Id INT IDENTITY(1,1) PRIMARY KEY,
      EnumValue VARCHAR(10) REFERENCES enumEquipment (Label)
      )
      INSERT INTO someTableUsingTheEnum (EnumValue) VALUES ('Tank') -- works
      INSERT INTO someTableUsingTheEnum (EnumValue) VALUES ('Micky') -- fails

      SELECT * FROM someTableUsingTheEnum -- shows 1 record, with 'Tank'

      DROP TABLE someTableUsingTheEnum
      DROP TABLE enumEquipment

      It'd be better, performance-wise, to use the integer-value of the enum. Just cast it to an int, and store it in an int in Sql Server.

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

      1 Reply Last reply
      0
      • C columbos14927

        Hello, I'm using VS2010 to create Data Base in SQL server. I want to create column that will be of type enum. For example the enum:

        public enum Equipment
        {
        Tank,
        Suit,
        Fins,
        Mask,
        Snorkel
        }

        And the column will be called DiveKit, the values in that column can be one of the Equipment enumeration members. Thanks

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Perhaps my GenOmatic[^] article will provide some insight.

        1 Reply Last reply
        0
        • C columbos14927

          Hello, I'm using VS2010 to create Data Base in SQL server. I want to create column that will be of type enum. For example the enum:

          public enum Equipment
          {
          Tank,
          Suit,
          Fins,
          Mask,
          Snorkel
          }

          And the column will be called DiveKit, the values in that column can be one of the Equipment enumeration members. Thanks

          J Offline
          J Offline
          Jorgen Andersson
          wrote on last edited by
          #4

          You can use a check constraint on the table if you just want the column limited to a list of values.

          ALTER TABLE EQIPMENT
          ADD CONSTRAINT EQUIPMENTTYPE_CHK CHECK (DIVEKIT IN ('Tank', 'Suit', 'Fins', 'Mask', 'Snorkel')

          "The ones who care enough to do it right care too much to compromise." Matthew Faithfull

          P 1 Reply Last reply
          0
          • J Jorgen Andersson

            You can use a check constraint on the table if you just want the column limited to a list of values.

            ALTER TABLE EQIPMENT
            ADD CONSTRAINT EQUIPMENTTYPE_CHK CHECK (DIVEKIT IN ('Tank', 'Suit', 'Fins', 'Mask', 'Snorkel')

            "The ones who care enough to do it right care too much to compromise." Matthew Faithfull

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            I'd be concerned about the need to easily add new members. And agnosticism. Plus, having a table allows the addition of more information related to the items.

            J 1 Reply Last reply
            0
            • P PIEBALDconsult

              I'd be concerned about the need to easily add new members. And agnosticism. Plus, having a table allows the addition of more information related to the items.

              J Offline
              J Offline
              Jorgen Andersson
              wrote on last edited by
              #6

              All true, and if one wanted to fully emulate an enum one would also need an extra column for the integer value. But you can't add new members to an enumeration without recompiling either. Anyway, I got the impression that the OP only wanted to limit the items in a column, and if your objections is of no concern for the OPs intended usage, this is a simple way to achieve a limited list of items.

              "The ones who care enough to do it right care too much to compromise." Matthew Faithfull

              P 1 Reply Last reply
              0
              • J Jorgen Andersson

                All true, and if one wanted to fully emulate an enum one would also need an extra column for the integer value. But you can't add new members to an enumeration without recompiling either. Anyway, I got the impression that the OP only wanted to limit the items in a column, and if your objections is of no concern for the OPs intended usage, this is a simple way to achieve a limited list of items.

                "The ones who care enough to do it right care too much to compromise." Matthew Faithfull

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                Jörgen Andersson wrote:

                the OP only wanted to limit the items in a column

                Which is likely true, but I hope to open his eyes to a broader solution that he may not have considered yet. Reducing record size may not be as important with today's databases and storage availability, but it's still better to store a foreign key rather than a string, particularly if you may need to select records with given values. Having a table with the keys and values also allows for easier loading of a list from which the user can select items.

                J 1 Reply Last reply
                0
                • P PIEBALDconsult

                  Jörgen Andersson wrote:

                  the OP only wanted to limit the items in a column

                  Which is likely true, but I hope to open his eyes to a broader solution that he may not have considered yet. Reducing record size may not be as important with today's databases and storage availability, but it's still better to store a foreign key rather than a string, particularly if you may need to select records with given values. Having a table with the keys and values also allows for easier loading of a list from which the user can select items.

                  J Offline
                  J Offline
                  Jorgen Andersson
                  wrote on last edited by
                  #8

                  Quite right. I'm usually pushing normalization myself.

                  "The ones who care enough to do it right care too much to compromise." Matthew Faithfull

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups