Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a domain type in PostgreSQL?
A domain type is a user-defined data type based on an existing data type with optional constraints to enforce validation rules.
Click to reveal answer
beginner
How do domain types help with data validation?
Domain types allow you to define rules like NOT NULL, CHECK constraints, or specific formats once, and reuse them to ensure consistent validation across tables.
Click to reveal answer
intermediate
Write the basic syntax to create a domain type that only accepts positive integers.
CREATE DOMAIN positive_int AS integer CHECK (VALUE > 0);
Click to reveal answer
beginner
Can domain types be used as column data types in tables?
Yes, domain types can be used as column types in tables, and the constraints defined in the domain are automatically enforced on the column data.
Click to reveal answer
beginner
What happens if you try to insert invalid data into a column using a domain type?
PostgreSQL rejects the insert or update operation and returns an error because the data violates the domain's constraints.
Click to reveal answer
What is the main purpose of a domain type in PostgreSQL?
ATo create a reusable data type with validation rules
BTo store large binary data
CTo define a new table
DTo create a backup of the database
✗ Incorrect
Domain types let you define a data type with constraints that can be reused for validation.
Which SQL keyword is used to create a domain type?
ACREATE TYPE
BCREATE DOMAIN
CCREATE TABLE
DCREATE CONSTRAINT
✗ Incorrect
The correct syntax to create a domain type uses CREATE DOMAIN.
If a domain type has a CHECK constraint, when is it enforced?
AOnly when the domain is created
BWhen the table is dropped
CWhen data is inserted or updated in columns using the domain
DOnly during database backup
✗ Incorrect
CHECK constraints on domains are enforced whenever data is inserted or updated in columns using that domain.
Can a domain type be based on another domain type?
AYes, domains can be based on other domains
BNo, domains must be based on built-in types only
COnly if the domain is temporary
DOnly in PostgreSQL versions before 10
✗ Incorrect
PostgreSQL allows domains to be based on other domains, inheriting their constraints.
What happens if you try to insert a NULL value into a domain with NOT NULL constraint?
AThe database crashes
BThe insert succeeds
CThe NULL is converted to zero
DThe insert fails with a constraint violation error
✗ Incorrect
NOT NULL constraints on domains prevent NULL values, causing an error if violated.
Explain what a domain type is and how it helps with data validation in PostgreSQL.
Think about how you can create a custom data type with rules that apply everywhere.
You got /4 concepts.
Describe the steps and syntax to create a domain type that only accepts email addresses in PostgreSQL.
Use CREATE DOMAIN with a CHECK that tests the format of the input.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using domain types in PostgreSQL?
easy
A. To speed up query execution by indexing
B. To create custom data types with automatic validation rules
C. To store large binary data efficiently
D. To create temporary tables for session use
Solution
Step 1: Understand what domain types are
Domain types are user-defined data types that include validation rules to ensure data quality.
Step 2: Identify the main purpose
The main purpose is to enforce rules automatically when data is inserted or updated, keeping data clean.
Final Answer:
To create custom data types with automatic validation rules -> Option B
Quick Check:
Domain types = custom types with validation [OK]
Hint: Domains add rules to types, not speed or storage [OK]
Common Mistakes:
Confusing domains with indexing
Thinking domains store large files
Mixing domains with temporary tables
2. Which of the following is the correct syntax to create a domain named positive_int that only allows positive integers?
easy
A. CREATE DOMAIN positive_int AS integer CHECK (VALUE > 0);
B. CREATE DOMAIN positive_int TYPE integer WHERE VALUE > 0;
C. CREATE DOMAIN positive_int AS integer VALIDATE (VALUE > 0);
D. CREATE DOMAIN positive_int AS integer IF VALUE > 0;
Solution
Step 1: Recall the correct syntax for domain creation
The syntax is: CREATE DOMAIN name AS base_type CHECK (condition);
Step 2: Match the syntax with options
CREATE DOMAIN positive_int AS integer CHECK (VALUE > 0); matches the correct syntax with CHECK and VALUE keyword.
Final Answer:
CREATE DOMAIN positive_int AS integer CHECK (VALUE > 0); -> Option A
Quick Check:
CREATE DOMAIN ... AS ... CHECK(...) [OK]
Hint: Use CHECK with VALUE keyword in domain creation [OK]
Common Mistakes:
Using TYPE instead of AS
Using WHERE or VALIDATE instead of CHECK
Missing parentheses around condition
3. Given the domain creation:
CREATE DOMAIN us_zipcode AS varchar(5) CHECK (VALUE ~ '^[0-9]{5}$');
What will happen if you try to insert '1234a' into a table column of type us_zipcode?
medium
A. The insert will succeed because it's a varchar
B. The insert will cause a syntax error
C. The insert will succeed but store NULL instead
D. The insert will fail due to the CHECK constraint violation
Solution
Step 1: Understand the domain's CHECK constraint
The domain requires the value to match exactly 5 digits using a regular expression.
Step 2: Check the value '1234a' against the regex
'1234a' contains a letter, so it does not match the pattern of 5 digits.
Final Answer:
The insert will fail due to the CHECK constraint violation -> Option D
Quick Check:
Regex check fails = insert rejected [OK]
Hint: Regex in CHECK rejects invalid patterns [OK]
Common Mistakes:
Assuming varchar allows any string without checks
Thinking invalid data stores as NULL automatically
Confusing constraint violation with syntax error
4. You have this domain:
CREATE DOMAIN non_empty_text AS text CHECK (LENGTH(VALUE) > 0);
Which of the following INSERT statements will cause an error when inserting into a column of type non_empty_text?
medium
A. INSERT INTO table_name (col) VALUES ('');
B. INSERT INTO table_name (col) VALUES ('world');
C. INSERT INTO table_name (col) VALUES (' ');
D. INSERT INTO table_name (col) VALUES (' hello ');
Solution
Step 1: Understand the CHECK condition
The domain requires text length to be greater than 0, so empty strings fail. Whitespace-only strings pass as they have length > 0.
Step 2: Evaluate each insert value
INSERT INTO table_name (col) VALUES (''); inserts an empty string '', length 0, violating the check.
Final Answer:
INSERT with empty string '' causes error -> Option A
Quick Check:
Empty string fails length > 0 check [OK]
Hint: Empty strings fail length > 0 check [OK]
Common Mistakes:
Thinking whitespace strings fail (they pass)
Assuming empty string is allowed
Confusing syntax errors with constraint errors
5. You want to create a domain rating that stores integers from 1 to 5 inclusive. Which domain definition correctly enforces this range and can be used in multiple tables?
hard
A. CREATE DOMAIN rating AS integer WHERE (VALUE BETWEEN 1 AND 5);
B. CREATE DOMAIN rating AS integer CHECK (VALUE > 1 AND VALUE < 5);
C. CREATE DOMAIN rating AS integer CHECK (VALUE >= 1 AND VALUE <= 5);
D. CREATE DOMAIN rating AS integer CHECK (VALUE IN (1, 2, 3, 4));
Solution
Step 1: Understand the required range
The rating must include 1 and 5, so boundaries are inclusive.
Step 2: Analyze each CHECK condition
The option using WHERE instead of CHECK has invalid syntax. CREATE DOMAIN rating AS integer CHECK (VALUE > 1 AND VALUE < 5); excludes 1 and 5. CREATE DOMAIN rating AS integer CHECK (VALUE >= 1 AND VALUE <= 5); uses >= 1 AND <= 5, correctly including boundaries. CREATE DOMAIN rating AS integer CHECK (VALUE IN (1, 2, 3, 4)); uses IN list which misses 5.
Step 3: Choose the best option for clarity and standard usage
CREATE DOMAIN rating AS integer CHECK (VALUE >= 1 AND VALUE <= 5); is clear, standard, and commonly used for range checks.
Final Answer:
CREATE DOMAIN rating AS integer CHECK (VALUE >= 1 AND VALUE <= 5); -> Option C
Quick Check:
Inclusive range uses >= and <= [OK]
Hint: Use >= and <= for inclusive numeric domain checks [OK]