Oracle8i SQL Reference Release 3 (8.1.7) Part Number A85397-01 |
|
SQL Statements:
CREATE SYNONYM to DROP ROLLBACK SEGMENT, 7 of 31
Use the CREATE
TYPE
statement to create the specification of an object type, named varying array (varray), nested table type, or an incomplete object type. You create object types with the CREATE
TYPE
and the CREATE
TYPE
BODY
statements. The CREATE
TYPE
statement specifies the name of the object type, its attributes, methods, and other properties. The CREATE
TYPE
BODY
statement contains the code for the methods in the type.
Oracle implicitly defines a constructor method for each user-defined type that you create. A constructor is a system-supplied procedure that is used in SQL statements or in PL/SQL code to construct an instance of the type value. The name of the constructor method is the same as the name of the user-defined type.
The parameters of the object type constructor method are the data attributes of the object type. They occur in the same order as the attribute definition order for the object type. The parameters of a nested table or varray constructor are the elements of the nested table or the varray.
An incomplete type is a type created by a forward type definition. It is called "incomplete" because it has a name but no attributes or methods. It can be referenced by other types, and so can be used to define types that refer to each other. However, you must fully specify the type before you can use it to create a table or an object column or a column of a nested table type.
See Also:
|
To create a type in your own schema, you must have the CREATE
TYPE
system privilege. To create a type in another user's schema, you must have the CREATE
ANY
TYPE
system privilege. You can acquire these privileges explicitly or be granted them through a role.
The owner of the type must either be explicitly granted the EXECUTE
object privilege in order to access all other types referenced within the definition of the type, or the type owner must be granted the EXECUTE
ANY
TYPE
system privilege. The owner cannot obtain these privileges through roles.
If the type owner intends to grant other users access to the type, the owner must be granted the EXECUTE
object privilege to the referenced types with the GRANT
OPTION
or the EXECUTE
ANY
TYPE
system privilege with the ADMIN
OPTION
. Otherwise, the type owner has insufficient privileges to grant access on the type to other users.
create_incomplete_type
::=
element_list
::=
procedure_spec or function_spec::=
Java_declaration
::=
C_declaration
::=
OR
REPLACE
Specify OR
REPLACE
to re-create the type if it already exists. Use this clause to change the definition of an existing type without first dropping it.
Users previously granted privileges on the re-created object type can use and reference the object type without being granted privileges again.
If any function-based indexes depend on the type, Oracle marks the indexes DISABLED
.
schema
Specify the schema to contain the type. If you omit schema
, Oracle creates the type in your current schema.
type_name
Specify the name of an object type, a nested table type, or a varray type.
If creating the type results in compilation errors, Oracle returns an error. You can see the associated compiler error messages with the SQL*Plus command SHOW
ERRORS
.
create_object_type
Use the create_object_type
clause to create a user-defined object type (rather than an incomplete type). The variables that form the data structure are called attributes. The member subprograms that define the object's behavior are called methods. AS
OBJECT
is required when creating an object type.
invoker_rights_clause
The invoker_rights_clause
lets you specify whether the member functions and procedures of the object type execute with the privileges and in the schema of the user who owns the object type or with the privileges and in the schema of CURRENT_USER
. This specification applies to the corresponding type body as well.
This clause also determines how Oracle resolves external names in queries, DML operations, and dynamic SQL statements in the member functions and procedures of the type.
Restriction: You can specify this clause only for an object type, not for a nested table or varray type.
See Also:
|
element_list
|
Specify the attribute's Oracle built-in datatype or user-defined type. Restrictions:
|
|
|
Specify, for an object type, the name of an object attribute. Attributes are data items with a name and a type specifier that form the structure of the object. You must specify at least one attribute for each object type. |
|
|
Specify a function or procedure subprogram associated with the object type that is referenced as an attribute. Typically, you invoke member methods in a "selfish" style, such as |
|
|
Specify a function or procedure subprogram associated with the object type. Unlike member methods, static methods do not have any implicit parameters (that is, |
|
For both member and static methods, you must specify a corresponding method body in the object type body for each procedure or function specification. |
||
The
If this subprogram does not include the declaration of the procedure or function, you must issue a corresponding
|
||
|
Specify the call specification ("call spec") that maps a Java or C method name, parameter types, and return type to their SQL counterparts. If all the member methods in the type have been defined in this clause, you need not issue a corresponding |
|
|
In
|
|
|
The |
|
|
The |
|
|
|
Specify the name of the |
|
|
Specify |
|
|
Specify |
|
|
Specify |
|
|
Specify |
|
|
Specify |
|
|
|
|
This clause lets you specify a member function ( |
|
|
If the argument to the map method is null, the map method returns null and the method is not invoked. |
|
|
An object specification can contain only one map method, which must be a function. The result type must be a predefined SQL scalar type, and the map function can have no arguments other than the implicit SELF argument. |
|
|
||
|
This clause lets you specify a member function ( |
|
|
If either argument to the order method is null, the order method returns null and the method is not invoked.
When instances of the same object type definition are compared in an |
|
|
An object specification can contain only one |
|
You can define either a |
||
If neither a |
||
Use
|
create_varray_type
The create_varray_type
lets you create the type as an ordered set of elements, each of which has the same datatype. You must specify a name and a maximum limit of zero or more. The array limit must be an integer literal. Oracle does not support anonymous varrays.
The type name for the objects contained in the varray must be one of the following:
Restrictions:
create_nested_table_type
The create_nested_table_type
lets you create a named nested table of type datatype
.
datatype
is an object type, the nested table type describes a table whose columns match the name and attributes of the object type.
datatype
is a scalar type, then the nested table type describes a table with a single, scalar type column called "column_value
".
Restrictions:
NCLOB
for datatype
. However, you can specify CLOB
or BLOB
.
The following example creates object type person_t
with LOB attributes:
CREATE TYPE person_t AS OBJECT (name CHAR(20), resume CLOB, picture BLOB);
The following statement creates members_type
as a varray type with 100 elements:
CREATE TYPE members_type AS VARRAY(100) OF CHAR(5);
The following example creates a named table type project_table
of object type project_t
:
CREATE TYPE project_t AS OBJECT (pno CHAR(5), pname CHAR(20), budgets DEC(7,2)); CREATE TYPE project_table AS TABLE OF project_t;
The following example invokes method constructor col.getbar()
:
CREATE TYPE foo AS OBJECT (a1 NUMBER, MEMBER FUNCTION getbar RETURN NUMBER,); CREATE TABLE footab(col foo); SELECT col.getbar() FROM footab;
Unlike function invocations, method invocations require parentheses, even when the methods do not have additional arguments.
The next example invokes the system-defined constructor to construct the foo_t
object and insert it into the foo_tab
table:
CREATE TYPE foo_t AS OBJECT (a1 NUMBER, a2 NUMBER); CREATE TABLE foo_tab (b1 NUMBER, b2 foo_t); INSERT INTO foo_tab VALUES (1, foo_t(2,3));
See Also: Oracle8i Application Developer's Guide - Fundamentals and PL/SQL User's Guide and Reference for more information about constructors |
The following example changes the definition of the employee_t
type to associate it with the construct_emp
function:
CREATE OR REPLACE TYPE employee_t AS OBJECT( empid RAW(16), ename CHAR(31), dept REF department_t, STATIC function construct_emp (name VARCHAR2, dept REF department_t) RETURN employee_t );
This statement requires the following type body statement (PL/SQL is shown in italics):
CREATE OR REPLACE TYPE BODY employee_t IS STATIC FUNCTION construct_emp (name varchar2, dept REF department_t) RETURN employee_t IS BEGIN return employee_t(SYS_GUID(),name,dept); END; END;
This type and type body definition allows the following operation:
INSERT INTO emptab VALUES (employee_t.construct_emp('John Smith', NULL));
|
![]() Copyright © 1996-2000, Oracle Corporation. All Rights Reserved. |
|