QA
Beginner LevelPostgreSQL

What are PostgreSQL Domains and how do they apply reusable column constraints?

❓ Interview Prompt: Explain PostgreSQL Domains (`CREATE DOMAIN`), attaching reusable CHECK constraints, and domain type validation across tables.

A Domain is a user-defined data type created from an underlying base type that incorporates custom CHECK constraints:

Benefit:

Encapsulates business validation rules (e.g. valid email format, positive currency) into a single reusable type applied across multiple tables!

Code Implementation Example

sql
CREATE DOMAIN email_address AS TEXT
CHECK (VALUE ~* '^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$');

CREATE TABLE users (
  id BIGINT PRIMARY KEY,
  email email_address NOT NULL -- Enforces CHECK constraint automatically!
);
💡Defining reusable email_address DOMAIN types with regex CHECK constraints.
🎯 Interview Key Takeaway

Use Domains to centralize domain-specific CHECK constraints and validation rules across multiple database tables.

#DOMAINS#CHECK Constraint#Type Safety#Beginner
Back to all PostgreSQL Questions→

More PostgreSQL Interview Questions

Beginner LevelPostgreSQL

How to use pgvector in PostgreSQL for vector similarity search with HNSW indexes?

Beginner LevelPostgreSQL

What is PostgreSQL architecture and how do client backends connect via process fork model?

Beginner LevelPostgreSQL

What special built-in data types exist in PostgreSQL (JSONB, UUID, ARRAY, INET, TEXT)?