SqlServer 和MySql都有自增長(zhǎng)的功能,而Oracle必須接結(jié)合sequence序列(一般用于主鍵列),并使用觸發(fā)器才能夠?qū)崿F(xiàn)自動(dòng)增長(zhǎng),具體步驟如下:
一、 建表
create table list(id number,name varchar2(50));
二、 創(chuàng)建序列
1. create sequence list_seq
2. minvalue 1
3. maxvalue 99999999999999
4. start with 1
5. increment by 1
6. cache 20
7. order;

http://www.finereport.com/forumimages/o-order.jpg
三、 創(chuàng)建觸發(fā)器
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
四、 插入數(shù)據(jù)
Insert into list values(’’,’aaa’);
Insert into list values(’’,’bbb’);
Select * from list ;
結(jié)果為:
ID name
1 aaa
2 bbb
在Oracle 10g 的em 操作界面按照上圖配置即可實(shí)現(xiàn)自增長(zhǎng)序列,數(shù)據(jù)庫(kù)端設(shè)置完成;
在插入數(shù)據(jù)時(shí)自增長(zhǎng)字段可由空值’ ’或null代替,如果insert 語(yǔ)句中列出了所插入的字段,如insert into list(name) values(’aaa’),則增長(zhǎng)字段不用寫(xiě) ;如模板所示為一簡(jiǎn)單的表單,ID字段綁定數(shù)據(jù)庫(kù)中的自增長(zhǎng)列,BS填報(bào)時(shí),id字段不輸入若輸入任意數(shù)字時(shí),數(shù)據(jù)庫(kù)中都按照自增長(zhǎng)的規(guī)律進(jìn)行賦值,如下圖所示:
http://www.finereport.com/forumimages/o-assign1.jpg

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

http://www.finereport.com/forumimages/o-assign3.jpg
文章轉(zhuǎn)自:http://blog.vsharing.com/fanfanzheng/A1463308.html