|
Home | Contents | Index | Master Index | New Book |
Accessing Collection Type DataThis section describes how to use DB-Access and the SQL Editor to query and modify columns that are defined on collection types. The only way to select, insert, update, or delete individual elements in a collection is through an external or SPL routine. In addition, you cannot perform subqueries on a column that is a collection type.For information on how to create an ESQL/C program to modify collection type data, see the INFORMIX-ESQL/C Programmer's Manual. For information on how to create a stored procedure to modify collection type data, see Chapter 14, "Creating and Using SPL Routines." Figure 12-3 shows the manager table, which is used in examples throughout this section. The manager table contains examples of simple and nested collection types. A simple collection is a collection type that does not contain any fields that are themselves collection types. The direct_reports column of the manager table is an example of a simple collection. A nested collection is a collection type that contains another collection type. The projects column of the manager table is an example of a nested collection.
Selecting CollectionsA query on a column that is a collection type returns, for each row in the table, all the elements that the particular collection contains. For example, Query 12-8 shows a query that returns data in the department column and all elements in the direct_reports column, for each row of the manager table.
Query Result 12-9 shows a project column collection for a single row of the manager table. The collection contains, for each element in the LIST, the project name (pro_name) and the SET of individuals (pro_members) who are assigned to each project.
Adams is an element of a collection in the direct_reports column.As Query Result 12-11 shows, the query returns the entire collection, never a particular element within the collection. You can use the IN keyword in a WHERE clause to reference a simple collection only. You cannot use the IN keyword to reference a collection that contains fields that are themselves collections. For example, you cannot use the IN keyword to reference the projects column in the manager table because projects is a nested collection.
You can combine the NOT and IN keywords in the WHERE clause of a SELECT statement to search for collections that do not contain a certain element. For example, Query 12-12 shows a query that returns values for mgr_name and department where
Using the CARDINALITY() Function to Count the Elements in a CollectionThe CARDINALITY() function counts the number of elements that a collection contains. Any duplicates in a collection are counted as individual elements. Query 12-13 shows a query that returns, for every row in the manager table, department values and the number of elements in each direct_reports collection.
To insert values into a collection that is a row type, you must specify a value for each field in the row type. You can insert null values into the fields of a collection row type, provided that at least one of the fields of the row type is not null. In other words, for a specific element of a collection, you can insert values for some fields of the collection row and specify null values for other fields. You also can specify an empty collection. An empty collection is a collection that contains no elements. To specify an empty collection, use the braces ({}). For example, the following statement inserts data into a row in the manager table but specifies that the direct_reports and projects columns are empty collections: A collection column cannot contain null elements. The following statement returns an error because the direct_reports column and all fields of the projects column specify null elements: The following syntax rules apply for performing inserts and updates on collection types:
Updating Collection TypesWhen you use DB-Access or the SQL Editor to update a collection type, you must update the entire collection. The following statement shows how to update the projects column. To locate the row that needs to be updated, use the IN keyword to perform a search on the direct_reports column.The first occurrence of the SET keyword in the preceding statement is part of the UPDATE statement syntax. Do not confuse it with the SET constructor which indicates that a collection is a SET. Although you can use the IN keyword to locate specific elements of a simple collection, you cannot update individual elements of a collection column from DB-Access or the SQL Editor.
|