How to create auto update trigger for MySQL DB? - mysql

I've got table with products (changes dynamically) and table for data export. I need a trigger which once a day will add new products from table products into table export. Mine code is:
CREATE TRIGGER auto_update
BEFORE UPDATE
FOR EACH ROW
SET export.name = product.prod_name, export.link=product.prod_link, export.price=product.prod_price
WHERE product.prod_type='product';
But this code don't works. How to fix it? Any ideas?

Since you want this code to execute on scheduled basis, you may want to use the Event Scheduler instead.
Please see here: https://dev.mysql.com/doc/refman/5.7/en/event-scheduler.html
From this you can do things like:
CREATE EVENT e_store_ts
ON SCHEDULE
EVERY 10 SECOND
DO
INSERT INTO myschema.mytable VALUES (UNIX_TIMESTAMP());
This will create a new event to execute every 10 seconds and insert a unix time stamp into table mytable. You can alter events with ALTER and you can drop them with DROP. Have fun!
So in your case create an event that executes every day, that performs the expression you wish to execute (DO clause).

Related

update 2 table, each table in diferent database using event schedule

I have 2 databases, one of them is log, I want to make an event schedule in the main db but write a log in the db of logs
mainDB (event schedule doing something in mainDB and writing log in LOGDB)
LOGDB
I just don't know how to record data from one db event to another db
could someone tell me an example?
That is quite wage,
but you can do this
USE mainDB;
DELIMITER $$
CREATE EVENT e_daily
ON SCHEDULE
EVERY 1 DAY
COMMENT 'explain here what has to be done each day'
DO
BEGIN
DELETE FROM mainDB.mytable WHERE ID > 10;
INSERT INTO LOGDB.mytable (time, total)
VALUES (NOW(),10);
END $$
DELIMITER ;
CEATE EVENT has some Restrictions that has to be observed.
The different schemas/Databses are addressed by writing the name of the database before a table name and add a dot like mainDB.mytable
The correct syntax of your queries should be tested, before starting an event.
Usually you make during testing, that it runs once or twice before ending, so that you can check the result.

Perform delete a perticular records after some interval of time in mysql

DROP EVENT `deleteTestEntries`;
CREATE DEFINER=`root`#`localhost` EVENT `deleteTestEntries`
ON SCHEDULE EVERY 1 MINUTE
STARTS '2018-05-25 18:17:01'
ON COMPLETION NOT PRESERVE ENABLE DO
DELETE FROM lead_master WHERE lead_master.lname LIKE '%Test%'
What is wrong in above event.
It has been created with no errors but not performing any action.
I simply want to delete the records from my lead_master table where lname is 'Test'
Go into my.ini file and add this line,most probably this is the issue.
event_scheduler = on
Restart mysql.Apparently you can even set it on the fly
I finally did it with cron jobs.
I created a controller which calls a model function contains query to delete record.
and set the cron job which calls the controller after specific interval of time.
I am guessing that you don't need an event every minute. Just define a view to filter out the records you don't want:
create view v_lead_master as
select lm.*
from lead_master lm
where lm.lname not like '%Test%';
Then schedule a job or event every day at a slow period to delete the rows you want. Your application should use the view. Your testing should use the base table.

MySQL - Force DROP TABLE in EVENT without confirm

In MySQL phpmyadmin environment, when i create an event in which a DROP TABLE statement is set to occur, DROP TABLE (silently) does not occur, because my MySQL install, apparently, expects a confirmation.
DROP EVENT IF EXISTS test.target_ultra_sync ;
CREATE EVENT test.target_ultra_sync
ON SCHEDULE EVERY 30 SECOND
DO
DROP TABLE IF EXISTS `test`.`ultra`;
CREATE TABLE `test`.`ultra` AS SELECT * FROM `target_ultra`;
The same occurs with TRUNCATE and DELETE.
How can i suppress this behavior?
I don't get any prompts in phpMyAdmin from the event scheduler when I try a variant of your event.
It appears that your event syntax is off just a bit (I had to apply a minor modification), as you have compound statements after the DO that needs a BEGIN and END section like so:
DROP EVENT IF EXISTS test.target_ultra_sync ;
CREATE EVENT test.target_ultra_sync
ON SCHEDULE EVERY 30 SECOND
DO
BEGIN
DROP TABLE IF EXISTS `test`.`ultra`;
CREATE TABLE `test`.`ultra` AS SELECT * FROM `target_ultra`;
END
After that event change is applied, you can then check that the Executed_events variable is counting (roughly twice a minute based on your 30 second setting) by running this SQL command:
show status WHERE variable_name = 'Executed_events'
After you've run that query once in phpMyAdmin, you can simply refresh the query to get updated results as shown in the image below.
Of course, if you have other scheduled events it will be hard to know if the counts are strictly from this event alone, unless the others have a longer time that do not run as frequently.
Should you find that the Executed_events count is NOT increasing after creating the event, ensure that the event scheduler is running! This can be checked in phpMyAdmin here:

Using mysql event scheduler and php

I would like to delete records from my database every 2 minutes.
I have a user table where I would like to delete users who are active after 2 minutes. I have read a little about using mysql event scheduler but unsure if I can achieve it?
wanted to ask if anybody has previously done anything similar who could help me start ?
You Can Create An Event SCHEDULE on your Mysql Server
First thing You must switch the event SCHEDULE to on that is because its Always off by default Run this Sql Query
SET GLOBAL event_scheduler = ON;
After That You can create an Event Schedule to delete your records from the table every 2 min You can use a query like this
DELIMITER $$
CREATE EVENT IF NOT EXISTS EventName
ON SCHEDULE EVERY 2 MINUTE
DO
BEGIN
DELETE FROM Your Table WHERE Your Conditions if Exists;
END$$
DELIMITER ;
This Event will automatically deletes your specific records every 2 min

UPDATE Same Row After UPDATE in Trigger

I want the epc column to always be earnings/clicks. I am using an AFTER UPDATE trigger to accomplish this. So if I were to add 100 clicks to this table, I would want the EPC to update automatically.
I am trying this:
CREATE TRIGGER `records_integrity` AFTER UPDATE ON `records` FOR EACH ROW SET
NEW.epc=IFNULL(earnings/clicks,0);
And getting this error:
MySQL said: #1362 - Updating of NEW row is not allowed in after trigger
I tried using OLD as well but also got an error. I could do BEFORE but then if I added 100 clicks it would use the previous # clicks for the trigger (right?)
What should I do to accomplish this?
EDIT - An example of a query that would be run on this:
UPDATE records SET clicks=clicks+100
//EPC should update automatically
You can't update rows in the table in an after update trigger.
Perhaps you want something like this:
CREATE TRIGGER `records_integrity` BEFORE UPDATE
ON `records`
FOR EACH ROW
SET NEW.epc=IFNULL(new.earnings/new.clicks, 0);
EDIT:
Inside a trigger, you have have access to OLD and NEW. OLD are the old values in the record and NEW are the new values. In a before trigger, the NEW values are what get written to the table, so you can modify them. In an after trigger, the NEW values have already been written, so they cannot be modified. I think the MySQL documentation explains this pretty well.
Perhaps you could write two separate statements in that transaction
update record set clicks=...
update record set epc=...
or you could put them inside a function, say updateClick() and just call that function. By doing it this way you can easily alter your logic should the need arise.
Putting the logic inside a trigger might create a situation where debugging and tracing are made unnecessarily complex.