Complex Data Types

An instance of a complex data type contains multiple values and provides access to its nested values. Currently, Oracle NoSQL Database supports the following kinds of complex values:

Table 2-3 Complex Data Types

Data Type Description Example
ARRAY (T) In general, an array is an ordered collection of zero or more items. The items of an array are called elements. Arrays cannot contain any NULL values.

An instance of ARRAY (T) is an array whose elements are all instances of type T. T is called element type of the array.

Type: ARRAY (INTEGER)

Type Instance: [600004,560076,01803]

MAP (T) In general, a map is an unordered collection of zero or more key-item pairs, where all keys are strings. The keys in a map must be unique. The key-item pairs are called fields. The keys are called fields names, and the associated items are called field values. Maps cannot contain any NULL field value.

An instance of MAP (T) is a map whose field values are all instance of type T. T is called the value type of the map.

Type: MAP(INTEGER)

Type Instance: { "Chennai":600004, "Bangalore":560076, "Boston":01803 }

RECORD (k1 T1 n1, k2 T2 n2, ….…, kn Tn nn) In general, a record is an ordered collection of one or more key-item pairs, where all keys are strings. The keys in a record must be unique. The key-item pairs are called fields. The keys are called fields names, and the associated items are called field values. Records may contain NULL as field value.

An instance of RECORD (k1 T1 n1, k2 T2 n2, ….…, kn Tn nn) is a record of exactly n fields, where for each field i (a) the field name is ki, (b) the field value is an instance of type Ti, and (c) the field conforms to the nullability property ni, which specifies whether the field value may be NULL or not.

Contrary to maps and arrays, it is not possible to add or remove fields from a record. This is because the number of fields and their field names are part of the record type definition associated with a record value.

Type: RECORD(country STRING, zipcode INTEGER, state STRING, street STRING)

Type Instance: { "country":"US", "zipcode":600004, "state":"Arizona", "street":"4th Block" }

Example 2-1 Complex Data Type

The following examples illustrate the difference between the way data get stored in various complex data types.

To store the zip codes of multiple cities when the number of zip codes is not known in advance, you can use arrays.

Declaration:
ARRAY(INTEGER)
Example:
[600004,560076,01803]

To store the names of multiple cities along with their zip codes and the number of zip codes are not known, you can use maps.

Declaration:
MAP(INTEGER)
Example:
{
"Chennai":600004,
"Bangalore":560076,
"Boston":01803
}

Records are used for an ordered collection. If you want to store a zip code as part of a bigger data set, you can use records. In this example, only a zip code is stored in a record.

Declaration:
RECORD(zipcode INTEGER)
Example:
{
"zipcode":600004
}

You can combine multiple complex data types so that more complex data can be stored. For the same zipcode data, the following example combines two complex data types and stores the information.

Declaration:
ARRAY(RECORD(area STRING, zipcode INTEGER))
Example:
[
    {"area":"Chennai","zipcode":600004},
    {"area":"Bangalore","zipcode":560076},
    {"area":"Boston","zipcode":01803}
]

Example 2-2 Complex Data Type

This example illustrate the differences in the way a map and a record should be declared for storing complex data.

Let us consider the following data.

{
"name":"oracle",
"city":"Redwood City",
"zipcode":94065,
"offices":["Chennai","Bangalore","Boston"]
}

For the above data, you declare a map and a record as shown below.

Record
(
name STRING,
city STRING,
zipcode INTEGER,
offices Array(STRING)
)
Map(ANY)