I have the following script written for event schedule. This is my first time with schedule. I am not getting any errors but it doesn't perform the required operation.
CREATE EVENT checkBlack ON SCHEDULE EVERY 5 SECOND DO
DELETE FROM BlackList WHERE id=13;
It displays this message <n>Query: Create EVENT checkBlack2 on Schedule every 5 second DO Delete from BlackList where id=13
0 row(s) affected
But I have a row with id 13.
Help!!
DELIMITER $$
CREATE /*[DEFINER = { user | CURRENT_USER }]*/ EVENT `checkBlack`
ON SCHEDULE EVERY 5 SECOND
DO
BEGIN
DELETE FROM `BlackList` WHERE id=13;
END$$
DELIMITER ;
Related
I have a database containing the name and the associated birthday. I have set an event scheduler to periodically check the database for bithdays within two days from the current date. This is my code-
DELIMITER $$ CREATE EVENT `Reminder` ON SCHEDULE
EVERY 1 Day
ON COMPLETION PRESERVE
ENABLE
DO BEGIN
select * from reminder where date_format(date_sub(Birthday,interval 2 day),'%m-%d') = Date_Format(curdate(),'%m-%d');
END $$
DELIMITER ;
Apparently the sql query does not display any information. I would like to display the result of the query in an interface, whenever the query returns at least one row.
I have created this trigger that inserts a record into another table once a record has been inserted in another table
DELIMITER $$
CREATE TRIGGER logan AFTER INSERT ON messagein
FOR EACH ROW
BEGIN
DECLARE le_number INT;
SET le_number = (select messagefrom from messagein where id=NEW.id);
insert into messageout (MessageTo,MessageText) VALUES(le_number,"Thank you for contacting our company.Our sales representatives shall be in touch with you soon.");
END $$
DELIMITER ;
This trigger is created successfully but no message is inserted into the messageout table.What is the reason for this?.
This trigger is wrong, it contains two errors:
quotes are used in a string literal instead of apostrophes
triggers cannot access a table (for reading or writing) that caused the trigger invovation (see this link: http://dev.mysql.com/doc/refman/5.7/en/faqs-triggers.html#qandaitem-B-5-1-9)
Try this code:
CREATE TRIGGER logan AFTER INSERT ON messagein
FOR EACH ROW
BEGIN
insert into messageout (MessageTo,MessageText)
VALUES(NEW.messagefrom,'Thank you for contacting our company.Our sales representatives shall be in touch with you soon.');
END
A link to working demo: http://sqlfiddle.com/#!2/bdbff/1
DELIMITER $$
ALTER DEFINER=`root`#`localhost` EVENT `update_start_date` ON SCHEDULE EVERY 1 MONTH
DO
UPDATE categories SET start_date=now() where status = 1 $$
DELIMITER ;
The above event runs only one time at the month beginning but next month begining not run, Le me know how run event every month begining.
I think you should use "cron" for this
I would personally advise that you use cron or some other external schedule management system.
IF you must to mySQL to handle schedules:
DELIMITER $$
ALTER DEFINER=`root`#`localhost` EVENT `update_start_date` ON SCHEDULE EVERY 1 MONTH START NOW()
DO
UPDATE categories SET start_date=now() where status = 1 $$
END$$
DELIMITER ;
You will need to see http://dev.mysql.com/doc/refman/5.1/en/events.html for more details, but here is a simple version.
First you need to enabled the scheduler ->
SET GLOBAL event_scheduler = ON;
and then schedule the actual thing
DELIMITER $$
CREATE EVENT IF NOT EXISTS event_name ON SCHEDULE EVERY MONTH
STARTS CURRENT_TIMESTAMP + INTERVAL 1 DAY
ON COMPLETION PRESERVE
ENABLE
COMMENT 'comment'
DO BEGIN
//SQL statements here.
END $$
DELIMITER ;
Please note, that this requires superuser access to your MySQL database (CRON would require super user access to the server ! )
In MySQL I tried to define a trigger like this:
DELIMITER $$
CREATE TRIGGER vipInvite
AFTER INSERT ON meetings
FOR EACH ROW
BEGIN
IF(NOT EXISTS (SELECT * FROM participants
WHERE meetid = NEW.meetid AND pid ='vip'))
THEN
IF(EXISTS(SELECT * FROM meetings WHERE meetid = NEW.meetid AND slot > 16))
THEN
INSERT INTO participants(meetid, pid)
VALUES (NEW.meetid,(SELECT userid
FROM people WHERE people.group = 'tap' GROUP BY invite));
END IF;
END IF;
END $$
DELIMITER ;
Produces this error:
This version of MySQL doesn't yet support 'multiple triggers with the same action time and event for one table.
Is there a way to work around this so I can define multiple triggers?
This error means you already have an AFTER INSERT trigger on meetings table.
If it is the same trigger (meaning vipInvite) that you created earlier and now you want to replace it then you need to drop it first
DROP TRIGGER vipInvite;
DELIMITER $$
CREATE TRIGGER vipInvite
...
END$$
DELIMITER ;
Now if you have some other trigger you have to merge code from both triggers into one, then drop existing trigger, and then create a new one.
To show the list of existing triggers use SHOW TRIGGERS.
SHOW TRIGGERS WHERE `table` = 'meetings';
How to reproduce this error in MySQL:
ERROR 1235 (42000): This version of MySQL doesn't yet support 'multiple
triggers with the same action time and event for one table'
Run the following queries:
DELIMITER //
CREATE TRIGGER mytrigger1 AFTER INSERT ON mytable
FOR EACH ROW
BEGIN
END//
DELIMITER //
CREATE TRIGGER mytrigger2 AFTER INSERT ON mytable
FOR EACH ROW
BEGIN
END//
If you want to hook more than one action onto the same event/table, you will have to cram all of it into one trigger. You could call many stored procedures like this:
DELIMITER //
CREATE TRIGGER mytrigger1 AFTER INSERT ON mytable
FOR EACH ROW
BEGIN
CALL fromulate_the_moobars(NEW.myid);
CALL its_peanut_butter_jelly_time(NEW.myname);
END//
I need to write a trigger that will create a record in another table.
In my user table when a registering user responds to an activation email their status in that table changes from 0 to 1. When this change occurs I need it create a record in another table that has an auto-incrementing int primary id (Party).
Since the user status can be of three different states (not active (0), active (1), and banned (-1) I need this trigger to only set off when the status is changed from 0 to 1.
Can someone please help me with the SQL here?
DELIMITER $$
CREATE TRIGGER users_status_change AFTER UPDATE on users
FOR EACH ROW BEGIN
IF OLD.Status = 0 AND NEW.Status = 1 THEN
INSERT Party(Name)
VALUES('blar blar');
END IF;
END;
$$
DELIMITER ;