Jimma University JIT School of Computing Advanced Database System Lab
Jimma University JIT School of Computing Advanced Database System Lab
Jimma University JIT School of Computing Advanced Database System Lab
JIT
School Of Computing
Advanced Database System
Lab
1
SQL Object Types
This section describes SQL object types including:
creating Objects types
Create, Insert, select ,delete
Type evaluations
Collection types
Object Methods
2
Object Types
An object type is a kind of data type.
We can use it in the same ways as number or
varchar2,integer.
3
CREATE TYPE
CREATE TYPE statement is used to create the specification
of an object type, a SQLJ object type, varray, a nested table
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 that implement the type.
4
Examples
Create OR Replace Type<Type_Name> AS Object
(<Member variables declarations>,
<Member function | procedure declarations>
);
/
Examples:
Create OR Replace Type PointType AS Object(
x integer,
y integer
);
/
5
Con
Create table PointTab(
Pid varchar(10),
Pvalue pointType);
Now it’s possible to apply OQL commands
Insert, select, update and delete, modify
Insert into pointTab1 values(‘P1’,pointType(3,4));
Insert into pointTab1 values(‘P2’,pointType(5,2));
Select * from pointTab1;
6
Notes
For insert and update statement we must use the name
of user defined data type.
To update, select or delete an individual value from
collections, we must use the name iterator of the user
defined data type.<iterator.column_name>
p.pvalue.x
p.pvalue.y
p.pid
Select p.* from pointTab p;
Select p.pvalue.x from pointTab p;
7
Con
Select p.pvalue.y from pointTab1 p;
Select p.pvalue.x from pointTab1 p;
8
Update commands
Update pointTab1 p set p.pvalue.x =7 where
p.pvalue.x=3;
9
Delete commands
Delete from pointTab1 p where p.pvalue.x=5;
Delete from pointTab1 p where p.pid=‘p2’;
10
Example2
11
Con
12
Con
13
Object Table
If a table is completely made up of an object type
(class) then it is called an object table.
All the records in the object table are called row
object.
Example
Create or replace type customer_typ as object(
Custid varchar(5),
Custname varchar(12),
Custphone number(10));
/
Create table custTab of customer_typ;
Insert into custTab values(customer_typ(‘c1’,’xxx’,0911));
14
Collection types
15
Varrays
collection.
16
Varray
To define a varray, we specify the maximum number of
elements it can contain.
17
Varray is used
To store only a fixed number of items,
To loop through the elements in order,
To retrieve and manipulate the entire collection as a value.
Syntax
Create or Replace type Varray_name AS VARRAY(n) OF
datatype; /
Example:
Create or replace type email_typ as varray(3) of
varchar(20);
/
18
Example 2
Create or replace type phone_typ as object (phone
number(10));
/
CREATE TYPE phone_list AS VARRAY(5) OF
phone_typ; /
/
Create table stud(
stid number(4),
stname varchar(12),
Stphone phone_list );
19
Insert records
Insert into stud values(10,’xxx’, phone_list
(phone_typ(0910), phone_typ(0911),phone_typ(0912),
phone_typ(0913),phone_typ(0914)));
20
Nested Tables
A nested table is an unordered set of data elements, all of the
same datatype.
No upper limit
steps
1. Create object type
2. Create a nested list using table keyword
3. Implement the nested list in the nested table
21
Example
Create or replace type phone_type as object(
phone number(10));
/
Create or replace type phone_nests as table of
phone_type;
/
Create table customers_phone(
Cusid integer,
Custname varchar(12),
Custphone phone_nests ) nested table Custphone store as
phone_num ;
22
Insert into customers_phone values(1,’dawod’,
phone_nests(phone_type(0911), phone_type(0913),
phone_type(0917)));
Insert into customers_phone values(2,sol’,
phone_nests(phone_type(0911), phone_type(0913)));
SQL> select * from customers_phone;
SQL> select s.custphone from customers_phone s;
CUSTPHONE(PHONE)
--------------------------------------------------------------------------------
PHONE_NESTS(PHONE_TYPE(911), PHONE_TYPE(913),
PHONE_TYPE(917))
PHONE_NESTS(PHONE_TYPE(911), PHONE_TYPE(913))
23
Select i.* from table(select p.custphone from
customers_phone p where p.cusid=1)i;
Value(i)=phone_type(0911);
24
Example Two
25
Object Methods
Map Methods
Order Methods
Static Methods
Constructor Methods
26
Methods
Methods are functions or procedures that can declare in an
object type definition to implement behavior of objects type.
Member Methods
Member methods are used for manipulating the attributes of
the object.
27
The object body defines the code for the member
methods.
The object body is created using the CREATE TYPE
BODY statement.
Constructors are functions that return a new object
as its value.
Every object has a system defined constructor method.
The name of the constructor is same as the object type.
28
Example1:
Create or replace type rectangle as object(
rid integer,
Length integer,
Width integer,
Map member function area return integer,
Member procedure display);
/
29
Create or replace type body rectangle as map member function area return
integer
Is
Begin
Return length* width;
End;
Member procedure display
Is
Begin
dbms_output.put_line(‘Rec_id is’||rid);
dbms_output.put_line(‘Rec_len is’||length);
dbms_output.put_line(‘Rec_wid is’||width);
End;
End;
30 /
Con…
Create table rect1 (
Rect_desc varchar(12),
Rect_details rectangle);
Create table rect2 of rectangle;
Insert into rect1 values(‘small’, rectangle(1,3,5));
Insert into rect2 values(2,5,8);
Select r.area() from rect2 r where r.rid=2;
Select r.rect_details.area() from rect1 r where r.rid=1;
31
Con…
Declare
m rectangle;
i integer;
Begin
i:=&i;
Select value(c) into m from rect2 c where c.rid=i;
m.display;
End;
/
32
Con…
Declare
m rectangle;
i integer;
Begin
i:=&i;
Select r.rect_details into m from rect1 r where
r.rect_details.rid=i;
m.display;
End;
/
33
Create or replace type squares as object (length integer,
Member Function squareArea return integer);
/
Create or replace type body squares as member function squareArea return integer
Is
area integer;
Begin
area :=length*length;
Return area ;
End;
End;
/
Create table sqr of squares ;
Insert into sqr values(4);
Select c.squareArea () from sqr c;
34
comparison methods
The comparison methods are used for comparing objects.
36
Examples
37
38
Type Evolution
Changing a object type is called type evolution.
You can make the following changes to an object type:
Add and drop attributes
Add and drop methods
Modify a numeric attribute to increase its length, precision, or
scale
Modify a varying length character attribute to increase its
length
Change a type's FINAL and INSTANTIABLE properties
Modify limit and size of VARRAYs
Modify length, precision, and scale of collection elements
39
Example
Create or replace type person_type as object(
Pid integer,
pname varchar(12);
/
Alter type person_type add attribute (phone number(10))
cascade not including table data;
Alter type person_type drop attribute pname cascade not
including table data;
Alter type person_type modify attribute (addr
varchar(30))cascade not including table data;
Modify is used only to increase the length of an attribute.
40
Con...
Create table person of person_type;
41
Type evaluation in collections
Create or replace type email as varray(2) of
varchar(20);
/
Alter type email modify element type varchar(50)
cascade;
Alter type email modify limit 6 cascade;
43
By default, the CASCADE option converts the data.
In any case, table data is always returned in the format of
the latest type version.
You can use the INVALIDATE option to drop a method that
has been redefined, but the redefined versions in the
subtypes must still be dropped manually.
The subtypes will remain in an invalid state until they are
explicitly altered to drop the redefined versions.
Until then, an attempt to recompile the subtypes for
revalidation will produce the error Method does not
override
44
Modifying the FINAL or INSTANTIABLE Property
Altering an object type from INSTANTIABLE to NOT
INSTANTIABLE is allowed only if the type has no table
dependents.
Altering an object type from NOT INSTANTIABLE to
INSTANTIABLE is allowed anytime.
46
Con…
By default, altering a type from FINAL to NOT FINAL
enables you to create new substitutable tables and
columns of that type, but it does not automatically
make existing columns (or object tables) of that type
substitutable. In fact, just the opposite happens:
existing columns and tables of the type are marked
NOT SUBSTITUTABLE AT ALL LEVELS.
47
If any embedded attribute of such a column is
substitutable, an error is generated.
48
ALTER TYPE Statement for Type Evolution
ALTER TYPE Options for Type Evolution
INVALIDATE Invalidates all dependent objects. Table data cannot be
accessed again until it is validated; if it cannot be
validated, it remains inaccessible.
NOT INCLUDING Leaves column data as is, associated with the current type
TABLE DATA version.
If an attribute is dropped from a type referenced by a table,
then the corresponding column of the dropped attribute is
not removed from the table.
Primary key
Unique
Check
54
Examples
SQL> create or replace type stud as object(
2 sid integer,
3 sname varchar(12),
4 age integer,
5 phone number(10)
6 );
7 /
Type created.
SQL> create table new_stud of stud(sid primary key);
Table created.
SQL> create table gradstud(
2 stdetail stud,
3 dname varchar(12),
4 class integer,
5 constraint c1 unique(stdetail.phone),
6 constraint c2 check(stdetail.age>20),
7 constraint c3 check(stdetail.sname is not null)
8 );
Table created.
References
A REF is a logical pointer to a row object that is
constructed from the object identifier (OID) of
the referenced object.
It is an Oracle built-in datatype.
Many -to-one relationships, thus reducing the need for
foreign keys.
56
Example
CREATE TYPE emp_person_typ AS OBJECT (
name VARCHAR2(30),
manager REF emp_person_typ );
/
CREATE TABLE emp_person_obj_table OF
emp_person_typ;
INSERT INTO emp_person_obj_table VALUES (
emp_person_typ ('John Smith', NULL));
INSERT INTO emp_person_obj_table
SELECT emp_person_typ ('Bob Jones', REF(e))
FROM emp_person_obj_table e
57
WHERE e.name = 'John Smith';
Ref
SQL> create or replace type emp as object(
2 eid integer,
3 ename varchar(12),
4 hod ref emp
5 );
6 /
Type created.
SQL> create table empl of emp;
Table created.
SQL> insert into empl values(1,'Sara',null);
1 row created.
SQL> insert into empl select emp(2,'semira',ref(e)) from empl e where e.ename='Sara';
1 row created.
SQL> insert into empl select emp(3,'dawod',ref(e)) from empl e where e.ename='Sara';
1 row created.
SQL> select * from empl;
EID ENAME HOD
---------- ------------- ----------
1 Sara
2 semira 000022020891D...
3 dawod 000022020891D3...
Inheritance in Object Type
Inheritance property allows the sub-object type to access
all the attribute and members of the super object type or
parent object type.
59
Types and Subtypes
A subtype can be derived from a supertype either
directly, or indirectly through intervening levels of
other subtypes.
subtype can directly derive only from a single
supertype.
It cannot derive jointly from more than one.
A supertype can have multiple sibling subtypes, but a
subtype can have at most one direct parent supertype.
60
In other words, Oracle supports only single
inheritance, not multiple inheritance.
A subtype is derived from a supertype by defining a
specialized variant of the supertype.
The below syntax shows the how to create parent and
inherited object type.
Example shape type
61
62
Examples
Create or Replace type shape_type as object(
shape_id integer,
shapeName varchar(12),
Member function area return integer) Not final;
/
create or replace type circle_type under shape_type(
radius integer,
overriding member function area return integer)
Not final;
/
63
Con…
Create or replace type sphere_type under circle_type (
height integer,
member function volume return integer);
/
Create or replace type body circle as overriding member
function area return integer
Is
Begin
Return radius*radius*3.14;
End;
End;
/
64
Con…
Create table shape of shape_type;
Insert into shape values(shape_type(1,’shape1’));
Insert into shape values(circle_type(2,’circle’,4));
Insert into shape values(sphere_type(3,’sphere’,4,4));
65
Steps
We are going to execute the above program in the
following steps
Step1: Create SUPER type.
Step2: Create SUB type and body.
Step3: Creating an anonymous block to call the SUB type.
66
Examples
SQL> create or replace type emp_object as object(
emp_no number,
emp_name varchar2(12),
salary number,
manager number,
constructor function emp_object(p_emp_no number,
p_emp_name varchar,p_salary number ) return self as result,
member procedure insert_records,
member procedure display_records) not final;
/
Type created.
67
Step2: Create SUB type and body.
SQL> create or replace type sub_emp under emp_object(
2 default_manager number,member procedure insert_manager
);
3 /
Type created.
SQL> create table emp(
2 emp_no number(10),
3 emp_name varchar(12),
4 salary number(10,3),
5 manager number(10));
Table created.
68
Step3: Creating an anonymous block to call the SUB type.
69
SQL> declare
2 emp_desc sub_emp;
3 begin
4 emp_desc:=sub_emp(12,'Dawod',7200,10,5);
5 emp_desc.insert_manager;
6 commit;
7 end;
8 /
PL/SQL procedure successfully completed.
70