You are on page 1of 1

Structured data types

A structured data type can be used as the type for a column in a regular table (Listings 1 and 2), the type for an entire table (or view), or as an attribute of another structured type (Listing 3). When used as the type for a table, the table is known as a typed table (Listing 3). You can create a table with structured type columns in the same way that you would create a table using only DB2 built-in data types. In both cases, you must assign a data type to every column in the table. If the column is a structured type column, you specify the name of the structured type as its data type (Listing 1). Structured data types exhibit a behavior known as inheritance. A structured type can have subtypes, other structured types that reuse all of its attributes and contain their own specific attributes. The type from which a subtype inherits attributes is known as its supertype. A type hierarchy is a set of subtypes that are based on the same supertype; the pre-eminent supertype in a hierarchy is known as the root type of the hierarchy. Use the CREATE TYPE statement to create a structured type, and use the DROP statement to delete a structured type. When you create a structured type, DB2 automatically creates a constructor function for the type and creates both mutator and observer methods for the attributes of the type. You can use the constructor function and the mutator method to create instances of the structured type, and then you can insert these instances into the column of a table. The constructor function has the same name as the structured type with which it is associated. The constructor function has no parameters and returns an instance of the type with all of its attributes set to null values. A mutator method exists for each attribute of a structured type. When you invoke a mutator method on an instance of a structured type and specify a new value for its associated attribute, the method returns a new instance with the attribute updated to the new value. An observer method exists for each attribute of a structured type. When you invoke an observer method on an instance of a structured type, the method returns the value of the attribute for that instance. To invoke a mutator or observer method on an instance of a structured type, use the double-dot (..) operator (Listings 1, 2, and3).

Typed tables
A typed table is a table that is defined with a user-defined structured type. Typed tables store instances of structured types as rows, in which each attribute of a type is stored in a separate column; in fact, the names and data types of the attributes of the structured type become the names and data types of the columns of the typed table. Similarly to structured types, typed tables can be part of a table hierarchy consisting of a single root table, supertables, and subtables. Use the CREATE TABLE statement to create a typed table, and use the DROP statement to delete a typed table. The typed table that is being dropped cannot have any subtables. You can also drop an entire table hierarchy by specifying the HIERARCHYkeyword in the DROP statement (DROP TABLE HIERARCHY <root-table-name>).

A detailed example
For this example, we will use the SAMPLE database that comes with DB2 UDB. Our example requires a database connection to start, nothing else, and for this, the SAMPLE database will do quite nicely. We will create a table named CLIENTS with four columns. CLIENT_ID is a system-generated identity column; CLIENT_LNAME and CLIENT_FNAME contain a client's last and first name, respectively; and ADDRESS is a structured type column of type Address_t. (A useful convention is to name structured types with the '_t' suffix to make their identity as structured types obvious.) We will need to create this structured type before we attempt to create the CLIENTS table (Listing 1). We will use the simplest form of the CREATE TYPE statement, specifying only four attributes (street, city, province and postal_code) and the required MODE DB2_SQL clause. To retrieve structured type data from a table, there must be some way to convert that type into a single scalar value whose type, in turn, must be based on one of the built-in DB2 data types. To accomplish this conversion, we first have to create a FROM SQLtransform function and then associate that transform function with a transform group. First, we will create a scalar transform function named ADDRESS_TRANSFORM, using the CREATE FUNCTION (SQL Scalar,Table, or Row) statement. In this example, we specify an input parameter named addr of type Address_t. We also specify that the function will return a VARCHAR(42), which is large enough to hold the concatenated address attributes. The SQL-functionbody consists of a RETURN statement, in which the address attributes for an instance of the structured type Address_t are retrieved through their observer methods (specified by the '..' operator) and concatenated (the '||' operator) into a single string to form a mailing address. Before we can use this transform function, we must use the CREATE TRANSFORM statement to associate theADDRESS_TRANSFORM transform function with a group name and a type. The CREATE TRANSFORM statement enables an existing function to be used as a transform function. If you do not specify a group name when you run an application that references a structured type, DB2 tries to use a group name called DB2_PROGRAM and assumes that this group name was defined for the structured type. If DB2_PROGRAM has not already been defined for the structured type, you can create that group for your structured type. We will issue the CREATE TRANSFORM statement, specifying the Address_t structured type and the DB2_PROGRAM group name. The FROM SQL clause defines the specific function that will be used to transform a value to a built-in data type value representing the structured type. The WITH FUNCTION clause specifies the transform function (in this case, ADDRESS_TRANSFORM). We are now ready to insert some values into the CLIENTS table. The VALUES clause of the INSERT statement includes a call to address_t(), which invokes the constructor function for the address_t structured type to create an instance of the type with all attributes set to null values. The double-dot operator invokes mutator methods to set values for each of the address attributes.

www.earnrupees4you.com

You might also like