Oracle中對象表,ref及deref 對象表的使用

來源:互聯網
上載者:User

Oracle在關聯式資料庫外,融入了物件導向的元素,比如可以建立type,type之間可以繼承,type可以帶建構函式、排序函數、各種各樣的成員函數、預存程序等等。

對象表是指該表的一行就是一個對象,有一個OID(object ID),對象表之間沒有主外部索引鍵關聯的概念,為了體現這層關係,oracle中用了ref對象來實現。

下面例子,建立一個地址類型,一個人員類型,人員有地址屬性,所以在人員類型中設定一個ref address來確定指向他所在地址的指標。

--建立地址類型

create type address as object(
  street varchar2(35),
  city varchar2(15),
  state char(2),
  zip_code integer
);

 

create table addresses of address; --建立地址對象表

 

--建立人員類型
create type person as object(
  first_name varchar2(15),
  last_name varchar2(15),
  birthday date,
  home_address ref address, --指向對應的地址,該地址應該在另外一個對象表中的一行
  phone_number varchar2(15)
);

 

CREATE TABLE persons of person; --建立人員對象表

 

--插入一個地址

insert into addresses values(address('nanhai','shenzhen','gd','518054'));

insert into addresses values(address('shennan','shenzhen','gd','518057'));

--插入一個人員,注意這裡的home_address部分是如何插入一個ref address的。
insert into persons values(person('shitou','haha',to_date('1982-07-05','yyyy-mm-dd'),
 (select ref(a) from addresses a where street='nanhai'),
        '1355555555'));

--也可以用下面的過程來插入一個個人記錄

declare
  addref ref address ;
begin
  select ref(a) into addref from addresses a where street='nanhai';
  insert into persons
    values (person('shitou','haha',to_date('1982-07-05','yyyy-mm-dd'),
                     addref,'1355555555'));
  commit;
end;

--查詢某人的地址資訊
select first_name,deref(home_address) from persons;

--修改地址
update persons set home_address=(select ref(a) from addresses a where street='shennan');

--刪除某個人員

delete from persons where first_name='shitou';

--刪除某個地址的相關個人記錄
delete from persons where home_address=(select ref(a) from addresses a where street='nanhai');

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.