Using ENUM Types in PostgreSQL
📖 Scenario: You are building a simple database to store information about customer orders. Each order has a status that can only be one of a few fixed values.
🎯 Goal: Create an ENUM type called order_status with specific values, then create a table orders using this ENUM type for the status column.
📋 What You'll Learn
Create an ENUM type named
order_status with values 'pending', 'shipped', and 'delivered'.Create a table named
orders with columns order_id (integer primary key), customer_name (text), and status using the order_status ENUM type.Insert one sample row into the
orders table with order_id 1, customer_name 'Alice', and status 'pending'.Update the
status of the order with order_id 1 to 'shipped'.💡 Why This Matters
🌍 Real World
ENUM types are useful when you have a column that should only allow a fixed set of values, like order statuses, user roles, or product categories.
💼 Career
Knowing how to use ENUM types helps you design databases that enforce data integrity and make your queries simpler and safer.
Progress0 / 4 steps