Declaration of Array Types
To create an array column in a table, use the following syntax:- nameis a regular text column.
- pay_by_quarteris a one-dimensional array of integers, representing an employee’s salary by quarter.
- scheduleis a two-dimensional array of text, representing an employee’s weekly schedule.
ARRAY keyword for one-dimensional arrays:
[]) to the data type name of the array elements. The declared size or number of dimensions doesn’t affect runtime behavior; it’s mainly for documentation purposes1.
Array Value Input
To write an array value as a literal constant, enclose the element values within curly braces and separate them by commas:Accessing Array Elements
You can access individual elements of an array using the[index] syntax. The first element has an index of one:
Modifying Arrays
Arrays are mutable. Use theARRAY[...] constructor to create or modify arrays:
Searching in Arrays
You can search for specific values within an array using operators likeANY or ALL:
Array Functions and Operators
PostgreSQL provides a rich set of functions and operators for working with arrays. Some useful ones include:- array_length(arr, dim): Returns the length of the array along the specified dimension.
- unnest(arr): Expands an array into a set of rows.
- array_agg(expr): Aggregates values into an array.
- @>and- <@: Tests if an array contains another array or value.