Trigger / trigger for oracle

2

I need to make a triger / trigger so that when someone inserts a value it is modified and another one is inserted.

That is, I from a web page will insert a list of products with their ID, but the database to be in oracle does not have auto_increment.

So from the web I will always pass the same ID but then in the database with a trigger I want the value of the id to be modified and changed by the last id + 1.

I do not know if it is possible to do this, if someone can give me a hand I would appreciate it, I have never worked with trigeers

    
asked by francisco 20.05.2018 в 20:40
source

1 answer

0

Hey, such a friend!

The ideal would be that you make use of sequences in the database, here I leave you documentation about it.

link

link

And here I leave you documentation of triggers, with an example bringing the current value of a sequence and when doing the insertion the id field would have the following value of the sequence, therefore it would be like a autoincremental.

link

link

Example:

CREATE OR REPLACE TRIGGER nombre_trigger
BEFORE INSERT ON nombre_tabla
FOR EACH ROW
BEGIN
     SELECT nombre_secuencia.nextval INTO :NEW.id
     FROM DUAL;
END

So when you do the insert from the backend do not pass any value to the id, that is, instead of doing this:

INSERT INTO nombre_tabla(id,campo1) VALUES(1,'valor_campo1')

Do this:

INSERT INTO nombre_tabla(campo1) VALUES('valor_campo1')

And already the trigger updates only the id field, as if it were auto-incremental.

I hope you serve, greetings!

    
answered by 21.05.2018 / 02:24
source