Introduction
ENUM types let you store a fixed set of values in a column. This helps keep data clean and consistent.
Jump into concepts and practice - no test required
ENUM types let you store a fixed set of values in a column. This helps keep data clean and consistent.
CREATE TYPE enum_name AS ENUM ('value1', 'value2', 'value3'); CREATE TABLE table_name ( column_name enum_name );
CREATE TYPE mood AS ENUM ('happy', 'sad', 'neutral'); CREATE TABLE person ( name TEXT, current_mood mood );
CREATE TYPE size AS ENUM ('small', 'medium', 'large'); CREATE TABLE tshirt ( id SERIAL PRIMARY KEY, size size );
This example creates an ENUM for traffic lights, inserts all values, and selects them.
CREATE TYPE traffic_light AS ENUM ('red', 'yellow', 'green'); CREATE TABLE signals ( id SERIAL PRIMARY KEY, light traffic_light ); INSERT INTO signals (light) VALUES ('red'), ('green'), ('yellow'); SELECT * FROM signals ORDER BY id;
You cannot insert values not listed in the ENUM.
To add new values to an ENUM, you must use ALTER TYPE.
ENUMs improve data integrity by restricting allowed values.
ENUM types store a fixed set of allowed values.
They help keep data consistent and clean.
Create ENUM types once, then use them in table columns.
ENUM types in PostgreSQL?mood with values 'happy', 'sad', and 'neutral'?CREATE TYPE name AS ENUM (values); with values in parentheses and single quotes.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;CREATE TYPE status AS ENUM ('new', 'in_progress', 'done');
CREATE TABLE tasks (
id SERIAL PRIMARY KEY,
task_status status DEFAULT 'pending'
);status with values ('new', 'in_progress', 'done'). Which statement correctly adds 'archived' after 'done'?ALTER TYPE ... ADD VALUE 'new_value' [BEFORE|AFTER existing_value] syntax.AFTER 'done'.