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

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.

Related

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:

How to create auto update trigger for MySQL DB?

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).

mysql clean a log table when there are too much records

I have a mysql database with a table storing logs. This table receives records every 10 second.
I would like to truncate my table every week, that means after 80.000 records. I'd like to do a stored procedure which checks at every insert if there are more than 80.00. If there are more, the table is truncated.
I can't find how to use the if statement with SQL.
I know it would be better to do a script in a crontab to connect to the db and delete the table once a week, but i have the constraint to do it directly by SQL stored procedure.
Thank you in advance for any help !
You can do this with MySQL Event Scheduler, which is basically like cron, but runs as a thread within the MySQL Server. With it you can schedule the execution of an SQL statement or procedure.
First you have to turn it on:
SET GLOBAL event_scheduler = ON;
Then you can create an event:
CREATE EVENT MyEvent
ON SCHEDULE EVERY 1 WEEK
DO
TRUNCATE TABLE MyTable;
Read more about it here:
https://dev.mysql.com/doc/refman/5.7/en/event-scheduler.html
https://dev.mysql.com/doc/refman/5.7/en/create-event.html

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

Working with Events in MySQL

I have a stored procedure which basically selects data from one table and insert into another table. Basically I am doing data archiving manually. Now, I want to write an event just like discussed here
However, after reading that post and researching online, I came to know that it's not possible to create an event inside a stored procedure. Is there a way to accomplish my goal in MySQLWorkbench?
I believe you are thinking this in the oposite direction: You can't create an event in a stored procedure, but you can create a stored procedure and call it from an event.
Example:
delimiter $$
create procedure myProc()
-- Dummy procedure to move the data from mainTable to backupTable,
-- and then clear (truncate) mainTable
begin
insert into backupTable select * from mainTable;
truncate mainTable;
end $$
delimiter ;
-- Now, suposing that you want to execute this procedure every hour:
delimiter $$
create event myEvent
on schedule every 1 hour
do
begin
call myProc();
end $$
delimiter ;
You can write this as any other query in the workbench, or directly in the command line client.
About your concern
After reading your comment, I believe you are a bit confused about what MySQL Workbench is.
MySQL Workbench is only a graphical application that allows you to connect to a MySQL server and perform queries and administration tasks. But Workbench is not the core of MySQL... it is only a viewer (with steroids, maybe, but a viewer after all).
Now, the event scheduler does not reside in Workbench, but in the MySQL server instance you are connecting to. Just as the tables, views, procedures and functions are not stored in the Workbench interface but in the server, the events are also stored in the server.
(Yes, I believe it is a relevant SNAFU that the scheduled events don't show anywhere in the graphical interface, but... after a while, one learns to live with that kind of frustrations and to move on with life)
Maybe your only concern is: "Hey, and what if I want to know what events are set to run in the event scheduler?" You can execute a "show events" query to show a list of the events in the current database, and you can execute "show create event yourEvent" to show the create event syntax for that event.
I insist: Read the manual, and keep a copy at hand (download the manual for your MySQL version here).