ARRAY_MAX_CARDINALITY

ARRAY_MAX_CARDINALITY is a predefined constant in SQL standards that denotes the maximum number of elements an array can have. Its value depends on the SQL implementation and may vary.

Example

DECLARE
TYPE t_numbers IS TABLE OF NUMBER
LIMIT 5;
BEGIN
DBMS_OUTPUT.PUT_LINE('Maximum number of elements: ' || t_numbers.LIMIT);
END;

Output

Maximum number of elements: 5

Explanation

The ARRAY_MAX_CARDINALITY attribute is used to limit the size of a collection. In the example, a PL/SQL block declares a type t_numbers as a table of numbers, limiting its size to 5 elements. The named block then outputs the limit of the collection, which shows as “5”.

Example

CREATE TYPE complex AS (
i INTEGER,
r REAL
);
SELECT ARRAY_MAX_CARDINALITY(ARRAY[(1,2.3)::complex, (4,5.6)::complex]);

Output

2

Explanation

The ARRAY_MAX_CARDINALITY function returns the maximum number of elements that any array can contain. The example finds the maximum cardinality for an array of a custom type complex, which consists of two elements - an INTEGER and a REAL number. The output 2 implies that the array contains two elements.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.