- Explain object types, objects, attributes and methods.
- Create object types and type bodies, use constructors and SELF.
- Use object types as columns (column objects) and access attributes/methods from SQL and PL/SQL.
- Understand type dependencies, object tables, OIDs, REF and DEREF.
- Enable object comparison with MAP or ORDER member functions.
Introduction to Object Types
An object type is a user-defined composite datatype that contains the data and code for an entity – used to model real-world entities such as a bank account or a customer address. It consists of attributes (data) and methods (operations on the data). An instance of an object type is an object. The type is only a template and occupies no space.
CREATE [OR REPLACE] TYPE type_name AS OBJECT ( attribute datatype [, attribute datatype] ... [, MEMBER {PROCEDURE | FUNCTION} specification] ... [, {MAP | ORDER} MEMBER FUNCTION specification] );create or replace type project_type as object
( name varchar2(20),
stdate date );
/
create table emp
( empno number(5),
ename varchar2(20),
project project_type ); -- column object
insert into emp values (1, 'Larry', project_type('Billing system', '12-dec-2000'));
select project from emp; -- PROJECT_TYPE('Billing system', '12-DEC-00')- An object type can define: columns of relational tables, PL/SQL variables, subprogram parameters, attributes of another object type (nested objects) and object tables.
- Oracle creates a constructor – a procedure with the same name as the type – that must be given one value per attribute (give NULL explicitly when needed).
- A column whose type is an object type is a column object. DESCRIBE shows a type’s structure; USER_TYPES and USER_TYPE_ATTRS describe types.
- Only the owner can use a type unless he grants EXECUTE on it; others then use
srikanth.project_type.
Methods and SELF
Methods are declared in CREATE TYPE with MEMBER and defined in CREATE TYPE BODY (needed only when the type has methods). Inside a method, attributes are used without a qualifier because each method implicitly receives a reference to the invoking object, called SELF (stdate = self.stdate).
create or replace type project_type as object
( name varchar2(20),
stdate date,
member function getage return number,
member procedure change_stdate (newstdate date) );
/
create or replace type body project_type is
member function getage return number is
begin
return sysdate - self.stdate;
end;
member procedure change_stdate (newstdate date) is
begin
if newstdate > sysdate then
raise_application_error(-20011, 'Invalid starting date');
else
stdate := newstdate;
end if;
end;
end;
/
select e.project.name, e.project.getage() from emp e; -- alias is required
update emp e set e.project.name = 'offers.com' where empno = 1;select project.name from emp fails with ORA-00904, while select e.project.name from emp e works.Dependencies, Object Tables and References
- A table with a column of an object type, or a type with an attribute of another type, depends on that type. A type with dependents cannot be dropped (ORA-02303: cannot drop or replace a type with type or table dependents) unless DROP TYPE … FORCE is used, which makes dependents invalid.
- An object table is a table whose every row is an object (row object); the type’s attributes become its columns, so it can be used like a relational table and can have constraints.
- Each row object has an object identifier (OID) – system-generated or derived from the primary key (
OBJECT ID PRIMARY KEY). - A REF attribute stores a reference to a row object; REF(alias) obtains it and DEREF returns the object it points to.
create table projects of project_type ( name primary key );
insert into projects values ( project_type('Billing System', sysdate) );
insert into projects values ( 'Hotel Management', '10-dec-2001' ); -- without constructor
update projects set stdate = sysdate where name = 'Billing System';
create type emp_type as object
( empno number(5),
ename varchar2(20),
project ref project_type );
/
create table emp_obt of emp_type;
insert into emp_obt
select 1, 'Praneeth', ref(p) from projects p where p.name = 'Billing System';
select empno, ename, deref(project) from emp_obt;Objects in PL/SQL
declare
proj project_type;
begin
proj := project_type('seconds.com', '12-jun-2001'); -- constructor
dbms_output.put_line( proj.getage() );
end;MAP and ORDER Member Functions
Objects can be compared with >, < etc. (and used in ORDER BY) only if the type has a MAP or an ORDER member function. Without either, only equality/inequality comparisons are possible. A type may have one or the other, not both.
| MAP MEMBER function | ORDER MEMBER function |
|---|---|
| Maps each instance to a value of a scalar type (e.g. returns STDATE); called implicitly to order instances | Takes another instance as an explicit argument and returns a negative, zero or positive number (SELF less than, equal to or greater than it) |
map member function map_stdate return date | order member function ord_function(obj project_type) return number – only one per type, return type NUMBER |
With either function, select ename from emp order by project; sorts employees by their project’s starting date.
- Object type = attributes + methods; object = instance; constructor has the type’s name.
- Methods are defined in CREATE TYPE BODY and reach attributes through SELF.
- Use a table alias to access object attributes and methods in SQL.
- Object tables store row objects with OIDs; REF/DEREF link objects; MAP/ORDER enable comparison.
Chapter 22 Notes (PDF)
Question / Answer (15 questions)
Moderate-level questions based on the Chapter 22 notes (PDF). Click a question to see the answer.
MCQ Test : Check Your Understanding (5 questions)
Select an option and press Check Answer, or answer all questions and press Submit Test to see your score. Each answer comes with an explanation.
Bibliography/References
The chapter notes PDF is the course study material and is credited to its author below. The explanations, questions and MCQs on this page are original study aids written for this course from the topics covered in that PDF. The other works are cited for verification and further reading. Oracle, SQL*Plus and PL/SQL are trademarks of Oracle Corporation; other names belong to their respective owners.
- Srikanth, P. (n.d.). Oracle For Beginners (chapter notes – the PDF shown on each chapter page). Srikanth Technologies. http://www.srikanthtechnologies.com/
- Oracle Corporation. (n.d.). Oracle Database Object-Relational Developer’s Guide, 19c. https://docs.oracle.com/en/database/oracle/oracle-database/19/adobj/
- Oracle Corporation. (n.d.). Oracle Database PL/SQL Language Reference, 19c. https://docs.oracle.com/en/database/oracle/oracle-database/19/lnpls/
- Oracle Corporation. (n.d.). Oracle Database SQL Language Reference, 19c. https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/