0
0
PostgreSQLquery~5 mins

ENUM types in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an ENUM type in PostgreSQL?
An ENUM type is a special data type that allows you to define a list of allowed values. It helps to store only one of these predefined values in a column.
Click to reveal answer
beginner
How do you create an ENUM type in PostgreSQL?
Use the command: CREATE TYPE mood AS ENUM ('happy', 'sad', 'neutral'); This creates a new ENUM type named 'mood' with three allowed values.
Click to reveal answer
beginner
Can you insert a value not listed in an ENUM type column?
No, PostgreSQL will reject any value that is not one of the predefined ENUM values. This ensures data consistency.
Click to reveal answer
intermediate
How do you add a new value to an existing ENUM type?
Use ALTER TYPE mood ADD VALUE 'excited'; to add a new allowed value 'excited' to the ENUM type 'mood'.
Click to reveal answer
intermediate
Why use ENUM types instead of text columns?
ENUM types restrict values to a fixed set, preventing invalid data. They also use less storage and can improve query speed compared to text columns.
Click to reveal answer
Which command creates a new ENUM type in PostgreSQL?
ACREATE TABLE status ENUM ('open', 'closed');
BCREATE ENUM status ('open', 'closed');
CCREATE TYPE status AS ENUM ('open', 'closed');
DALTER TYPE status ADD ENUM ('open', 'closed');
What happens if you try to insert a value not in the ENUM list?
AThe value is converted to the closest ENUM value.
BThe value is inserted as NULL.
CThe value is automatically added to the ENUM list.
DPostgreSQL throws an error and rejects the insert.
How do you add a new value to an existing ENUM type?
AALTER TYPE mood ADD VALUE 'excited';
BCREATE TYPE mood ADD VALUE 'excited';
CUPDATE TYPE mood SET VALUE = 'excited';
DALTER TABLE mood ADD VALUE 'excited';
Which of these is NOT an advantage of ENUM types?
ARestricting column values to a fixed set.
BAutomatically updating ENUM values on insert.
CUsing less storage than text columns.
DImproving query speed for fixed sets.
What data type would you use to store a user's mood with fixed options?
AENUM
BINTEGER
CTEXT
DBOOLEAN
Explain what an ENUM type is and how it helps maintain data quality in PostgreSQL.
Think about how you limit choices in a form.
You got /4 concepts.
    Describe the steps to create an ENUM type and add a new value to it in PostgreSQL.
    Start with creating, then modifying the ENUM.
    You got /3 concepts.