Creating Object type and usage : Example-I
You can create object types with the
bkd@Oracle>CREATE OR REPLACE TYPE Studentobj as object (
2 id number,
3 dob date,
4 member function getage return number);
5 /
Type created.
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. bkd@Oracle>CREATE OR REPLACE TYPE Studentobj as object (
2 id number,
3 dob date,
4 member function getage return number);
5 /
Type created.
If the a type contains member functions/procedures, the the procedural work needs to be defined in the TYPE BODY(similar to create specification and package BODY)
bkd@Oracle>ed
Wrote file afiedt.buf
1 create or replace type body studentobj as
2 member function getage return number as /* subprogram associated with the object type that is referenced as an attribute*/
3 begin
4 return trunc(months_between(sysdate , dob)/12);
5 end;
6* end;
bkd@Oracle>/
Type body created.
bkd@Oracle>create table student
2 ( roll number,
3 studentinf studentobj);
Table created.
bkd@Oracle>insert into student values( 1, studentobj( 123,sysdate-365*5));
1 row created.
bkd@Oracle>select p.id ,
p.studentinf.roll,
p.studentinf.dob,
p.studentinf.getage() /* method constructor studentinf.getage()*/
from student p;
ID STUDENTINF.ROLL STUDENTIN P.STUDENTINF.GETAGE()
---------- ------------- --------- ------------------------------------------------------------------
1 123 25-AUG-07 4
from student p;
ID STUDENTINF.ROLL STUDENTIN P.STUDENTINF.GETAGE()
---------- ------------- --------- ------------------------------------------------------------------
1 123 25-AUG-07 4
Regards
BKD