oracle数据库,插入数据

oracle database, insert data

我正在使用 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)
)
/

我插入的数据

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')
)    

我想通过引用函数向数据中插入主管,

例如: 负责人 (jone smith) 是主管或经理 (miss ross smith),

经理(罗斯·史密斯小姐)是客户主管(简·史密斯先生),

谢谢!

您正在插入 employee 类型的记录:适用于 整个记录 因此您需要编写一个匹配该类型投影的 VALUES 子句。

要填充 REF 子句,您需要 select 相关对象的引用。您的第一条记录没有主管,因此在这种情况下我们传递 NULL:

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
    ))
/

对于其他记录,我们使用 INSERT ... SELECT ... FROM 语法:

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' 
/ 

这里是a Oracle LiveSQL demo (free OTN account required)。 (遗憾的是 Oracle 的开发人员云无法很好地处理 user-defined 类型。)