Arrays in Fields

One of the nice features that PostgreSQL supports is the concept of fields that can hold arrays. This enables multiple values of the same data type to be stored in a single field.

To insert arrays into a field, the field should be marked as holding arrays during table creation. After a field has been designated to hold arrays, data can be inserted, selected, or updated into the array by referring to the specific array element in the designated table.

Creating an Array

For example, let’s create a table named students that has a four-element array field to hold test scores:

CREATE TABLE students 
     (name char(20), testscore int[4]); 

Notice that, at table creation time, a field is designated as being able to support arrays by including ...

Get PostgreSQL Essential Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.