What if your database could stop errors before they happen by knowing exactly what values are allowed?
Why ENUM types in PostgreSQL? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you are managing a list of user roles like 'admin', 'editor', and 'viewer' in a database using plain text fields.
Every time you add or check a role, you have to remember the exact spelling and case.
This manual approach is slow and error-prone because typos can sneak in, causing bugs.
It's hard to enforce that only valid roles are used, and searching or filtering becomes unreliable.
ENUM types let you define a fixed set of allowed values for a column.
This means the database itself ensures only valid roles are stored, preventing mistakes and making queries faster and safer.
CREATE TABLE users (role TEXT);
-- Insert roles as text, no restrictionCREATE TYPE user_role AS ENUM ('admin', 'editor', 'viewer'); CREATE TABLE users (role user_role); -- Only allowed roles can be inserted
With ENUM types, your database enforces valid categories, making your data cleaner and your code simpler.
A company uses ENUM to store order statuses like 'pending', 'shipped', and 'delivered'. This prevents invalid statuses and helps track orders reliably.
Manual text fields for fixed categories cause errors and confusion.
ENUM types restrict values to a predefined list enforced by the database.
This leads to safer data and easier querying.
Practice
ENUM types in PostgreSQL?Solution
Step 1: Understand ENUM type purpose
ENUM types define a list of allowed values for a column, ensuring data consistency.Step 2: Compare with other options
Other options describe unrelated features like text storage, temporary tables, or indexing.Final Answer:
To restrict a column to a fixed set of allowed values -> Option AQuick Check:
ENUM = fixed allowed values [OK]
- Thinking ENUM stores large text data
- Confusing ENUM with temporary tables
- Assuming ENUM improves indexing speed
mood with values 'happy', 'sad', and 'neutral'?Solution
Step 1: Recall ENUM creation syntax
The correct syntax isCREATE TYPE name AS ENUM (values);with values in parentheses and single quotes.Step 2: Check each option
CREATE TYPE mood AS ENUM ('happy', 'sad', 'neutral'); matches the correct syntax exactly. Others have wrong keywords or brackets.Final Answer:
CREATE TYPE mood AS ENUM ('happy', 'sad', 'neutral'); -> Option DQuick Check:
CREATE TYPE ... AS ENUM (values) [OK]
- Using CREATE ENUM instead of CREATE TYPE
- Using square brackets instead of parentheses
- Omitting AS keyword
CREATE TYPE colors AS ENUM ('red', 'green', 'blue');
CREATE TABLE items (id SERIAL PRIMARY KEY, color colors);
INSERT INTO items (color) VALUES ('green'), ('blue'), ('red');
SELECT color FROM items ORDER BY color;Solution
Step 1: Understand ENUM ordering
ENUM values are ordered by their declaration order: 'red' < 'green' < 'blue'.Step 2: Apply ORDER BY on color column
Ordering by color sorts rows as per ENUM order, so 'red', 'green', 'blue'.Final Answer:
red, green, blue -> Option AQuick Check:
ENUM order = declaration order [OK]
- Assuming alphabetical order instead of ENUM order
- Confusing insertion order with sort order
- Expecting default text sorting
CREATE TYPE status AS ENUM ('new', 'in_progress', 'done');
CREATE TABLE tasks (
id SERIAL PRIMARY KEY,
task_status status DEFAULT 'pending'
);Solution
Step 1: Check ENUM values and default
ENUM 'status' has values 'new', 'in_progress', 'done'. Default 'pending' is not listed.Step 2: Understand default value constraints
Default must be one of ENUM values; otherwise, it causes an error.Final Answer:
The default value 'pending' is not in the ENUM list -> Option CQuick Check:
Default must be ENUM member [OK]
- Assuming any string can be default
- Thinking ENUM can't be column type
- Ignoring missing semicolon errors
status with values ('new', 'in_progress', 'done'). Which statement correctly adds 'archived' after 'done'?Solution
Step 1: Recall how to add ENUM values
PostgreSQL usesALTER TYPE ... ADD VALUE 'new_value' [BEFORE|AFTER existing_value]syntax.Step 2: Identify correct position
To add 'archived' after 'done', useAFTER 'done'.Step 3: Check options
ALTER TYPE status ADD VALUE 'archived' AFTER 'done'; matches correct syntax and position. Others use BEFORE (wrong position) or invalid keywords.Final Answer:
ALTER TYPE status ADD VALUE 'archived' AFTER 'done'; -> Option BQuick Check:
ALTER TYPE ADD VALUE ... AFTER ... [OK]
- Using MODIFY instead of ADD VALUE
- Placing new value BEFORE wrong existing value
- Using wrong position like BEFORE 'done'
