Create event daily, on a specific time - events

First event deletes all rows from a table:
CREATE DEFINER=`frontdes_user`#`localhost` EVENT `DELETE_flux_receptie` ON SCHEDULE EVERY 1 DAY STARTS '2015-09-06 00:00:00' ON COMPLETION PRESERVE ENABLE DO DELETE FROM `flux_receptie`
Second event deletes the id column from the same table:
CREATE DEFINER=`frontdes_user`#`localhost` EVENT `delete_id` ON SCHEDULE EVERY 1 DAY STARTS '2015-09-06 00:00:00' ON COMPLETION PRESERVE ENABLE DO ALTER TABLE `flux_receptie` DROP `id`
Third event adds id column into the same table:
CREATE DEFINER=`frontdes_user`#`localhost` EVENT `add_id` ON SCHEDULE EVERY 1 DAY STARTS '2015-09-06 00:00:00' ON COMPLETION PRESERVE ENABLE DO ALTER TABLE `flux_receptie` ADD `id` INT NOT NULL AUTO_INCREMENT FIRST, ADD PRIMARY KEY (`id`)
I want these events to run daily:
First one daily at 06:30, the second one daily at 06:35 and the third daily at 06:40.
Can anyone help me please with the correct schedule? Best regards, Bogdan.

If you have access to your machine you can use cronJob, the basic format of a crontab schedule consists of 6 fields, placed on a single line and separated by spaces, formatted as follows:
minute hour day month day-of-week command-line-to-execute
first create a script:
First script
#!/bin/bash
mysql --user=[username] --password=[password] --database=[db name] --execute="CREATE DEFINER=`frontdes_user`#`localhost` EVENT `DELETE_flux_receptie` ON SCHEDULE EVERY 1 DAY STARTS '2015-09-06 00:00:00' ON COMPLETION PRESERVE ENABLE DO DELETE FROM `flux_receptie`"
Second script
#!/bin/bash
mysql --user=[username] --password=[password] --database=[db name] --execute="CREATE DEFINER=`frontdes_user`#`localhost` EVENT `delete_id` ON SCHEDULE EVERY 1 DAY STARTS '2015-09-06 00:00:00' ON COMPLETION PRESERVE ENABLE DO ALTER TABLE `flux_receptie` DROP `id`"
Third script
#!/bin/bash
mysql --user=[username] --password=[password] --database=[db name] --execute="ALTER TABLE `flux_receptie` ADD `id` INT NOT NULL AUTO_INCREMENT FIRST, ADD PRIMARY KEY (`id`)"
then create 3 cron using crontab -e
crontab -e
30 6 * * * /path/of/scriptOne
35 6 * * * /path/of/scriptTwo
40 6 * * * /path/of/scriptThree

Related

Mysql event not working in the server. Same code worked in local

DROP EVENT `game_insert`; CREATE DEFINER=`numberBetting_user`#`%` EVENT `game_insert` ON SCHEDULE EVERY 5 MINUTE STARTS '2021-08-18 17:25:29' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN UPDATE `game` set `status` =0 where `status`=1; INSERT INTO `game`(`startTime`, `endTime`,`status`,`betting`) VALUES (now(),NOW() + INTERVAL 5 minute,1,1); END
This event would insert a record every 5 minutes. It was working fine in local(xampp) but in the live server, it is not inserting data. Though event scheduler is on.
Everything looks fine but the event is not doing its job.
Add DELIMITER like below
DELIMITER $$
DROP EVENT `game_insert`;
CREATE DEFINER=`numberBetting_user`#`%` EVENT `game_insert` ON
SCHEDULE EVERY 5 MINUTE STARTS '2021-08-18 17:25:29' ON COMPLETION NOT PRESERVE ENABLE DO
BEGIN
UPDATE `game` set `status` =0 where `status`=1;
INSERT INTO `game`(`startTime`, `endTime`,`status`,`betting`) VALUES (now(),NOW() + INTERVAL 5 minute,1,1);
END$$
DELIMITER ;
Only when you are in mysql workbench, you don't need thejm because they are added automatically
SOLVED the issue. The problem was with my insert query that I was missing a value for a not null column. After I added the value in the insert query the event works fine. Thank you

How to automatically execute a trigger in Toad every 15 minutes

I have this SQL query
SELECT TOALIAS, COUNT(*), TO_CHAR(TIMESTAMP,'DD/MM/YYYY HH12:MI') AS TIMESTAMP
FROM TABLE1
WHERE TIMESTAMP >= SYSDATE - 15/(24*60)
AND STATUS = 1
GROUP BY TOALIAS, TO_CHAR(TIMESTAMP,'DD/MM/YYYY HH12:MI');
I am using TOAD 12.9. I want to create a job and run this query every 15 minutes and insert the data into Table2(these records will be automatically pushed as an SMS message by an application).
I also have access to create a cron job. I found this one on Stack Overflow
0 0/15 * 1/1 * ? * /home/testdata/script/sample.sql
Can I create a cron job; let it connect to database and run a query from Table1 and insert into Table2 every 15 minutes?
*/15 * * * * command
this is the way to make cronjob to run every 15 mins.
I think you'd better to write a shell script to connect to your database. Other wise you may get authentication error.

How do I restrict the day and month of a DATE to be one of two different dates?

How to check if inputted date is one of two options.
I'm trying to create a table with a enrollment date ('DD.MM.YYYY') and that enrollment should be only possible on April first or on October first.
I've been trying different ways but can't make it work.
CREATE TABLE student
(
student_id CHAR(10),
enrollment_date DATE CHECK (enrollment_date = TO_DATE('01.04','DD.MM') OR enrollment_date = TO_DATE('01.10','DD.MM'))
);
and
CREATE TABLE student (
student_id CHAR(10),
enrollment_date DATE CHECK (enrollment_date = TO_DATE('01.04','DD.MM') OR enrollment_date = TO_DATE('01.10','DD.MM'))
);
In both cases I got:
ORA-02436: date or system variable wrongly specified in CHECK
constraint 02436. 00000 - "date or system variable wrongly specified
in CHECK constraint" *Cause: An attempt was made to use a date
constant or system variable, such as USER, in a check constraint that
was not completely specified in a CREATE TABLE or ALTER TABLE
statement. For example, a date was specified without the century.
*Action: Completely specify the date constant or system variable. Setting the event 10149 allows constraints like "a1 > '10-MAY-96'",
which a bug permitted to be created before version 8.

Oracle TO_DATE with only time input will add date component based on what logic?

Running this code in Oracle 11/12:
select to_date('101200', 'hh24miss') from dual
will return a DATE component that Oracle automatically adds based on what logic?
Eg:
select to_char(to_date('101200', 'hh24miss'), 'yyyymmdd') from dual
returns
20160701
We see the added date component is always set to the first day of the current month. Where does this logic come from?
Thanks in advance
A value of date data type always has date and time components. if you specify only time portion of the datetime value as you did, the date portion defaults to the first day of the current month.
Here is one of the places (7th paragraph) in the Oracle documentation where this behavior is documented.
There is also undocumented TIME literal and TIME data type (needs to be enabled via 10407 (datetime TIME datatype creation) event) if you need to use and store just time, without date part.
Here is a small demonstration of using time literal and time data type. But again it's undocumented and unsupported feature.
SQL> select time '11:32:00' as res
2 from dual;
res
------------------------
11.32.00.000000000 AM
You can use time literal without enabling 10407 event, but in order to be able to define a column of time data type the 10407 event needs to be enabled:
SQL> create table time_table(time_col time);
create table time_table(time_col time)
*
ERROR at line 1:
ORA-00902: invalid datatype
-- enable 10407 event
SQL> alter session set events '10407 trace name context forever, level 1';
Session altered.
Now we can create a table with a column of time data type:
SQL> create table time_table(time_col time);
Table created.
SQL> insert into time_table(time_col)
2 values(time '11:34:00');
1 row created.
SQL> select * from time_table;
TIME_COL
---------------
11.34.00 AM
SQL> alter session set events '10407 trace name context off';
Session altered.

Automatically delete new records with specific values - ORACLE

I have a table in a database (Oracle 11g) that receives roughly 45000 new records each day. Our organization has roughly 15 items (is a predetermined be a static unique value for each) and I am looking to either delete these records automatically or change a specific value in the these records columns before my batch job packages these transactions and sends them off. Any suggestions on the best way to do this? These transactions are only 10-20 of the 45000 so checking each time they are entered seems like it may have to much cost. To add the values come periodically through the day via DTS package from SQL 2000 server; and yes 2000 is end of life an we will be upgrading early next year.
sample below - accept only +0 values, if a value is less than 0 we change it to 99999;
create table my_table (val int);
create or replace trigger my_trigger
before insert on my_table
for each row
declare
begin
if :new.val <0 then:new.val := 99999; end if;
end my_trigger;
insert into my_table values(0);
insert into my_table values(1);
insert into my_table values(-1);
select * from my_table
1 0
2 1
3 99999
if want to prevent insert of the "wrong" values - "silent insert reject" is not recommended, you'd better need either raise an exception in trigger or set a constraint, see discussion there: Before insert trigger

Resources