Oracle 數據庫實現自增加列

SqlServer MySql都有自增加的功能,而Oracle必須接結合sequence序列(通常用於主鍵列),並使用觸發器纔可以實現自動增加,具體步驟以下:html

 

1、            建表數據庫

create table listid number,name varchar2(50);spa

 

 

2、            建立序列orm

 

1.        create sequence list_seqhtm

2.        minvalue 1blog

3.        maxvalue 99999999999999rem

4.        start with 1get

5.        increment by 1it

6.        cache 20table

7.        order;

http://www.finereport.com/forumimages/o-order.jpg

                                  

3、            建立觸發器

1.        Create or replace trigger list_tg

2.        Before insert on list

3.        For each row

4.        Begin

5.        Select list_seq.nextval into :new.id from dual;

6.        End;

http://www.finereport.com/forumimages/o-trigger1.jpg

http://www.finereport.com/forumimages/o-trigger2.jpg

http://www.finereport.com/forumimages/o-trigger3.jpg

4、            插入數據

Insert into list values(’’,’aaa’);

Insert into list values(’’,’bbb’);

Select * from list ;

結果爲:

   ID     name

   1       aaa

   2       bbb

 

Oracle 10g  em 操做界面按照上圖配置便可實現自增加序列,數據庫端設置完成;

在插入數據時自增加字段可由空值’ ’null代替,若是insert 語句中列出了所插入的字段,如insert into list(name) values(’aaa’),則增加字段不用寫 ;如模板所示爲一簡單的表單,ID字段綁定數據庫中的自增加列,BS填報時,id字段不輸入若輸入任意數字時,數據庫中都按照自增加的規律進行賦值,以下圖所示:

 
http://www.finereport.com/forumimages/o-assign1.jpg

http://www.finereport.com/forumimages/o-assign2.jpg

http://www.finereport.com/forumimages/o-assign3.jpg

文章轉自:http://blog.vsharing.com/fanfanzheng/A1463308.html

相關文章
相關標籤/搜索