Object Tables

An object type can be used to define an entire table. Such a table is known as an object table.


CREATE TYPE t_emp AS OBJECT (
   id INTEGER,
   name VARCHAR2(10),
   sal NUMBER(5, 2),
   hiredate DATE,
   MEMBER FUNCTION get_hire_date RETURN DATE
);
/

CREATE TYPE BODY t_emp AS
   MEMBER FUNCTION get_hire_date RETURN DATE IS
      v_date DATE;
   BEGIN
      SELECT hiredate INTO v_date FROM dual;
      RETURN v_date;
   END;
END;
/


SQL> CREATE TABLE emp OF t_emp;

Table created.

SQL>

Use a constructor to insert data


SQL> INSERT INTO emp VALUES (t_emp(1, 'Peter', 395,'17-DEC-1980'));

1 row created.

SQL>

Or to insert the values in the same way with a relational table. 

SQL> INSERT INTO emp(id, name, sal, hiredate) VALUES (2, 'Jack', 299,'17-DEC-1980');

1 row created.

SQL>
SQL>
SQL> SELECT * FROM emp;

        ID NAME              SAL HIREDATE
---------- ---------- ---------- ---------
         1 Peter             395 17-DEC-80
         2 Jack              299 17-DEC-80

SQL>
SQL>

Select specify individual object attributes in a query:

SQL> SELECT id, name, sal FROM emp op WHERE id = 1;

        ID NAME              SAL
---------- ---------- ----------
         1 Peter             395

SQL>
Home »
Oracle »
PL/SQL » 

Object Types:
  1. Creating Object Types
  2. A type with member function:
  3. Using DESCRIBE to Get Information on Object Types
  4. Using Object Types in Database Tables
  5. Retrieve an individual column object from a table
  6. Call method from type
  7. UPDATE/DELETE row based on custom data type
  8. Object Tables
  9. VALUE() selects a row from an object table.
  10. UPDATE Object Table
  11. DELETE rows from Object Table
  12. Object table abased on nested types
  13. Object Identifiers and Object References
  14. REF type for an object reference
  15. Retrieve the actual objects stored in an object reference using the DEREF() function,
  16. Comparing Object Values
Related: