1建立数据表
Code
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->create table Test_Increase(
userid number(10) primary key, /*主键,自动增加*/
username varchar2(20)
);
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->create table Test_Increase(
userid number(10) primary key, /*主键,自动增加*/
username varchar2(20)
);
2创建自动增长序列
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> CREATE SEQUENCE TestIncrease_Sequence
INCREMENT BY 1 -- 每次加几个
START WITH 1 -- 从1开始计数
NOMAXVALUE -- 不设置最大值
NOCYCLE -- 一直累加,不循环
CACHE 10 ;
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> CREATE SEQUENCE TestIncrease_Sequence
INCREMENT BY 1 -- 每次加几个
START WITH 1 -- 从1开始计数
NOMAXVALUE -- 不设置最大值
NOCYCLE -- 一直累加,不循环
CACHE 10 ;
3创建触发器
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> CREATE TRIGGER Test_Increase BEFORE
insert ON Test_Increase FOR EACH ROW
begin
select TestIncrease_Sequence.nextval into :New.userid from dual;
end ;
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> CREATE TRIGGER Test_Increase BEFORE
insert ON Test_Increase FOR EACH ROW
begin
select TestIncrease_Sequence.nextval into :New.userid from dual;
end ;
4 提交
commit;
5 测试
反复执行如下语句:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> insert into Test_Increase(Username) values ( ' test ' )
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> insert into Test_Increase(Username) values ( ' test ' )
6 查看插入结果:
userid username
1 test
2 test
3 test
4 test
5 test
6 test
7 test
8 test
9 test
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26481450/viewspace-731191/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/26481450/viewspace-731191/