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:
Related
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.
I want to run a trigger once and only once the first time a condition is satisfied.
To do this I would like to drop the trigger from within the body of the trigger itself. I have two questions: 1) is there a better way than this and 2) will anything weird happen if I drop the trigger inside the trigger body?
This is what I have so far. For context: There's another process running moving things to done and in a particular case it does not write the result so in that case I want to run a script such that when they're all done I want this trigger to read some values another table and then remove the trigger itself so that it doesn't run every single time stuff gets done normally.
CREATE TRIGGER some_trigger AFTER UPDATE ON table_name FOR EACH ROW
SELECT CASE WHEN ((SELECT count(*) FROM table_name WHERE status!='done') = 0)
THEN BEGIN
UPDATE table_name SET result = (SELECT other.result FROM table_name, other WHERE other.id = table_name.id);
DROP TRIGGER some_trigger;
END;
ELSE BEGIN END;
END CASE;
EDIT: also a third question, what does "FOR EACH ROW" mean? I only want the trigger to run once, not once per row. Looking at the docs it seems like "FOR EACH ROW" is not optional.
DROP TRIGGER cannot be performed within a Trigger.
To explain why, firstly, DROP TRIGGER causes an implicit commit, and secondly, commits cannot occur within triggers. Details below:
DROP TRIGGER causes an implicit commit
See (https://dev.mysql.com/doc/refman/8.0/en/implicit-commit.html):
The statements listed in this section (and any synonyms for them) implicitly end any transaction active in the current session, as if you had done a COMMIT before executing the statement.
...
Data definition language (DDL) statements that define or modify database objects. ALTER EVENT, ALTER FUNCTION, ALTER PROCEDURE, ALTER SERVER, ALTER TABLE, ALTER VIEW, CREATE DATABASE, CREATE EVENT, CREATE FUNCTION, CREATE INDEX, CREATE PROCEDURE, CREATE ROLE, CREATE SERVER, CREATE SPATIAL REFERENCE SYSTEM, CREATE TABLE, CREATE TRIGGER, CREATE VIEW, DROP DATABASE, DROP EVENT, DROP FUNCTION, DROP INDEX, DROP PROCEDURE, DROP ROLE, DROP SERVER, DROP SPATIAL REFERENCE SYSTEM, DROP TABLE, DROP TRIGGER, DROP VIEW, INSTALL PLUGIN, RENAME TABLE, TRUNCATE TABLE, UNINSTALL PLUGIN.
Commits cannot occur within a trigger:
See (https://dev.mysql.com/doc/refman/8.0/en/trigger-syntax.html):
The trigger cannot use statements that explicitly or implicitly begin or end a transaction, such as START TRANSACTION, COMMIT, or ROLLBACK. (ROLLBACK to SAVEPOINT is permitted because it does not end a transaction.).
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).
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
i am learning to create event in mysql, i tried sucessfully insert, update and delete commands.
but with SELECT statement, it doesn't show the output.
mysql> CREATE EVENT e_totals ON SCHEDULE AT '2013-06-04 23:59:00' DO INSERT INTO test.totals VALUES (NOW());
the above event works fine.
but the event below isn't not working...
mysql> CREATE EVENT e_totals ON SCHEDULE EVERY 30 seconds DO SELECT * FROM test.totals;
i've set all the global parameters and also enabled the event.
what i am doing wrong????
You can find the answer here: http://dev.mysql.com/doc/refman/5.1/en/create-event.html
Answer:
Statements such as SELECT or SHOW that merely return a result set have no effect when used in an event; the output from these is not sent to the MySQL Monitor, nor is it stored anywhere. However, you can use statements such as SELECT ... INTO and INSERT INTO ... SELECT that store a result. (See the next example in this section for an instance of the latter.)