Automation on SQL (MySQL) - mysql

I would like to ask a question for connoisseurs of SQL (MySQL, to be specific).
I have a table reservation schedules. And when a customer makes a reservation there is a time to let the client to use my service. Therefore, the reservation that he did have to leave the table reservations.
Once the time limit of use is reached, there is some method (trigger,
I believe), which automatically erase the record of this book on the
table?
If so, can someone give me some idea of ​​how to start my search for it, or it is also totally welcome some help as some more advanced lines of code.
There is also the possibility that this only be possible to be implemented via Server-Side (PHP, ASP ...), which does not believe is so true because SQL is a language very complete (to my knowledge).
Edit1: The problem is that I believe this is a task of the DBMS, so I wanted to leave this responsibility to the MySQL The problem is: how?

A trigger is triggered by either before or after an insert , update or delete event (at least in MySQL according to the docs)
What you want is some sort of scheduled job either through your application be it php, asp.net, etc.. or cron job that calls some sort of SQL script.
So to answer, it can't be done purely with triggers.

You can use SQL jobs, but if the removal logic is to complex to manage it with queries I suggest you to use a PHP script that does all that work for you.
Just write down the data check/remove logic in PHP and set up a simple cron operation for it.
The advantage of this solution is that you can access to your scripts/classes/db providers and save your time and your can log all the operations separately (instead of logging to MySQL logs, no matter what script language you are relying on).
If you have a full control of your server the scheduled operation will look like this (if you want to check your DB entries every day at 00:01):
cat /etc/cron.d/php5
0 1 * * * php /path/to/your/script.php >> /path/to/your_script.log
..otherwise you will have to check the control panel of your hosting account and figure out how to manage

You can create one more column in your table where you will create the expiration date. Then you can on your sql server create the job that will erase all records that have expiration date less than curent date.
CREATE EVENT db_name
ON SCHEDULE EVERY 10 SECOND
DO
DELETE FROM myschema.mytable WHERE expiration_date < NOW()
I hope that will help.

Related

Trigger python, Php, ruby or Javascript by mysql database change

How can I make sure that specific changes in the database trigger a script?
I use windows server and mysql, and I can use the woocommerce api.
It isn't really within relational database architecture for the database to be letting other programs know what's happening in the database. MySQL triggers can make changes in the database - for example, update one table each time there's an insert in another table. But they don't call external programs.
I think you basically have two options here.
Whatever is making changes to your data, have that thing also trigger the script you want to trigger.
Have your script query the database every 5 minutes (or even every 1 minute) and act on changes. You can find changes by creating a column that tells you when a row was last updated, and having your script use the timestamp; or creating a column that marks rows when they've been acted on by your script (slightly more reliable).

Mysql - Automatically Copy Data from one table to another on a specific date

I need to be able to copy data from one table to another on a specific date. I have a list of names that can be altered by a user. However, on a certain date, these need to be locked in and copied to a uneditable table and then mailed to key users.
How would I do this?
Cheers
mysql has trigger.
trigger is like functions which is called on a specific events and cannot be called explicitly. If the event specified occurs, database automatically calls that trigger and does what it supposed to do.
So to do your job, you can write a trigger which will work on your tables and set the date when you want to do your task. Then use if to check the system date is your desired date and if yes do your job.
I used trigger on oracle and never used on mysql but as triggers are using pl/sql, there should be no difference between triggers of oracle and mysql.
PHP has Mail sending options. You can send mail using php.
Let me know if this helps you.
First step would be to create a script in any language, let's say PHP, this script will just connect to the database, and execute the copy queries (grab and insert queries) whenever called.
Then, create a cronjob using Linux to execute the script on the needed schedule.
Sending Emails would be carried out by a similar technique.
Configure a cronjob: setup a cronjob
Copying data using PHP: copying data using php
MySQL implements an event scheduler which can execute mysql stored procedures either once after a specific interval, once at a specific time or repeatedly at a specific interval; that might be of some use to you.

Application retrieving values at a particular time from sql database

I have a database in MySQL. The values in column named Curr_BaL is updated by different operations performing on it. The application, which is written in Java, accesses that database. When it runs, by default it should retrieve the last updated value. However, I also want to be able to get the value at a specific DATE entered by the user.
I have tried to do my best, but have not successful yet, and my whole application depends on that data.
Your problem is not entirely clear. What I can understand is that you need a way to have your users aware of this "last updated" value.
You have several designs approach for this. I think that the simpler would be to fetch this value when you're authenticating your user, and set it to its session information, so it will be available at any time.
You can also have some kind of service caching this value (since I guess is the same for all users).
A very important thing you didn't mentioned is who updates this value, is an external application? is a process on the same application?.
What I can understand, users date more priority then automaticaly date. Simple way for it's using triggers. Below may be useful:
CREATE OR ALTER trigger on_table_ins for TABLE
active before insert position 0
AS
BEGIN
IF (NEW.DATEFIELD IS NULL) THEN NEW.DATEFIELDD='now';
END
It correct for firebird, so see manual for triggers and insert current date(time) for your RDBMS.

MySQL daily check and update

Is possible to create a routine or procedure that can automatic everyday check a date in a table.
To be simpler : I want to check if the date on a user have permitions to enter a site and when the date pass want to make the user field activated false.
routine daily
check if (todaydate < dateclient) then
client.activated= false
Thanks for all the help.
Yes, this should be a simple UPDATE query. Something like the following:
UPDATE Client
SET Activated = 'false'
WHERE NOW() < dateclient
You would obviously need to modify this for your schema and then schedule it to run daily using cron or an alternative scheduler of your choice.
Creating the routine is simple enough, but to run it on a daily basis you'll need to wrap it in a cronjob (*nix) or Scheduled Task (Windows). The routine, unfortunately, cannot execute itself - and to the best of my knowledge MySQL server does not possess the ability to run routines at scheduled intervals.

weekly Automatic running sql server script

I have sql server script and i want to automatic run it on the database every week.
any help.
i tried sql server agent job but i have alot of data bases on my server and i should make step to every database and it will run in the same day and same time.
You were on the right track, in management studio:
(1) Find SQL Agent and right click and select Job.
(2) Give you job a name and description and then choose the Steps option on the left
(3) In the Step options you can name your step, insert your script code, AND select which database you want the job to be performed on.
(4) Next go to the Schedules option on the left and designate how often you want the job to run (pretty self-explanatory)
(5) The Alerts, Notifications, etc options should also be utilized if you want to be informed on failure/success/etc.
That's it pretty much.
If you want one job with one step that performs the same action on every database (or perhaps a subset of databases), consider using system procedure sp_msForEachDB. I spelled out one way to use this in this prior answer.