SQL's OR features include three column data types for storing multiple values: ROW, ARRAY, and MULTISET. You can use these data types without using any of the other OR features (in particular, typed tables to store objects). Because they do not act as objects, these columns cannot have methods.
Row Type
A column declared as a row type holds an entire row of data (multiple pieces of data). This gives you the equivalent of a table within a table. The contents of the row—called fields—can be declared as built-in data types or UDTs.
As an example, we'll create a table for customers of the rare book store that stores the customer's address in a single column using the ROW data type:
address ROW (street CHAR (50),
Notice that the ROW column is given a name, just as a single-valued column. The data type is followed by the contents of the row in parentheses. The row's fields are declared in the same way as any other SQL column (name followed by data type).
We use “dot” notation to access the individual fields in a column of type ROW. For example, to reference the
street field in the
address column without qualifying the table name you would use
When the SQL statement requires a table name (for example, for use in a join or for display by a query in which the field appears in multiple tables), you preference the field reference with the table name, as in
Inserting values into a row column is only a bit different from the traditional INSERT statement. Consider the following example:
INSERT INTO customer VALUES
Street,’Anytown’,’ST’,’11224’),
The data for the address column are preceded by the keyword ROW. The values follow in parentheses.
Array Type
An
array is an ordered
collection of elements. Like arrays used in programming, they are declared to have a maximum number of elements of the same type. That type can be a simple data type or a UDT. For example, we might want to store order numbers as part of a customer's data in a
customer table:
The array column is given a name and a data type, which are followed by the keyword ARRAY and the maximum number of elements the array should hold (the array's cardinality), in brackets. The array's data type can be one of SQL's built-in data types or a UDT.
Access to values in an array is by the array's index (its position in the array order). Although you specify the maximum number of elements in an array counting from 1, array indexes begin at 0. An array of 100 elements therefore has indexes from 0 to 99. The sample customer table above includes a column (numb_orders) that stores the total number of elements in the array. The last used index will be numb_orders - 1.
You can input multiple values into an array at one time when you first insert a row:
INSERT INTO customer VALUES
ARRAY (25,109,227,502,610), 5,
The keyword ARRAY precedes the values in parentheses.
You can also insert or modify a specific array element directly:
INSERT INTO customer (first, last, orders[0], numb_orders,phone)
VALUES (‘John’,’Doe’,25,1,’555-111-2233’);
When you query a table and ask for display of an array column by name, without an index, SQL displays the entire contents of the array, as in:
WHERE first = ‘John’ AND last = ‘Doe’;
Use an array index when you want to retrieve a single array element. The query
SELECT orders [numb_orders - 1]
WHERE first = ‘John’ AND last = ‘Doe’;
displays the last order stored in the array.
Processing each element in an array requires programming (a trigger, stored procedure, or embedded SQL). Declare a variable to hold the array index, initialize it to 0, and increment it by 1 each time an appropriate loop iterates—the same way you would process all elements in an array using any high-level programming language.
Note: Although many current DBMSs support arrays in columns, not all automatically perform array bounds checking. In otherwords, they do not necessarily validate that an array index is within the maximum number specified when the table was created. Check your software's documentation to determine whether array bounds constraints must be handled by an application program or can be left up to the DBMS.
Manipulating Arrays
Restrictions of content and access notwithstanding, there are two operations that you can perform on arrays:
◊ Comparisons: Two arrays can be compared (for example, in a WHERE clause) if the two arrays are created from data types that can be compared. When making the comparison, SQL compares the elements in order. Two arrays A and B therefore are equivalent if A[0] = B[0], A[1] = B[1], and so on; all comparisons between all the pairs of values in the array must be true. By the same token, A > B if A[0] > B[0], throughout the arrays.
◊ Concatenation: Two arrays with compatible data types (data types that can be converted into a single data type) can be concatenated with the concatenation operator (‖). The result is another array, as in
Figure 19-2. Notice that the data from array A have been converted to real numbers because SQL will always convert to the format that has the highest precision.
Multiset Type
A
multiset is an unordered collection of elements of the same type. The following table contains a multiset to hold multiple phone numbers:
phones CHAR (20) MULTISET);
You specify the contents of a multiset when you insert a row into a table much like you do for an array. The only difference
is the use of the keyword MULTISET to indicate that the values in parentheses are intended as a single group:
INSERT INTO customer (first, last, orders[0],numb_orders,phones)
VALUES (‘John’,’Doe’,25,1, MULTISET (’555-111-2233’,’555-222-1122’));
Because a multiset is unordered, you cannot access individual elements by position as you do with array elements. You can, however, display the entire contents of the multiset by using its name in a query:
WHERE first = ‘John’ AND last = ‘Doe’;
Updating a multiset is an all or nothing proposition. In other words, you can't pull one value out or put in a single value. An UPDATE statement such as
MULTISET (‘555-111-2233’,’555-333-1122’);
replaces the entire contents of the phones column.
Manipulating Multisets
As with arrays, there are a few operations that can be performed on multisets with compatible data types:
◊ Multisets can be compared, just as arrays. Multisets A and B will be true if they contain exactly the same elements.
◊ Union: The MULTISET UNION operator returns a multiset that contains all elements in the participating multisets. For example,
SET big_multiset = small_multiset1
MULTISET UNION small_multiset2
puts the two small multisets into the big multiset.
◊ Intersect: The MULTISET INTERSECT operator returns all elements that two multisets have in common. For example,
SELECT table1.multiset MULTISET INTERSECT table2.multiset
works on each row in the joined tables, returning the elements that the multisets in each row have in common.
◊ Difference: The MULTISET EXCEPT operation returns the difference between two multisets (all elements they
don't have in common). The query
SELECT table1.multiset MULTISET EXCEPT
functions exactly like the previous example but returns elements from each row that the multisets don't share.
The union, intersect, and difference operators have two options. If you include ALL after the operator, SQL includes duplicate elements in the result. To exclude duplicates, use DISTINCT.