ENUM
An ENUM, short for enumeration, is a data type in DuckDB and many other database systems that allows you to define a fixed set of named values.
An ENUM, short for enumeration, is a data type in DuckDB and many other database systems that allows you to define a fixed set of named values. It's particularly useful when you have a column that should only contain a limited number of distinct values. ENUMs are more memory-efficient than storing strings and can improve query performance.
In DuckDB, you can create an ENUM type using the following syntax:
Copy code
CREATE TYPE mood AS ENUM ('happy', 'neutral', 'sad');
Once defined, you can use this ENUM type in table definitions:
Copy code
CREATE TABLE person (
id INTEGER,
name VARCHAR,
current_mood mood
);
You can then insert values using the defined ENUM values:
Copy code
INSERT INTO person VALUES (1, 'Alice', 'happy');
INSERT INTO person VALUES (2, 'Bob', 'sad');
ENUMs provide type safety, ensuring that only predefined values can be inserted into the column. They also allow for efficient sorting based on the order of definition. However, it's important to note that adding new values to an existing ENUM type can be challenging, so careful planning is necessary when using this data type in your schema design.
Related terms
In the context of databases and programming, a data type defines the nature of data that can be stored in a specific field or variable.
MAP type →MAP is a nested SQL data type storing an ordered collection of unique key-value pairs, useful for representing dynamic or sparse attributes within a single column.
CHECK constraint →A CHECK constraint is a rule attached to a table column (or the table as a whole) that requires every row to satisfy a given boolean expression.
Slowly changing dimensions (SCD) →Slowly changing dimensions (SCD) are techniques for handling changes to dimension attributes over time — such as a customer moving to a new region — while preserving or overwriting historical values as needed.
