7.7.
VALUES
Lists
7.7.VALUES list
VALUES
provides a way to generate a “constant table” that can be used in a query without having to
actually create and populate a table on-disk. The syntax is
VALUES provides a generation
can be used in the query
method "often scale" without having to actually create and populate tables on disk. grammar:
VALUES ( expression [, ...] ) [, ...]
Each parenthesized list of expressions generates a row in the table. The lists must all have the same
number of elements (i.e., the number of columns in the table), and corresponding entries in each
list must have compatible data types. The actual data type assigned to each column of the result is
determined using the same rules as for
UNION
(see Section 10.5).
Expression yields within each row brackets. The list must have the same number of elements (e.g., the number of columns in the table), and the corresponding columns have compatible data types. The actual allocation of data types for each column UNION same rules (see
Section 10.5
).
As an example:
Example:
VALUES (1, 'one'), (2, 'two'), (3, 'three');
will return a table of two columns and three rows. It's effectively equivalent to:
Regular returns on a table with three rows of two. Equivalent to the following statement:
SELECT 1 AS column1, 'one' AS column2
UNION ALL
SELECT 2, 'two'
UNION ALL
SELECT 3, 'three';
By default, PostgreSQL assigns the names
column1
,
column2
, etc. to the columns of a
VALUES
table. The column names are not specified by the SQL standard and different database systems do it
differently, so it's usually better to override the default names with a table alias list, like this:
By default, PostgreSQL distribution column names column1, column2 etc. VALUES columns of the table. Column names were not bound by the SQL standard and different database systems may be different, so it's best to use table aliases list to override the default name, for example:
=> SELECT * FROM (VALUES (1, 'one'), (2, 'two'), (3, 'three')) AS t (num,letter);
num | letter
-----+--------
1 | one
2 | two
3 | three
(3 rows)
Syntactically,
VALUES
followed by expression lists is treated as equivalent to:
Syntactically, VALUES followed by expression lists is treated as equivalent to:
SELECT select_list FROM table_expression
and can appear anywhere a
SELECT
can. For example, you can use it as part of a
UNION
, or attach a
sort_specification
(
ORDER BY
,
LIMIT
, and/or
OFFSET
) to it.
VALUES
is most commonly
used as the data source in an
INSERT
command, and next most commonly as a subquery.
And can appear anywhere in SEELCT may arise. For example,
it may be used as part of UNION also sort_specification (ORDER BY, LIMIT and / or OFFSET) may be attached to the suit. VALUES most commonly used as a data source in the INSERT command, followed by the most commonly used as sub-queries.
For more information see VALUES.