码迷,mamicode.com
首页 > 数据库 > 详细

oracle 12c之前用sequence 和 trigger来生成自动增长的列

时间:2014-07-29 21:12:42      阅读:302      评论:0      收藏:0      [点我收藏+]

标签:for   art   ar   new   sql   oracle   table   har   

SQL> create table scott.t1 (id number, val varchar2(8));

Table created.

SQL> CREATE SEQUENCE scott.t1_id_seq INCREMENT BY 1 START WITH 1 NOMAXVALUE NOCYCLE ;

Sequence created.

 

SQL> create or replace trigger scott.t1_id_trigger
2 before insert on scott.t1 for each row
3 begin
4 select scott.t1_id_seq.nextval into :new.id from dual;
5 end;
6 /

Trigger created.

 

SQL> insert into scott.t1(val) values(‘a‘);

1 row created.

SQL> commit;

Commit complete.

SQL> select * from scott.t1;

ID VAL
---------- ------------------------
1 a

SQL> insert into scott.t1(val) values(‘b‘);

1 row created.

SQL> select * from scott.t1;

ID VAL
---------- ------------------------
1 a
2 b

SQL> commit;

Commit complete.

oracle 12c之前用sequence 和 trigger来生成自动增长的列,布布扣,bubuko.com

oracle 12c之前用sequence 和 trigger来生成自动增长的列

标签:for   art   ar   new   sql   oracle   table   har   

原文地址:http://www.cnblogs.com/kramer/p/3876016.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!