0
0
PostgreSQLquery~20 mins

Domain types for validation in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Domain Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of domain constraint violation
Consider the following domain definition and insert statement in PostgreSQL:

CREATE DOMAIN positive_int AS INTEGER CHECK (VALUE > 0);
INSERT INTO test_table (id) VALUES (-5);

What will be the result of the insert statement if id is of type positive_int?
PostgreSQL
CREATE DOMAIN positive_int AS INTEGER CHECK (VALUE > 0);
CREATE TABLE test_table (id positive_int);
INSERT INTO test_table (id) VALUES (-5);
AThe insert succeeds and stores -5.
BThe insert succeeds but stores NULL instead of -5.
CThe insert fails with a check constraint violation error.
DThe insert fails with a syntax error.
Attempts:
2 left
💡 Hint
Think about what the CHECK constraint on the domain does when a value does not meet the condition.
🧠 Conceptual
intermediate
1:30remaining
Purpose of domain types in PostgreSQL
What is the main purpose of using domain types in PostgreSQL?
ATo speed up query execution by indexing columns automatically.
BTo create reusable custom data types with built-in validation rules.
CTo store large binary objects efficiently.
DTo define user roles and permissions.
Attempts:
2 left
💡 Hint
Think about how domains help keep data consistent across tables.
📝 Syntax
advanced
2:00remaining
Correct domain creation syntax
Which of the following SQL statements correctly creates a domain named email_addr that stores text and validates that the value contains an '@' character?
ACREATE DOMAIN email_addr TYPE TEXT CHECK (VALUE CONTAINS '@');
BCREATE DOMAIN email_addr AS TEXT WHERE VALUE LIKE '%@%';
CCREATE DOMAIN email_addr AS VARCHAR CHECK (VALUE LIKE '@');
DCREATE DOMAIN email_addr AS TEXT CHECK (VALUE LIKE '%@%');
Attempts:
2 left
💡 Hint
Remember the correct keywords for domain creation and how to write a CHECK constraint.
optimization
advanced
1:30remaining
Performance impact of domains with complex checks
If a domain has a complex CHECK constraint involving multiple function calls, what is the likely impact on database performance when inserting rows using this domain?
AInsert performance may decrease because the CHECK constraint is evaluated for each row.
BInsert performance improves because the domain caches results.
CThere is no impact on performance because CHECK constraints are ignored during inserts.
DInsert performance depends only on the base data type, not the domain.
Attempts:
2 left
💡 Hint
Consider what happens when PostgreSQL validates data against constraints.
🔧 Debug
expert
2:30remaining
Diagnosing domain constraint error
A developer created this domain:

CREATE DOMAIN phone_number AS TEXT CHECK (LENGTH(VALUE) = 10 AND VALUE ~ '^[0-9]+$');

They try to insert '123-456-7890' but get a constraint violation error. Why?
AThe value contains dashes which violate the regular expression constraint.
BThe LENGTH function does not work on TEXT types.
CThe domain does not allow NULL values by default.
DThe regular expression is invalid and causes a syntax error.
Attempts:
2 left
💡 Hint
Check what the regular expression requires and what the inserted value contains.