|
|
wyq@ORCL>create type User_ty as object(
2 User_ID VARCHAR2(20) ,
3 Name VARCHAR2(10)
4 )
5 /
Type created.
wyq@ORCL>create table USERS
2 of USER_TY
3 tablespace users
4 /
Table created.
wyq@ORCL>Create table Document
2 ( docid number not null,
3 user_ref ref user_ty not null
4 )
5 /
Table created.
为了演示ref的用法,下面作了一些修改,最后触发器的作用是将当前用户指针指向的用户的名字填入表的新加的一个字段中。
wyq@ORCL>alter table document add username varchar2(10);
Table altered.
wyq@ORCL>create or replace trigger document_ai
2 Before insert on document
3 for each Row
4 Declare
5 l_User User_Ty;
6 Begin
7 Select Deref(:New.User_Ref) Into l_User From Dual;
8 Select Us.Name
9 Into :New.Username
10 From Users Us
11 Where Us.User_Id = l_User.User_Id;
12 End;
13 /
Trigger created.
wyq@ORCL>insert into users values (100,'TEST');
1 row created.
wyq@ORCL>commit;
Commit complete.
wyq@ORCL>Insert Into Document
2 (Docid, User_Ref)
3 Select 10001, Ref(u) From Users u Where User_Id = 100;
1 row created.
wyq@ORCL>select * from document;
DOCID
----------
USER_REF
----------------------------------------------------------------------------------------------------
USERNAME
----------
10001
00002202081187DB3BDF204FDE8458832E6137DC83E309C616090D466EBF1AE42837DB6426
TEST |
|