Deleting/Copying the data from Database with specific condition in mySQL - mysql

I am looking for an Idea to handle data in DB directly.
Here is my use case
I have table “EVENT_REGISTERED” with column (ID, Event_name,Event_DateTime)
I want to display the EVENT_REGISTERED in the fronted end whose date and time is not passed. i.e. I want to display only Upcoming event not Historical events.
Of course this can be handle with JS code before displaying.
But What I want is there should be some sort of a trigger which will delete the Instance form the “EVENT_ REGISTERED” table and copy it to another Table “HISTORICAL_EVENT”
I cannot Create an MY SQL EVENT to do this as it like batch job and I cannot run this every 5 mins as there can be more than 10000 rows in there.
I see Trigger option as well, I am not sure how to use this as it says that it will be activated after the specific action is executed. Here the specific action is CURRENT_DATETIME == EVENT_DATETIME.
Can anybody give me a direction or any sort of alternative way to achieve this?
**I am not an Expert of MySQL*
Thank you
Regards
Prat

Don't start moving rows between tables. Simply run a query:
select er.*
from event_registered
where er.event_datetime > now();
With an index on (event_datetime), performance should be fine.

Related

MS Access Data Macro to SET a calculated field value on INSERT using DMAX

I have a two table scenario with a typical parent / child relational setup:
tblGroup - idGroup (autonumber, PK); GroupName (Short Text), Rank (Number)
tblRange - idRange (autonumber, PK), idGroup (FK -> tblGroup.idGroup), RangeName (Short Text), Rank (Number)
What I am doing on the parent (tblGroup) table is using a data macro to add the rank using the BeforeChange event:
IF
IsInsert
SetField - Rank
- DMAX(Rank, [tblGroup])+1
This works nicely and I can happily use a parametized INSERT query to add rows to the table and not have to worry about duplicate ranks and so forth.
What I would like to be able to do but cannot figure out how, is to have a data macro do the same thing for the child (tblRange) table, with the rank being set to the new highest for the parent group the child record belongs to.
If I use the same DMAX approach as I have above I am supposed to be able to set a criteria as a third option, acting like a where clause, to limit the lookup / calculation. How can I refer to the specific idGroup I am working with in tblRange in the macro? I cannot seem to figure out how to reference the new records value for this in the macro.
Something like DMAX(Rank, [tblRange], ???How_to_refer_to_idGroup_Properly???)+1
Any help greatly appreciated
Cheers
The Frog
I figured out a way to do this. Thankyou caffeinated beverages!
The reason for the strange error messages is due to limitations in the Data Macro processing, specifically in the BeforeChange event. The solution is as follows:
Create a query that selects MAX rank (MaxRank) and GROUP BY for the idGroup (ParentID)
The resultant query produces two columns of data: [MaxRank] and [ParentID]
There will be a row for every idGroup with the maximum Rank for each
Create a BeforeChange data macro
Set the following:
IF IsInsert
LookupRecord
Lookup Record In - qryGetMaxRank (or whatever you called your query)
WHERE - [qryGetMaxRank].[ParentID] = [tblRange].[idGroup]
Set Field
Name - [tblRange].[Rank]
Value - [MaxRank] + 1
The BeforeChange event cannot handle parameters for a query, and I am guessing that this applies in some form the to DMAX function here too. The use of a query that does not use any parameters, and then using the LookupRecord WHERE clause to do the filtering provided the single row result needed. The [MaxRank] value from the returned result is then able to be used to set a new value for the field.
Bit of a workaround but it does allow someone to work with the data either through a form or through the datasheet view and not create a problem.
**In answer to if this is a multi-user DB - it is not. It is just me working with it. If / when the solution is scaled up to something requiring multi-user I will likely recreate the BE in SQL Server or MySQL and use stored procedures for all data I/O. Happy to keep Access as the FE and compile into an application (using the runtime for clients), but I am a fair way off from having to do that yet. Very early stages of development at this time.
Cheers to everyone for the pointers. They helped me figure this out. Hopefully this will be of use to someone else in the future.
PS: If you need to use a parametrized query in a data macro it looks like the best bet is with the AfterInsert event or AfterUpdate event as they can support parameters.

Delete Row at Set Time

I'm creating a database table where rows need to be removed after a set time. That time is defined in minutes by the valid_time cell in that row. I found this answer though I am not sure how I can implement what I need to into it.
Is someone able to tell me how I can implement this time (as minutes) into the event in the previous answer or if it's not possible, another way to do so. Thanks.
Clarification, I have two columns in the table. One is created which is a TIMESTAMP of when the row is created, and the second is valid_time, an integer in minutes of how long the row is valid for.
DELETE FROM table WHERE created < DATE_SUB(NOW(), INTERVAL `valid_time` MINUTE)
You can try to use the MySQL event scheduler and attach a DELETE query to it. That DELETE will be a simple query that will delete all records where current_time is greater that the valid_time/valid_until fields.
You can configure the scheduler to run in a minute/hourly/daily/... basis as you wish to erase the registers.
Check here and here for more information. M0rtiis offered the query example.

mysql event scheduler - update field of all rows of table, using one of the rows

Good evening,
I've been searching around about the mysql event scheduler. Info and examples found on the web are very simple (and I have to admit my mysql experience is quite poor).
I need to do a mysql event scheduler that runs every hour. The event by itself isn't the problem. The thing is, I need the event to read a value from a field in each of the rows of the table, and then sum that value to another field of the row. Notice that this has to be done in all of the rows of the table, in a dinamic way, since the database keeps growing and I don't know how many rows I've got.
In PHP this would easy be to do with some for loops, a few arrays, then some UPDATE to mysql, and we would be done. The problem is that mysql event scheduler only works with mysql syntax, so this makes things a bit dificult.
I've got a table called users, it has some fields:
id, name, economicalincome, credits, etc...
So.. For each id, the event should some the value from the economicalincome to the existent credits.
Thanks in advance.

create a computed SQL column that maintains the running total orders each customer has

I thought about using a trigger or function to solve the problem but I don't really know how to go about coding it. Any help would be much appreciated.
did you try to create view? view update every time a new row added or updated.
Views serve as query and look like a table.
IMHO you should not persist the running totals, the possibility of the value becoming stale is high, instead you should go the route of View as suggested by #m-farhan. Still if you would want to persist the running total, then Trigger would be the safest bet. The Trigger ideally should fire a SQL query to accumulate the Customer's total and update the Total's column.

Update a mySQL table when something changes

This may be a stupid question but is it possible to store a count query in a field in a table such that when the result of the count changes the result will update itself?
Thread(ThreadID,NumMessages)
Message(MessageID,ThreadID,MessageText,PreviousMessage)
I want to update the Thread.NumMessages field any time a message with the corresponding ThreadID gets added or removed. I know I can do this by incrementing/decrementing the Thread.NumMessages field of by using a count query
SELECT COUNT(*), FROM SCHEMA.Message WHERE ThreadID='SOMETHREADID'
But is there anyway of setting up the NumMessages field so this is kept up to date without it being done explicitly at every addition and delete?
Thanks
Graeme
yes, you can use a view as the implementation of your Thread table.
http://dev.mysql.com/doc/refman/5.1/en/create-view.html
Create view thread_view
select
count(*) as NumMessages,
threadID
from message
group by threadID
Triggers!
You're wanting a trigger. This should help you with your searching (knowing what to look for is half the battle). http://www.databasedesign-resource.com/mysql-triggers.html
Basically, a trigger fires any time a designated event (such as insert) on a table is tripped, and executes a stored procedure.
your stored procedure would then check to make sure it's something it cares about (you can add all sorts of logic here), then does something (increment that value?) before MySQL executes the original query that triggered it in the first place.
http://www.roseindia.net/mysql/mysql5/triggers.shtml