0
0
PostgreSQLquery~10 mins

Domain types for validation in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Domain types for validation
Define Domain with Base Type
Add Validation Constraints
Use Domain in Table Columns
Insert Data
Validation Checks Applied
Data Stored
You define a domain with a base type and constraints, then use it in tables. When inserting data, PostgreSQL checks constraints and accepts or rejects the data.
Execution Sample
PostgreSQL
CREATE DOMAIN us_postal_code AS TEXT
  CHECK (VALUE ~ '^\d{5}(-\d{4})?$');

CREATE TABLE addresses (
  id SERIAL PRIMARY KEY,
  postal_code us_postal_code
);

INSERT INTO addresses (postal_code) VALUES ('12345');
INSERT INTO addresses (postal_code) VALUES ('ABCDE');
Defines a domain for US postal codes with a regex check, creates a table using it, then inserts one valid and one invalid postal code.
Execution Table
StepActionInput/ValueValidation ResultOutcome
1Create domain us_postal_codeTEXT with regex checkDomain createdDomain ready for use
2Create table addressesid SERIAL, postal_code us_postal_codeTable createdTable ready for data
3Insert postal_code '12345''12345'Matches regexRow inserted successfully
4Insert postal_code 'ABCDE''ABCDE'Does not match regexError: check constraint violation
💡 Execution stops on error at step 4 due to invalid postal code format
Variable Tracker
VariableStartAfter Step 3After Step 4
postal_codeundefined'12345' accepted'ABCDE' rejected
Key Moments - 2 Insights
Why does the second insert fail even though the column type is TEXT?
Because the domain us_postal_code has a check constraint that requires the value to match a specific regex pattern. The value 'ABCDE' does not match, so PostgreSQL rejects it (see execution_table step 4).
Can I use the domain type in multiple tables?
Yes, once defined, the domain type can be used in any table column just like a regular data type, enforcing the same validation everywhere.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what happens at step 3 when inserting '12345'?
AThe value fails validation and raises an error
BThe value matches the regex and is inserted successfully
CThe value is converted to a number before insertion
DThe domain is redefined at this step
💡 Hint
Check the Validation Result and Outcome columns at step 3 in the execution_table
At which step does the check constraint cause an error?
AStep 4
BStep 2
CStep 3
DStep 1
💡 Hint
Look at the Validation Result and Outcome columns for each step in the execution_table
If the regex in the domain was changed to allow letters, how would the execution_table change?
AStep 3 would fail instead
BThe domain creation would fail at step 1
CStep 4 would succeed and insert the value
DNo change would happen
💡 Hint
Think about how the Validation Result and Outcome depend on the regex check in the domain
Concept Snapshot
CREATE DOMAIN name AS base_type
  CHECK (condition);
Use domain as column type.
PostgreSQL enforces constraints on insert/update.
Invalid data causes errors, valid data stores normally.
Full Transcript
This visual execution shows how PostgreSQL domain types work for validation. First, a domain is created with a base type and a check constraint using a regex pattern. Then a table is created using this domain as a column type. When inserting data, PostgreSQL checks if the value matches the domain's constraint. If it matches, the row is inserted successfully. If not, an error is raised and insertion fails. This ensures data validity at the database level. Domains can be reused in multiple tables for consistent validation.