Previous Next JavaScript must be enabled to correctly display this content

## Using Enums

Enumerated types are declared using the `ENUM()` statement. You must declare the acceptable enumeration values when you use this statement.

To define a simple two-field table where the primary key is a UID and the second field contains an enum, you use the following DDL statement:

    CREATE TABLE myTable (
        uid INTEGER,
        myEnum ENUM (Apple,Pears,Oranges),
        PRIMARY KEY (uid)
    )

`DEFAULT` and `NOT NULL` constraints are supported for enumerated fields. See DEFAULT for more information.

To write the enum, use `Row.putEnum()`. If the enumeration value that you use with this method does not match a value defined on the `-enum-values` parameter during table definition, an `IllegalArgumentException` is thrown.

    TableAPI tableH = kvstore.getTableAPI();

    Table myTable = tableH.getTable("myTable");

    Row row = myTable.createRow();
    row.put("uid", 12345);

    row.putEnum("myEnum", "Pears");

    tableH.put(row, null, null);

To read the enum, use `Row.get().asEnum()`. This returns a `EnumValue` class instance. You can then use `EnumValue.get()` to retrieve the stored enum value's name as a string. Alternatively, you can use `EnumValue.getIndex()` to retrieve the stored value's index position.

For example:

    TableAPI tableH = kvstore.getTableAPI();

    Table myTable = tableH.getTable("myTable");

    /* Create a primary key for user id 12345 and get a row */
    PrimaryKey key = myTable.createPrimaryKey();
    key.put("uid", 12345);
    Row row = tableH.get(key, null);

    EnumValue ev = row.get("testEnum").asEnum();
    System.out.println("enum as string: " +
                    ev.get()); // returns "Pears"
    System.out.println("enum index: " +
                    ev.getIndex()); // returns '1'