Using Object Types in Database Tables

A constructor is a built-in method for the object type and we use it to insert data to the table.


SQL> CREATE TYPE t_emp AS OBJECT (
  2     id INTEGER,
  3     name VARCHAR2(10),
  4     sal NUMBER(5, 2),
  5     hiredate DATE,
  6     MEMBER FUNCTION get_hire_date RETURN DATE
  7  );
  8  /

Type created.

SQL>
SQL> CREATE TYPE BODY t_emp AS
  2     MEMBER FUNCTION get_hire_date RETURN DATE IS
  3        v_date DATE;
  4     BEGIN
  5        SELECT hiredate INTO v_date FROM dual;
  6        RETURN v_date;
  7     END;
  8  END;
  9  /

Type body created.

SQL>
SQL> CREATE TABLE emp(
  2     emp t_emp,
  3     dept INTEGER
  4  );

Table created.

SQL>


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

1 row created.

SQL>

SQL> SELECT * FROM emp;

EMP(ID, NAME, SAL, HIREDATE)      DEPT
--------------------------------------
T_EMP(1, 'Peter', 20, '17-DEC-80')  50


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: