Ira SQL ProIra SQL Pro

ENUM Types

intermediatepostgresql
2 min read

What are ENUM Types?

An ENUM (enumerated type) defines a column with a fixed set of allowed values. It provides type safety and storage efficiency compared to plain VARCHAR columns.

Syntax

CREATE TYPE mood AS ENUM ('happy', 'sad', 'neutral');

CREATE TABLE journals (
  id SERIAL PRIMARY KEY,
  entry_date DATE,
  mood mood
);

Adding a value:

ALTER TYPE mood ADD VALUE 'excited' AFTER 'happy';

When to Use

  • Status columns with a known, small set of values (active, inactive, pending)
  • Priority levels (low, medium, high, critical)
  • Any column where only specific string values are valid

Key Points

  1. Ordered — ENUM values have an order based on their definition sequence.
  2. Type Safety — Inserting an undefined value raises an error.
  3. Storage — ENUMs use 4 bytes internally, which is efficient.
  4. Adding Values — Use ALTER TYPE to add new values. You cannot easily remove or rename values.
  5. Comparison — ENUMs compare by position, not alphabetically. 'happy' < 'sad' if happy was defined first.

Guided Practice

Solve the challenge below. Use hints when stuck and check your answer for instant feedback.

Practice challengeGuided learning mode

ENUM Types Challenge

Write a query that solve this task: define a grade ENUM and use it in a table.

Expected result

A letter_grade type and a student_grades table that only accepts A, B, C, D, or F.

Hidden checks

  • Returned rows and values
  • Output columns and result shape
  • Final database state after the query runs

Lesson guidance

What are ENUM Types?

Initializing database...Each run starts from fresh sample data.

More Examples

Insert and query ENUM values

Insert data and filter by ENUM value.

Initializing database...Each run starts from fresh sample data.

Frequently Asked Questions

Can I remove a value from an ENUM?
Not directly. You would need to create a new type, migrate the data, and drop the old type. ALTER TYPE only supports adding values.
Should I use ENUM or CHECK constraint?
ENUM is better for a fixed set unlikely to change. CHECK constraints are easier to modify. For frequently changing values, use a reference table.

Related Topics