QA
Intermediate LevelPostgreSQL

How do Composite Types (CREATE TYPE name AS (...)) group column attributes?

❓ Interview Prompt: Explain Composite Types (`CREATE TYPE name AS (...)`), multi-attribute data structures, array of composite types, and functions.

Composite Types represent a list of field names and data types, usable as column types or function return signatures:

Use Case:

Grouping related address attributes ('street', 'city', 'zip') into a single composite data structure.

Code Implementation Example

sql
CREATE TYPE address_type AS (
  street TEXT,
  city TEXT,
  zip_code VARCHAR(10)
);

CREATE TABLE companies (
  id BIGINT PRIMARY KEY,
  company_name TEXT,
  headquarters address_type -- Composite type column!
);

-- Access composite fields via parenthesis notation:
SELECT (headquarters).city FROM companies;
💡Defining and querying custom Composite Types.
🎯 Interview Key Takeaway

Use Composite Types to structure multi-attribute domain objects cleanly.

#Composite Types#CREATE TYPE#Schema Design#Intermediate
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)?