Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
329 views
in Technique[技术] by (71.8m points)

sql - oracle autoincrement with sequence and trigger is not working correctly

here is my problemI have this code to make an autoincrement variable in oracle database:

CREATE TABLE Korisnici
    (
        id_korisnika number PRIMARY KEY,
        ime_korisnika varchar2(200),
        prezime_korisnika varchar2(200),
        broj_telefona varchar2(30),
        adresa_korisnika varchar2(400)
    )
    /

create sequence test_seq
start with 1 
increment by 1;

CREATE OR REPLACE TRIGGER test_trigger
BEFORE INSERT ON Korisnici FOR EACH ROW
BEGIN
  SELECT test_seq.NEXTVAL
  INTO :NEW.id_korisnika
  FROM DUAL;
END;
/

If I start from begining everything works great, I have numbers as 1,2,3,4.... I close the program, open it again, so oracle database connection is once again started. I add one more input and I have numbers like 20,21,22,23... I put program on my android and connect from different device, when I input one user I have 30,31,33,34...

Why is this happening? And how to fix it?

Thank you

EDIT:

Here is my proc for reading data from database

CREATE OR REPLACE PROCEDURE Citanje_korisnika( p_rc OUT SYS_REFCURSOR )
AS
BEGIN
  OPEN p_rc
   FOR SELECT *
         FROM Korisnici;
END;

I am a bit of newbie in oracle database.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Specifying the SEQUENCE with NOCACHE will stop a session caching 20 numbers at a time and help.

create sequence test_seq
start with 1 
increment by 1
NOCACHE;

However, if you're hoping for a completely contiguous sequence this is very difficult to achieve - numbers taken from the sequence are "lost" if (for example) an insert is rolled back.


Based on your comment, I wonder if you're forgetting to COMMIT?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...