oracle数据库,插入数据

时间:2018-03-16 12:58:59

标签: database oracle user-defined-types

我正在使用Oracle 11g表:

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

我想使用引用函数

将管理程序插入数据

例如: for head(jone smith)是一名主管或经理(罗斯史密斯小姐),

经理(罗斯史密斯小姐)是账户主管(詹恩史密斯先生),

谢谢!

1 个答案:

答案 0 :(得分:1)

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

要填充REF子句,您需要选择相关对象的引用。你的第一条记录没有主管,所以我们在这种情况下传递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)。 (令人遗憾的是甲骨文的开发人员云无法很好地处理用户定义的类型。)