4.1. Defining Object Types

In this section, you will learn how to define object types. The examples here create object types that will be used to store the details of a customer and an address. The address type will be embedded within the customer type, enabling a customer's address to be stored as an attribute of the customer. These types form the basis for all the other examples in this chapter.

When defining an object type, you must define a specification. The specification defines the attributes for the object type. If your object type will contain methods, then the signatures for those methods must also be included in the specification. A method signature is made up of the name of the method and any parameters that will be passed to the method. In addition, if your object type contains methods, you must define a body for the type. The body contains the actual code for the methods.

You use the SQL DDL statements CREATE TYPE and CREATE TYPE BODY to create an object type specification and body. The following CREATE TYPE statement defines the specification for an object type named t_address:

CREATE TYPE t_address AS OBJECT (
  street VARCHAR2(15),
  city   VARCHAR2(15),
  state  CHAR(2),
  zip    VARCHAR2(9)
);
/

Because the t_address type will not contain any methods, there's no need to create an object body for the type. A body is necessary only when you need to specify code for a method.

In this chapter's examples, the t_customer object type will be used to store customer details. Unlike ...

Get Java Programming with Oracle SQLJ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.