Challenge - 5 Problems
Domain Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of domain constraint violation
Consider the following domain definition and insert statement in PostgreSQL:
What will be the result of the insert statement if
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);
Attempts:
2 left
💡 Hint
Think about what the CHECK constraint on the domain does when a value does not meet the condition.
✗ Incorrect
The domain
positive_int restricts values to integers greater than 0. Inserting -5 violates this CHECK constraint, so PostgreSQL raises an error and rejects the insert.🧠 Conceptual
intermediate1:30remaining
Purpose of domain types in PostgreSQL
What is the main purpose of using domain types in PostgreSQL?
Attempts:
2 left
💡 Hint
Think about how domains help keep data consistent across tables.
✗ Incorrect
Domains allow you to define a custom data type with constraints like CHECK rules. This helps enforce consistent validation rules wherever the domain is used.
📝 Syntax
advanced2: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?Attempts:
2 left
💡 Hint
Remember the correct keywords for domain creation and how to write a CHECK constraint.
✗ Incorrect
Option D uses the correct syntax:
CREATE DOMAIN name AS base_type CHECK (condition). The condition uses LIKE with wildcards to check for '@'.❓ optimization
advanced1: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?
Attempts:
2 left
💡 Hint
Consider what happens when PostgreSQL validates data against constraints.
✗ Incorrect
CHECK constraints are evaluated on every insert or update. Complex checks with functions add overhead, slowing down inserts.
🔧 Debug
expert2:30remaining
Diagnosing domain constraint error
A developer created this domain:
They try to insert '123-456-7890' but get a constraint violation error. Why?
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?
Attempts:
2 left
💡 Hint
Check what the regular expression requires and what the inserted value contains.
✗ Incorrect
The regex requires only digits 0-9 with no other characters. The inserted value has dashes, so it fails the CHECK constraint.