sql - Too many rows are inserted into Audit Table by Triggers I created -


i want create history records in table "sampletableaudit" insert, update , delete transactions happen records on table "sampletable"

this work fine if had not need of trigger adds uniqueidentifier each inserted record on "sampletable".

the updates made trigger updates every new record after being inserted in sampletable new uniqueidentifier "sampletableuid" should not stored each executed step of trigger "sampletableaudit" final new record including uniqieidentifier. @ moment 3 steps triggering insert mechanism.

create table "sampletable" ( "sampletableid" integer primary key autoincrement  not null, "sampletableuid" nvarchar, "name" nvarchar(50), "city" nvarchar, "datecreated" datetime default current_timestamp, "datemodified" datetime, "usernameid" integer );  create table "sampletableaudit" ( "sampletableauditid" integer primary key autoincrement  not null, "sampletableid" integer, "sampletableuid" nvarchar, "name" nvarchar(50), "city" nvarchar, "datecreated" datetime, "datemodified" datetime, "usernameid" integer );  create table "username" ( "usernameid" integer primary key autoincrement  not null, "sampletableuid" nvarchar, "username" nvarchar, "fullname" nvarchar );  create trigger "uid" after insert on "sampletable" each row begin update "sampletable" set "sampletableuid" = (select substr(u,1,8)||'-'||substr(u,9,4)||'-4'||substr(u,13,3)||   '-'||v||substr(u,17,3)||'-'||substr(u,21,12) (     select lower(hex(randomblob(16))) u, substr('89ab',random() % 4 + 1, 1) v)) rowid = last_insert_rowid(); end;  create trigger "modifiedupdate" after update on "sampletable" each row begin insert  sampletableaudit ("sampletableid","sampletableuid","name","city","modifiedtype","datemodified","usernameid",) values  (old.sampletableid,old.sampletableuid,old.name,old.city,"update",current_timestamp,old.usernameid); end;  create trigger "modifiedinsert" after insert on "sampletable" each row begin insert  sampletableaudit ("sampletableid","sampletableuid","name","city","modifiedtype","datemodified","usernameid",) values  (new.sampletableid,new.sampletableuid,new.name,new.city,"insert",current_timestamp,new.usernameid); end;  create trigger "modifieddelete" before delete on "sampletable" each row begin insert  sampletableaudit ("sampletableid","sampletableuid","name","city","modifiedtype","datemodified","usernameid",) values  (old.sampletableid,old.sampletableuid,old.name,old.city,"delete",current_timestamp,old.usernameid); end;  insert "sampletable" ("name", "city") values ("russel","dallas"); insert "sampletable" ("name", "city") values ("jones","seattle"); insert "sampletable" ("name", "city") values ("mciver","san diego"); insert "sampletable" ("name", "city") values ("mendez","boston"); insert "sampletable" ("name", "city") values ("roberts","miami"); insert "sampletable" ("name", "city") values ("yong","new york");  update "sampletable" set city = 'new york' name = "roberts";  delete "sampletable" sampletableid = 2; 

the modifiedupdate trigger should not run update sets unique id. prevent running, add when clause checks case (when old.sampletableuid not null).

the modifiedinsert trigger not see new unique id in new values, wrong place add history record. instead, want add history record when new id has been set. therefore, add update trigger opposite check in when clause.

note: using last_insert_rowid() in insert trigger dangerous; use new.rowid.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -