Can mysql send signals to the outer world - mysql

I need to execute a system command every time a specific mysql table is updated. Is it possible? Is mysql able to somehow let the system know that a table has been updated?
Thanks in advance.

You can take a look at this: http://bytes.com/topic/mysql/answers/424231-trigger-run-external-program
However, I recommend that you just include it in the programs that access your database. At least, you can customize it better than just putting them in your query.

Use this UDF.

There is a possible to get last updated date of the table.
select update_time from information_schema.TABLES where table_schema='<database>' and table_name='<table>';
at a specific time interval you can fetch the updated time and check with last updated time.

Related

How to update a table when its specified date is today in mysql?

I have a table in my database that has a closing date column that was an input from a user. Now I want to update a column status when the closing date is today..
Are there any suggestions on how can I do this?
After searching for answers I found out there you can CREATE AN EVENT in MySQL.
this link should help http://www.sitepoint.com/how-to-create-mysql-events/.
If your have running your database on an server that is allways running, try to use crontab(linux) or something equal.
Crontab is a deamon that runs some commands for you at specified date. I myself use crontab in combination with php scripts to reset some stuff in by mysql database.
Where is your database running ? - Maybee this would helf to answer.
Ask if you wanna know mor about this .
Gordon

How to delete the duplicate records from the whole database

Today I have tried to fire the job that checks for the redundancy in the particular table.
I have one table EmpDetails
Please find the screenshot to find the records in the table
A job runs from the sql in every 2 min and delete the redundancy from the table.
Result of the job: :
But my expectations from the job are some bit higher, I want from the job to check the the redudancy from the whole database not from the single table.
Can anyone please suggest me, is that really possible or not. If it is possible so what should be the right approach. Thanks in advance.
You should first define what a duplicate is. And for running across multiple DB use can either loop through the databases or you can use EXEC sp_MSforeachdb which is an undocumented sp
Thanks

is there a type definition similar to the oracle rowtype in mysql

I am developing a procedure where I need to insert all the columns of one table to another table including other calculation.
I have to fetch record by record, manipulate it and transfer it to another table.
is there a type definition similar to the oracle rowtype in mysql???
Any help like example or any link will be very helpful to me.
Thanks in advance...
No, there is nothing similar. You have to declare a single variable for each column to fetch
(http://www.mssqlforums.com/fetching-entire-row-cursor-t93078.html).
See this post for an example of how to use cursors in SQL-Server: Get Multiple Values in SQL Server Cursor
EDIT: sorry Miljen Mikic, my bad - but for mysql applies the same:
see this post: Conversion of Oracle %ROWTYPE to MySQL
MySQL has no an equivalent of Oracle %ROWTYPE. In MySQL it is necessary to declare a variable for every column.
Unfortunately, no ROWTYPE or the like behaviour on MySQL. Perhaps in a future, but in 2022 this feature remains not developed.
So we must continue declaring tons of variables in order to manage cursor data... :`(
See related link on MySQL dev forum at High Level Architecture tab

Automation on SQL (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.

Can a MySQL query be run every second?

I like to update my table every second. I know this is possible in AJAX but is this possible in MySQL query? I found MySQL event, can I get solution from this?
" i wanna check condition **if date_time > now() ** then update status as 1 . is this possible"
it does not seems like you need special status to be setup...
this condition can be checked when data is pulled (if need to be marked execute UPDATE and SELECT when pull),
also it can be done as cron job every minute (not sure can be done every second), however if it very related with user being on page - ajax could be the way to do it and downgrade performance at this same time
It is possible to write an sql query that will set an update status equal to 1 when the date value for that record is out of date, however you will still need a scheduled task to run this sql query from time to time.
If you are able to run code on your server, then you should write up a script that periodically runs your query against the database.
so cron is a scheduler in linux to run anything periodically.
So let's say your script that contains "**if date_time > now() ** then update status as 1" is called updateIfOld.php
then you should make crontab runs "php updateIfOld.php" every second.
Here are the manuals to use crontab: http://www.manpagez.com/man/5/crontab/Ta3HK4KosQPd8aT8BQ&usg=AFQjCNErp1Hz19N7xJwVY1wisQNxmtgpQQ&sig2=D4NQu19AJnBZil9J54V8ww
If you could actually tell a bit more about this situation and why u need this to be done, it will help us to give a better solutions.
With assumptions on what you are trying to achieve , we can give the best.
Anyway sending too many ajax and updating query every second is not an good option.
Here is an idea,
if you can store the expiry time for each row and you can set them set status to 1 where your condition matches.
Anyway i think there must be some reason to change the status to 1 (may be for making them not to display/consider).. If we know the exact reason, i think i can give a better solution..