Hi,

I have "Balance" table that is shown below.

Table :Balance
todaysDate/openBalance/closeBalance
------------ --------------- --------------
2008-07-24 250000 100000

Above record will be inserted into next row as it is at 10:00pm.

How do i do this?

Thanks

Dani AI

Generated

Short answer: you cannot make a trigger run at a time of day — triggers fire on table DML only. was on the wrong track there; was right to point at an external scheduler. If the server is MySQL 5.1+ the simplest, self-contained choice is the MySQL Event Scheduler. If you run an older MySQL or prefer central scheduling, use cron / Task Scheduler to call a small script or a mysql CLI statement.

Example: a safe daily event that copies the most recent closeBalance into the next day while avoiding duplicates.

CREATE EVENT balance_snapshot_daily
ON SCHEDULE EVERY 1 DAY
STARTS '2008-07-25 22:00:00'
DO
  INSERT INTO Balance (todaysDate, openBalance, closeBalance)
  SELECT src.next_day, src.last_close, src.last_close
  FROM (
    SELECT DATE_ADD(CURDATE(), INTERVAL 1 DAY) AS next_day,
           closeBalance AS last_close
    FROM Balance
    ORDER BY todaysDate DESC
    LIMIT 1
  ) AS src
  WHERE NOT EXISTS (
    SELECT 1 FROM Balance b WHERE b.todaysDate = src.next_day
  );

Enable and check the scheduler before creating events:

SET GLOBAL event_scheduler = ON;
SHOW VARIABLES LIKE 'event_scheduler';
SHOW EVENTS FROM your_database;

Notes and practical tips: the account creating the event needs the EVENT privilege and the event runs with its DEFINER rights. Events use the server time zone; cron uses OS time — verify timezones match. If the server might be down at 22:00, a cron/script that checks for missing daily snapshots and backfills them is more robust. Test the SQL on a copy of the table first, and always include a NOT EXISTS or other idempotent guard so repeated runs don’t create duplicates.

Recommended Answers

All 2 Replies

By tricky triggers, as you may have already figured out.

I do not believe that is actually the case. MySQL has no concept of triggering on a time of day (which is to say, scheduling). If you want something to happen at 10:00 PM, you need to use whatever scheduling support your OS provides (cron, on Unix systems, Scheduled Tasks on Windows, etc.) to run a job at 10:00 PM.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.