Doubt use of Sequence - Oracle

Asked

Viewed 288 times

2

I have a table on oracle 11g which in turn has a

CREATE TABLE "BANCO"."TEXTO" 
    ("TEXTOID" NUMBER NOT NULL ENABLE, 
    "NOME" VARCHAR2(4000 BYTE) NOT NULL ENABLE, 
    "VALOR" VARCHAR2(4000 BYTE) NOT NULL ENABLE, 
    "DATACADASTRO" DATE NOT NULL ENABLE, 
    "DATAATUALIZACAO" DATE NOT NULL ENABLE, 
     CONSTRAINT "TEXTO_PK" PRIMARY KEY ("TEXTOID"))

CREATE SEQUENCE  "BANCO"."TEXTO_SEQUENCE"  MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 41 CACHE 20 NOORDER  NOCYCLE ;

And I have an insert script sent by our supplier:

INSERT INTO Texto
  (nome,valor,datacadastro,dataatualizacao,textoid)
VALUES
  ('texto1','valor1',sysdate,sysdate,(SELECT MAX(textoid)+1 FROM texto));

If I add items this way in a script, will I be breaking the sequence? Taking into account that the table does not have a trigger to enter the sequence value in the record id!

1 answer

3


So you won’t be using the SEQUENCE. You must add to the command INSERT as follows:

INSERT INTO Texto
  (nome,valor,datacadastro,dataatualizacao,textoid)
VALUES
  ('texto1','valor1',sysdate,sysdate,texto_sequence.nextval);

The great advantage over the max mentioned is that the SEQUENCE is atomic, guaranteeing new unique values, even in competition. In the case of max, you would have competition issues in a possible rollback or time-consuming commit, where another user could take the same id.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.