I'm using Oracle 11g table:
create or replace type address as object (
street varchar2(20),
city varchar2(10),
p_code varchar2(8)
) not final;
/
create or replace type name as object (
title varchar2(5),
firstName varchar2(8),
surname varchar2(8)
) not final;
/
create or replace type phone as object
(
homePhone int,
mobile1 int,
mobile2 int
) not final;
/
create or replace type person as object (
pname name,
pAddress address,
Pphone phone
) not final;
/
create or replace type employee under person (
empId varchar2(5),
position varchar2(16),
salary int,
joinDate date,
supervisor ref employee);
/
create table tb_employee of employee
(
primary key(empID)
)
/
data I insert
insert into tb_employee values
(
person(name('mr','jone','smith'),address('street','city','post
code'),phone('11111111111','22222222222','33333333333')),
position('head'),
salary(1111),
joinDate(12-Feb-1994),
empID('001')
)
insert into tb_employee values
(
person(name('mr','jane','smith'),address('street','city','post
code'),phone('11111111111','22222222222','33333333333')),
position('accountant'),
salary(2222,
joinDate(13-Feb-1995),
empID('002')
)
insert into tb_employee values
(
person(name('miss','ross','smith'),address('street','city','post
code'),phone('11111111111','22222222222','33333333333')),
position(manager),
salary(333),
joinDate(14-Feb-1996),
empID('003')
)
I would like to insert supervisor to data by using reference function,
for example: for head (jone smith) is a supervisor or a manager (miss ross smith),
manager(miss ross smith) is a supervisor of account(Mr jane smith),
thanks!
You are inserting records of employee
type: that applies to the whole record so you need to write a VALUES clause which matches the projection of that type.
To populate the REF clause you need to select the reference of the pertinent object. Your first record doesn't have a supervisor, so we pass NULL in this case:
insert into tb_employee values
( employee(
name('mr','jone','smith')
, address('street','city','postcode')
, phone('11111111111','22222222222','33333333333')
, '001' -- emp id
, 'head' -- position
, 11111 -- salary
, to_date('12-Feb-1994','dd-mon-yyyy') -- joinDate
, null-- supervisor
))
/
For the other records we use the INSERT ... SELECT ... FROM syntax:
insert into tb_employee
select
employee(
name('mr','jane','smith')
, address('street','city','postcode')
, phone('11111111111','22222222222','33333333333')
, '002' -- emp id
, 'accountant' -- position
, 2222 -- salary
, to_date('13-Feb-1995','dd-mon-yyyy') -- joinDate
, ref (m) -- supervisor
)
from tb_employee m
where m.empid = '001'
/
insert into tb_employee
select
employee(
name('miss','ross','smith')
, address('street','city','postcode')
, phone('11111111111','22222222222','33333333333')
, '003' -- emp id
, 'manager' -- position
, 333 -- salary
, to_date('14-Feb-1996','dd-mon-yyyy') -- joinDate
, ref (m) -- supervisor
)
from tb_employee m
where m.empid = '002'
/
Here is a Oracle LiveSQL demo (free OTN account required). (It's a shame that Oracle's developer Cloud can't handle user-defined types nicely.)