Creation and use of Oracle triggers and sequences (auto-Grow column)

Source: Internet
Author: User

--Create sequence creation sequence sequence name start with 1--Start value increment by 1--increment maxvalue 99999999--Max Nocycle-If the maximum value is recalculated, the current is not recalculated and cycle is recalculated nocache; --do not cache, easy to jump number

--Create TRIGGER OR REPLACE TRIGGER trigger name before INSERT on table name for each ROW BEGIN SELECT sequence name. Nextval into:new. field name from DUAL; END;

Note: You need to build a sequence first, and then build the corresponding trigger. It's best to build each table like this one. Each time the record is inserted, it is automatically interpolated into the identity column.

Test code:

?
1234567891011121314151617181920 --创建表create table t_windturbine_day(id number primary key,createdate nvarchar2(25),windturbinecode varchar(5),indicode number(5),indivalue varchar2(10),passflage INTEGER default(0) );--创建序列create sequence seq_windturbine_day increment by 1 start with 1000000 nomaxvalue nominvalue nocache ;--创建触发器create or replace trigger tr_windturbine_day   before insert on t_windturbine_day   for each rowbegin   select seq_windturbine_day.nextval into :new.id from dual; end;--插入数据insert into t_windturbine_day(createdate,windturbinecode,indicode,indivalue)values(‘20131108‘,18,10002,‘3.95‘);--通过这种方式插入,步长为1,但是id一次长2(第3条数)。为什么?我认为是触发器增长了1,--seq_windturbine_day.nextval又增长了1所导致的。不知道对不对?insert into t_windturbine_day(id,createdate,windturbinecode,indicode,indivalue)values(seq_windturbine_day.nextval,‘20131108‘,18,10002,‘3.95‘);--查询数据select * from t_windturbine_day t;

Test results:

 

Creation and use of Oracle triggers and sequences (auto-Grow column)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.