ORACLE数据库中通过触发器实现自动增加

1083阅读 0评论2012-11-26 hui_unix
分类:

1、在scott用户下建立表dept(其实oracle缺省安装就已经生成了)  
create   table   DEPT  
(  
    DEPTNO   NUMBER(2)   not   null,  
    DNAME   VARCHAR2(14),  
    LOC   VARCHAR2(13)  
)  
2、建立一个sequence  
create   sequence   seq_dept  
minvalue   1  
start   with   1  
increment   by   1  
nocache;  
3、为dept表建立一个insert   trigger  
create   or   replace   trigger   insert_dept  
    before   insert   on   dept      
    for   each   row  
declare  
    --   local   variables   here  
begin  
      select   seq_dept.nextval   into   :new.deptno   from   dual;  
end   insert_dept;  
4、用insert语句测试  
insert   into   dept(dname,loc)   values( 'aaaaaa ', 'bbbbb ');  
用select   *   from   dept察看结果
上一篇:Oracle数据库常见错误
下一篇:VI 快速上手