RefLib

Reference library for AI

Back

postgresql_16:3.5.16.2 Constructing Composite Values

PostgreSQL 16 > The SQL Language > Data Types > Composite Types > Constructing Composite Values

To write a composite value as a literal constant, enclose the field values within parentheses and separate them by commas. You can put double quotes around any field value, and must do so if it contains commas or parentheses. (More details appear below (see postgresql_16:3.5.16.6).) Thus, the general format of a composite constant is the following: '( *val1* , *val2* , ... )' An example is: '("fuzzy dice",42,1.99)' which would be a valid value of the inventory_item type defined above. To make a field be NULL, write no characters at all in its position in the list. For example, this constant specifies a NULL third field: '("fuzzy dice",42,)' If you want an empty string rather than NULL, write double quotes: '("",42,)' Here the first field is a non-NULL empty string, the third is NULL.

(These constants are actually only a special case of the generic type constants discussed in (see postgresql_16:3.1.1.2). The constant is initially treated as a string and passed to the composite-type input conversion routine. An explicit type specification might be necessary to tell which type to convert the constant to.)

The ROW expression syntax can also be used to construct composite values. In most cases this is considerably simpler to use than the string-literal syntax since you don't have to worry about multiple layers of quoting. We already used this method above: ROW('fuzzy dice', 42, 1.99) ROW('', 42, NULL) The ROW keyword is actually optional as long as you have more than one field in the expression, so these can be simplified to: ('fuzzy dice', 42, 1.99) ('', 42, NULL) The ROW expression syntax is discussed in more detail in (see postgresql_16:3.1.2.13).

Back