Domain types for validation in PostgreSQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time it takes to check data using domain types grows as we add more data.
How does validation time change when the number of rows increases?
Analyze the time complexity of the following domain type validation.
CREATE DOMAIN positive_int AS INTEGER
CHECK (VALUE > 0);
CREATE TABLE orders (
order_id SERIAL PRIMARY KEY,
quantity positive_int
);
INSERT INTO orders (quantity) VALUES (5), (10), (0); -- last will fail
This code creates a domain type that only allows positive integers and uses it to validate data on insert.
Look at what happens when many rows are inserted or checked.
- Primary operation: Checking the domain constraint for each row inserted or updated.
- How many times: Once per row, for every row being inserted or updated.
Each new row adds one more check to do.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of checks grows directly with the number of rows.
Time Complexity: O(n)
This means the time to validate grows in a straight line as you add more rows.
[X] Wrong: "The domain validation happens once for the whole table, so time stays the same no matter how many rows."
[OK] Correct: Each row is checked separately when inserted or updated, so more rows mean more checks and more time.
Understanding how validation scales helps you design databases that stay fast as data grows. This skill shows you think about real-world data handling.
"What if the domain check was more complex, like checking a pattern or multiple conditions? How would the time complexity change?"
Practice
domain types in PostgreSQL?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 BQuick Check:
Domain types = custom types with validation [OK]
- Confusing domains with indexing
- Thinking domains store large files
- Mixing domains with temporary tables
positive_int that only allows positive integers?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 AQuick Check:
CREATE DOMAIN ... AS ... CHECK(...) [OK]
- Using TYPE instead of AS
- Using WHERE or VALIDATE instead of CHECK
- Missing parentheses around condition
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?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 DQuick Check:
Regex check fails = insert rejected [OK]
- Assuming varchar allows any string without checks
- Thinking invalid data stores as NULL automatically
- Confusing constraint violation with syntax error
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?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 AQuick Check:
Empty string fails length > 0 check [OK]
- Thinking whitespace strings fail (they pass)
- Assuming empty string is allowed
- Confusing syntax errors with constraint errors
rating that stores integers from 1 to 5 inclusive. Which domain definition correctly enforces this range and can be used in multiple tables?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 CQuick Check:
Inclusive range uses >= and <= [OK]
- Using > and < excludes boundary values
- Using WHERE instead of CHECK
- Incomplete IN list misses values
