1

Is there a more performant way to copy the autoincrement field value to another field after row insert? I'd like to make it automatic with triggers, but I haven't enough knowledge with them to be sure of my code.

this answer (for mysql) doesn't count for it is in the query, and i don't know if it would work with multiple rows

this is the table:

CREATE TABLE IF NOT EXISTS commenti ( 
  id INTEGER PRIMARY KEY AUTOINCREMENT, 
  content TEXT COLLATE NOCASE, 
  idcommento INTEGER REFERENCES commenti (id) 
    ON DELETE CASCADE ON UPDATE CASCADE);

and this is the trigger i'm using: it copies id (the autoincrement field) into idcommento only if the value is null in the query.

CREATE TRIGGER set_comment_to_self AFTER INSERT ON commenti
WHEN NEW.idcommento IS NULL 
BEGIN
  UPDATE commenti SET idcommento = NEW.id WHERE id = NEW.id;
END;

I don't know if I can use some sort of easy NEW.idcommento = NEW.id istead of searching every time in all the table...

Community
  • 1
  • 1
Nereo Costacurta
  • 7,693
  • 4
  • 21
  • 27

0 Answers0