I have the tables:
In%with%Ihavecolumnanuncios
thatreceivesthenumberofdaysthatthatadisvalid,Iwanteverydaythatpassesthedatabasetoautomaticallydecreaseexpiracao
inthatcolumn,inacountdowntodeactivatetheannouncement.Andwhenitreaches0,itcreatesarecordinthe1
tablewithexpiracao
expiracao.supermercado
.Isitpossibletodothiswithroutines?Ihavenevermovedwithroutines,ItriedtosearchbutIdidnotunderstand,Ihopetheydonotclosethequestion.
Withtheanswerfrom@bigownIresearchedandunderstoodabit,Ibootedatestdatabaselikethis:
//Notificationstable,replacestableexpiration - -Tablestructureanuncios.supermecado
-
CREATETABLEIFNOTEXISTS'notificacao'('id'int(11)NOTNULLAUTO_INCREMENT,'idteste'int(11)NOTNULL,'mensagem'varchar(20)NOTNULL,PRIMARYKEY('id'))ENGINE=InnoDBDEFAULTCHARSET=latin1AUTO_INCREMENT=1;----------------------------------------------------------
//TestTable,replacetableads
----Estruturadatabela'teste'--CREATETABLEIFNOTEXISTS'teste'('id'int(11)NOTNULLAUTO_INCREMENT,'expiracao'int(11)NOTNULL,'nome'varchar(10)NOTNULL,PRIMARYKEY('id'))ENGINE=InnoDBDEFAULTCHARSET=latin1AUTO_INCREMENT=5;----Extraindodadosdatabela'teste'--INSERTINTO'teste'('id','expiracao','nome')VALUES(1,1,'Teste01'),(2,99,'Teste02'),(3,149,'Teste03'),(4,199,'Teste04');
//Triggerofthetesttable,whenadataisupdated,itlooksatwhethertheexpirycolumnis0,ifitdoes,itdeletestherecordandinsertsadataintotheothertable
----Acionadores'teste'(TRIGGER)--DROPTRIGGERIFEXISTS'geraNotificacao';DELIMITER//CREATETRIGGER'geraNotificacao'BEFOREUPDATEON'teste'FOREACHROWBEGINIFteste.expiracao=0THENINSERTINTOnotificacao('idteste','mensagem')VALUES(teste.id,"Teste apagado!");
DELETE FROM teste WHERE teste.id = OLD.id;
END IF;
END
//
DELIMITER ;
// Event that every 10 seconds decrements 1 expiration
DELIMITER $$
--
-- Eventos
--
CREATE DEFINER='root'@'localhost' EVENT 'decrementaDia'
ON SCHEDULE EVERY 10 SECOND
STARTS '2016-05-12 15:11:10' ON COMPLETION NOT PRESERVE ENABLE
DO UPDATE teste SET teste.expiracao = (teste.expiracao - 1) WHERE 1$$
DELIMITER ;
When I run script nothing happens, the test table does not decrement the expiration value, and it is also impossible to manually edit the value of it.