In Oracle Database, objects and types are powerful features of the Object-Relational Model, allowing you to define complex data structures and store them in tables much like you would in object-oriented programming.
An object in Oracle is an instance of a user-defined object type. Think of it like an object in Java or C++ — it has attributes (data) and methods (behavior).
CREATE OR REPLACE TYPE employee_type AS OBJECT (
emp_id NUMBER,
emp_name VARCHAR2(100),
emp_salary NUMBER,
MEMBER FUNCTION annual_salary RETURN NUMBER
);
CREATE OR REPLACE TYPE BODY employee_type AS
MEMBER FUNCTION annual_salary RETURN NUMBER IS
BEGIN
RETURN emp_salary * 12;
END;
END;
Once a type is defined, you can use it as a column in a table:
CREATE TABLE employee_table (
emp_info employee_type
);
Or you can use it in PL/SQL variables:
DECLARE
emp employee_type;
BEGIN
emp := employee_type(1, 'Alice', 5000);
DBMS_OUTPUT.PUT_LINE(emp.annual_salary);
END;
Oracle has built-in types (e.g., NUMBER
, VARCHAR2
) and user-defined types, which include:
Type | Description |
---|---|
OBJECT types | Structured records with fields and methods. |
VARRAY types | Arrays of a fixed max size. |
Nested tables | Like SQL tables stored inside rows. |
REF types | References (like pointers) to objects in tables. |
CREATE TYPE num_array AS VARRAY(5) OF NUMBER;
CREATE TYPE string_table AS TABLE OF VARCHAR2(50);
Customer
, Address
.Feature | Relational Table | Object Type |
---|---|---|
Data Structure | Flat (columns) | Hierarchical/nested |
Methods | Not supported | Supported (via member functions) |
Reusability | Limited | High |
Storage | Tables | Tables or variables |
Let me know if you’d like real-world schema examples using Oracle Objects and Types!