You are on page 1of 12

ASSIGNMENT-2

October 27
Oracle
Object
Creation 2007

Submitted To: Submitted By:


Name:- Name:-Gurpreet Singh
Class:-MCA-III
Roll No.:-613210751
Oracle Object Creation 2007

Object Creation in Oracle


You can extend your relational database to include object-oriented concepts and
structures- you will see an overview of the major object-oriented features (first
introduced in Oracle8) and the impact they have on SQL.

Do I Have to Use Objects?


Just because you use Oracle does not mean you have to use object-oriented
programming (OOP) concepts when implementing your database. In tact, the
database is referred to as an object-relational database management system if
(ORDBMS). The implication for developers is that three different "flavors" of
Oracle are available:

Relational The traditional Oracle relational database management system


(RDBMS), as described in the preceding chapters
Object- The traditional Oracle relational database, extended to include
relational object-oriented concepts and structures such as abstract
datatypes, nested tables, and varying arrays
Object-oriented An object-oriented database whose design is based solely on
object-oriented analysis and design

Oracle provides lull support For all three implementations. If you have previously
used Oracle as a relational database, you can continue to use it in the same
manner. Since the OOP capabilities are extensions to the relational database, you
can select which OOP features you want to use when enhancing existing
relational applications. II you want to redesign and implement your application
using only OOP features, you can also do that. Regardless of the method you
choose, you should first be familiar with the functions and features of the core
Oracle relational database. Even if you plan to use only OOP capabilities, you still
need to know the functions and data types available in Oracle, as well as its
programming languages (SQL and PL/SQL).
LOVELY INSTITUTE OF MANAGEMENT Page 1
Oracle Object Creation 2007

In this chapter, you will seethe basic parts of speech for SQL extended to include
object-relational structures. In the following chapters, you will find detailed
descriptions of Oracle's functions, followed by sections on PL/SQL (Oracle's
procedural programming language), triggers, and procedures. Following the
chapters on PL/SQL and procedures, you will find several chapters on the
implementation of OOP features. You should understand Oracle's functions,
structures, and programming languages before implementing the more advanced
OOP and object-relational structures.

Why Should I Use Objects?


Since you don't have to use objects, should you use them at all? At first, using
OOP features may seem to complicate the design and implementation of your
database systems—just as adding new features to any system may automatically
increase its complexity- OOP adherents claim that objects reduce complexity by
giving you an intuitive way of representing complex data and its relations. For
example, if you want to move a car, you can either move the car (an object), or
you can break it into its components (tires, steering column, and so on), move the
components individually, and then perform a join in the new location. Treating
the car as an object is a more natural way of relating to it and simplifies your
interaction with it.

Besides simplifying your interactions with data, objects may help you in other
ways. In this chapter, you will see examples of three benefits that come from
using OOP features:

Object reuse It you write OOP code, you increase the chances of reusing
previously written code modules. Similarly, if you create OOP database
objects, you increase the chances that the database objects can be reused.
Standards adherence If you create standard objects, you increase the
chance that they will be reused. If multiple applications or tables use the

LOVELY INSTITUTE OF MANAGEMENT Page 2


Oracle Object Creation 2007

same set of database objects, you have created a de facto standard for the
database objects. For example, if you create standard data types to use for
all addresses, then all the addresses in your database will use the same
internal format.
Defined access paths For each object, you can define the procedures and
functions that act upon it—you can unite the data and the methods that
access it. Having the access paths defined in this manner allows you to
standardize the data access methods and enhance the reusability of the
objects.

The costs of using objects are chiefly the added complexity of the system and the
time it takes to learn how to implement the features. But, as you'll see in this
chapter, the basics of extending the Oracle RDBMS to include OOP capabilities
build easily upon the relational model presented in the earlier chapters- The short
time required to develop and use abstract datatypes, as shown later in this
chapter, should help you gauge the time required for learning OOP features.

Everybody Has Objects


Everybody has data, and everybody has methods of interacting with data. Since
the combination of data and methods makes up an object, everybody has objects.
Consider the bookshelf example from the preceding chapters. If you track book
borrowers, each entry will hold basic contact information—name, address, and
perhaps favorite type of book. You will usually have a standard for the structure
of an address—it starts with a person's name, followed by street name, city name,
and state or region. When you add a new person to your list of borrowers, you'll
follow the same process each time. Methods for adding new borrower records
could include the following:

LOVELY INSTITUTE OF MANAGEMENT Page 3


Oracle Object Creation 2007

Add_ Borrower For adding a person lo the list

Update_Borrower For updating a person's entry

Remove_ Borrower For deleting a person from the list

Count_Borrower For counting the number of borrowers per favorite


type of book

As shown by the Count_Borrower method, methods do not have to manipulate


the data; they can report on the data. They can perform functions on the data and
return the result of the function to the user. For example, if you stored borrowers'
birth dates, an Age method could be used to calculate and report their ages.

Oracle supports many different types of objects. In the following sections, you will
see descriptions of the major object types available.

Abstract Datatypes

Abstract datatypes are datatypes that consist of one or more subtypes. Rather
than being constrained to the standard Oracle datatypes of NUMBER, DATE, and
VARCHAR2, abstract datatypes can more accurately describe your data. For
example, an abstract datatype for addresses may consist ol the following
columns:

Street VARCHAR2(50)

City VARCHAR2 (125)

State CHAR(2)

Sip NUMBER(4)

When you create a table that uses address information ,you can create a column
that uses the abstract datatype for addresses—and thus contains the Street, City,
LOVELY INSTITUTE OF MANAGEMENT Page 4
Oracle Object Creation 2007

State, and Zip columns that are part of that datatype- Abstract datatypes can be
nested; they can contain references to other abstract datatypes. You will see a
detailed example of nested abstract datatypes in the next section of this chapter.

Two of the benefits listed earlier for objects—reuse and standards adherence—
are realized from using abstract datatypes. When you create an abstract
datatype, you create a standard for the representation of abstract data elements
(such as addresses, people, or companies). If you use the same abstract datatype
in multiple places, you can be sure that the same logical data is represented in the
same manner in those places.

You can use abstract datatypes to create object tables. In an object table, the
columns of the table map to the columns at the abstract datatype.

Nested Tables
A nested table is a collection of rows, represented as a column within the main
table. For each record within the main table, the nested table may contain
multiple rows. In one sense, it's a way of storing a one-to-many relationship
within one table. Consider a table that contains information about departments,
each of which may have many projects in progress at any one time. In a strictly
relational model, you would create two separate tables—DEPARTMENT and
PROIECT. Nested tables allow you to store the information about projects within
the DEPARTMENT table. The PROJECT table records can be accessed directly via
the DEPARTMENT table, without the need to perform a join. The ability to select
the data without traversing joins may make the data easier to access for users.
Even if you do not define methods for accessing the nested data, you have clearly
associated the department and project data. In a strictly relational model, the
association between the DEPARTMENT and PROJECT tables would be
accomplished via foreign key relationships. For examples of nested tables and
other collection types,

LOVELY INSTITUTE OF MANAGEMENT Page 5


Oracle Object Creation 2007

Large Objects
A large object, or LOB, is capable of storing large volumes of data. The LOB
datatypes available are BLOB. CLOB. NCLOB, and BFILE. The BLOB datatype is used
for binary data, and can extend to 4GB in length. The CLOB datatype stores
character data and can also store up to 4CB. The NCLOB datatype is used to store
CLOB data for multibyte character sets. The data for BLOB,

Thus, you could have a single row that is over 4GB in length.

The files referenced by BFILEs exist on the operating system; the database only
maintains a pointer to the file. The size ol the external file is limited only by the
operating system- Since the data is stored outside the database, Oracle does not
maintain the concurrency or integrity of the data.

You can use multiple LOBs per table. For example, you could have a table with a
CLOB column and two BLOB columns. This is an improvement over the LONG
datatype, since you can only have one LONG per table. Oracle provides a number
of functions and procedures to manipulate and select LOB data. Part IV contains
implementation details for LOBs.

Naming Conventions for Objects


It included a discussion of the naming standards for tables and columns. In
general, you should use common, descriptive words for table and column names.
For the OOP examples in this book, the following rules will be enforced:

 Table and column names are singular (such as EMPLOYEE, Name, and
State)
 Abstract datatype names are singular nouns with an _TY suffix (such
as PERSONTY or ADDRESSJT)

LOVELY INSTITUTE OF MANAGEMENT Page 6


Oracle Object Creation 2007

 Table and datatype names are always uppercase (such as EMPLOYEE


or PERSON TY)
 Column names are always capitalized (such as State and StartDate)
 Object view names are singular nouns with an _OV suffix (such as
PERSON OV or ADDRESSJDV)
 Nested table names are plural nouns with an NT suffix (such as
WORKERS NT)
 Varying array names are plural nouns with an _VA suffix (such as
WORKERS VA)

The name of an object should consist of two parts: the core object name and the
suffix. The core object name should follow your naming standards; the suffixes
help to identify special types of objects.

A Common Object Example


Let's consider an object found in most systems: addresses. Even in a system as
simple as the bookshelf system, addresses are maintained and selected. These
addresses follow a standard format—the street address, city, and state or region-
You can use this standard format as the basis for an abstract datatype for
addresses. First, expand it to include additional address information, such as the
Zip code. Next, use the create type command to create an abstract datatype:

create type ADDRESSJTY as object

(Street VARCHAR2(50) ,

City VARCHAR2(25) ,

State CHAR(2),

Zip NUMBER);

The create type command is the most important command in object-relational


databases. What the command in this example says is "create an abstract

LOVELY INSTITUTE OF MANAGEMENT Page 7


Oracle Object Creation 2007

datatype named ADDRESSTY. It will be represented as having four attributes,


named Street, City, State, and Zip, using the defined datatypes and lengths for
each column." So far, no methods have been created by the user—but the
database has internally created methods that will be used whenever the
ADDRESSTY object is accessed. For information on creating your own methods,
see Part IV and the create type body command entry in the Alphabetical
Reference.

Within the create type command, the as object clause explicitly identifies
ADDRESSTY as an OOP implementation.

Now that the ADDRESSTY datatype exists, you can use it within other datatypes.
For example, you may decide to create a standard datatype for people. People
have names and addresses. Therefore, you can create the following type:

create type PBRSON_TY as object

(Name VARCHAR2(25),

Addreos ADDRESSJTY) ;

What did this command do? First, the datatype was given a name—PERSON TY—
and identified as an object via the as object clause- Then, two columns were
defined. This line,

Based on these definitions, a PERSON_TY entry will have Name, Street, City, State,
and Zip columns—even though only one of those columns is explicitly defined
within the PERSON TY type definition.

You can imagine how this capability to define and reuse abstract datatypes can
simplify data representation within your database. For example, a Street column
is seldom used by itself; it is almost always used as part of an address. Abstract
datatypes allow you to join elements together and deal with the whole—the

LOVELY INSTITUTE OF MANAGEMENT Page 8


Oracle Object Creation 2007

address—instead of the parts—the Street and other columns that constitute the
address.

You can now use the PERSONTY datatype in the process of table creation.

Inserting Records into CUSTOMER


Oracle creates methods, called constructor methods, for data management when
you create an abstract datatype. A constructor method is a program that is
named after the datatype; its parameters are the names of the attributes defined
for the datatype. When you need to insert records into a table based on abstract
datatypes, you use the constructor methods. For example, the CUSTOMER table
uses the PERSON_TY datatype, and the PERSONJTY datatype uses the ADDRESSTY
datatype. To insert a record into the CUSTOMER table, you need to insert a record
using the PERSON_TY and ADDRESS_TY datatypes' constructor methods.

In the following example, a record is inserted into CUSTOMER using the


constructor methods for the PERSON_TY and ADDRESS_TY datatypes- The
constructor methods for these datatypes are shown in bold in the example; they
have the same names as the datatypes:

insert into CUSTOMER values (1, PERSON_TY('NEIL MULLANE ,

ADDRESS_TY( 57 MT PLEASANT ST',

FINN', 'NH', 11111)));

1 row created.

The insert command provides the values to be inserted as a row in the


CUSTOMER table. The values provided must match the datatypes of the columns
in the table.

LOVELY INSTITUTE OF MANAGEMENT Page 9


Oracle Object Creation 2007

In this example, a CustomerJD value of 1 is specified- Next, the values for the
Person column are inserted, using the PERSONTY constructor method (shown in
bold). Within the PERSON JT datatype, a Name is specified, and then the
ADDRESS_TY constructor method is used to insert the Address values. So, for the
record inserted in the example, the Name value is 'NEIL MULLANE', and the Street
value is '57 MT PLEASANT ST. Note that the parameters for the constructor
method are in the same order as the attributes of the datatype.

A second record can be inserted into CUSTOMER, using the exact same format for
the calls to the constructor methods (again shown in bold):

LOVELY INSTITUTE OF MANAGEMENT Page 10


Oracle Object Creation 2007

Table of Content

Object Creation in Oracle ....................................................................................... 0


Do I Have to Use Objects?...................................................................................... 1
Why Should I Use Objects? .................................................................................... 2
Everybody Has Objects .......................................................................................... 3
Nested Tables ........................................................................................................ 5
Large Objects ......................................................................................................... 6
Naming Conventions for Objects ........................................................................... 6
A Common Object Example ................................................................................... 7
Inserting Records into CUSTOMER ......................................................................... 9

LOVELY INSTITUTE OF MANAGEMENT Page 11

You might also like