0
0
PostgreSQLquery~10 mins

Composite types in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Composite types
Define Composite Type
Create Table Using Composite Type
Insert Data into Table
Query Data
Access Composite Type Fields
First, you define a composite type, then create a table using it, insert data, query it, and access its fields.
Execution Sample
PostgreSQL
CREATE TYPE address AS (
  street TEXT,
  city TEXT,
  zip_code TEXT
);

CREATE TABLE person (
  id SERIAL PRIMARY KEY,
  name TEXT,
  home_address address
);

INSERT INTO person (name, home_address) VALUES
('Alice', ROW('123 Main St', 'Springfield', '12345'));

SELECT name, home_address.city FROM person;
This code defines a composite type 'address', creates a table 'person' with a column of that type, inserts a row, and queries the city field.
Execution Table
StepActionEvaluationResult
1CREATE TYPE addressDefines composite type with fields street, city, zip_codeType 'address' created
2CREATE TABLE personCreates table with id, name, home_address of type addressTable 'person' created
3INSERT INTO personInsert row with name='Alice' and home_address=('123 Main St','Springfield','12345')Row inserted with id=1
4SELECT name, home_address.city FROM personRetrieve name and city field from home_addressReturns row: name='Alice', city='Springfield'
5End of operationsNo more commandsExecution complete
💡 All commands executed successfully; query returned expected data.
Variable Tracker
VariableStartAfter Step 3Final
address typeundefineddefined with fields street, city, zip_codedefined
person tableundefinedcreated with columns id, name, home_addresscreated
person rowsempty1 row inserted: (1, 'Alice', ('123 Main St', 'Springfield', '12345'))1 row present
Key Moments - 3 Insights
How do you access a field inside a composite type in a SELECT query?
You use dot notation like home_address.city as shown in step 4 of the execution_table.
Can you insert data into a composite type column using a simple tuple?
Yes, as in step 3, you insert data using parentheses with values matching the composite type fields.
Is the composite type created before the table that uses it?
Yes, step 1 creates the composite type before step 2 creates the table that uses it.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of step 4?
AReturns error due to wrong field access
BReturns empty result
CReturns row with name='Alice' and city='Springfield'
DReturns all fields of home_address
💡 Hint
Check the 'Result' column in step 4 of the execution_table.
At which step is the composite type 'address' defined?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column to find when 'CREATE TYPE address' happens.
If you wanted to add a new field 'state' to the composite type, which step would change?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Composite type definition happens in step 1, so changes to its fields affect that step.
Concept Snapshot
Composite types in PostgreSQL:
- Use CREATE TYPE to define a composite type with named fields.
- Use the composite type as a column type in tables.
- Insert data using tuples matching the composite structure.
- Access fields with dot notation (e.g., column.field).
- Composite types help group related data logically.
Full Transcript
This visual execution trace shows how to use composite types in PostgreSQL. First, a composite type named 'address' is created with fields street, city, and zip_code. Then, a table 'person' is created with a column 'home_address' of type 'address'. Next, a row is inserted into 'person' with name 'Alice' and an address tuple. Finally, a SELECT query retrieves the name and the city field from the composite address. The trace highlights how composite types are defined before use, how data is inserted as tuples, and how to access individual fields using dot notation.