Setting up MySQL trigger syntax correctly and carefully -


i've been doing lot of new learning mysql , triggers. think understand concept , realise there lot of possible dangers in using them. believe limited use of them correct function want perform.

i have 9 tables correspond 9 different web based ajax engined forms. i've worked hard on these, being first time using ajax, , i'm reasonably happy them. each time user makes change whichever form filling out, change ajaxed db , confirmation or error response. straight forward. each forms respective table has "status" field, "lastmodified" field , field call "agref" sort of status null until form reaches stage, further along process.

i have additional table called "records" entries in of other tables, listed can see forms have been started, when last changes made , status's have. here believe trigger part should work, don't have make updates "records" table in php on every single transaction.

the "records" table set out this:

`uaid` int(11) not null auto_increment, `uid` int(11) not null, `appno` int(11) not null, `applicationkey` varchar(8) not null, `appid` int(11) default null, `applicationname` varchar(64) not null, `agref` varchar(32) default null, `status` varchar(32) not null, `datestarted` int(11) not null, `lastmodified` int(11) not null, 

now of these fields populated @ same time matching entry inserted ever 1 of other 9 tables form connects to. small example of 1 of other 9 tables this:

`appid` int(11) not null auto_increment, `uid` int(11) not null, `uaid` int(11) not null, `status` varchar(32) not null default 'data acquisition', `agref` varchar(32) default null, `groupname` varchar(64) default null, `shorttitle` varchar(64) default null, `recipient` varchar(64) default null, `partofvalch` varchar(64) default null, `sector` varchar(64) default null, `subsector` varchar(64) default null, `topic` varchar(64) default null,    <snip because can go on lot of lines> `datestarted` int(11) not null, `lastmodified` int(11) not null, 

agref on both tables remain null now, appid null on records table @ point of creation updated corresponding entry made second table, generated auto increment , call made records table insert appid there.

the 3 things change of data tables 3 fields "status", "agref", "lastmodified".

so i'm trying create trigger after each alteration/update data table, data in records table consistent , accurate.

this first ever trigger set attempt:

delimiter $$ create trigger `datatableone_to_records_sync` after update on `datatableone` each row begin update records (agref, status, lastmodified) values (new.agref, new.status, new.lastmodified) appid = old.appid; end$$ delimiter ; 

i trying set through phpmyadmin, returning error telling me have syntax problem within update line. feel issue part - appid 1 common element ties row in "records" row being updated/changed in "datatableone". how set correctly? error more serious, , running risk of creating huge mess, never ending loop? i'm bit paranoid doing first time. in advance , advice.

update have tried few other trigger attempts although mysql accept them being valid trigger syntax, seem break entire db functionality. can me trigger syntax work correctly? in demo tables above, if second table gets updated @ all, want 3 fields copied on first table trigger. 3 values want copied across "status", "agref", , "lastmodified".

my recent failed attempt this:

create trigger aigltinq_sync after insert on app_aigltinq each row update records r set r.agref        = new.agref   , r.status       = new.status   , r.lastmodified = new.lastmodified uaid = new.uaid; 

i'm not @ familiar form of update statement.

to change values of columns in rows, we'd typically write update statement this:

 update records r     set r.agref        = new.agref       , r.status       = new.status       , r.lastmodified = new.lastmodified   r.appid        = old.appid 

reference: https://dev.mysql.com/doc/refman/5.5/en/update.html


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -