phpMyAdmin: Create column that increments after updating any data in its row - mysql

I have an SQL database with over 800 entries and 40 values for each entry. I would like to add a column that increases every time any of that row's values change. It would be like a version number for each individual row so I know which rows have edited data.
I know little to nothing about sql coding, I just use phpmyadmin to hold my data. Is something like this possible without adding some sort of function, and if not, how would I go about implementing something like this. Any input would be appreciated.

You can use triggers to do this job. Run the following query in phpmyadmin after editing the table name and column name for count
CREATE TRIGGER incr_on_update BEFORE UPDATE ON yourtablename
FOR EACH ROW SET NEW.count =OLD.count+1;

Related

Get logs of all update queries within database in mysql

I am looking to get all the update statements with old and new values within a database into one table.
For an example :
I have database name "users".
It contains four tables "primary_info","address_info","avtars","audit_logs"
Now, Whichever update statements executes on primary_info,address_info and avtars table that i need to log into audit logs table with below way.
ID, Table Name, Field_name,Old_value,New_value,Current Datetime
I know we can create triggers to manage such things.But i have database which contains more than 90 tables.So it won't help me to achieve by making trigger (update before) .
So is there any other way which i missed here ?
Thanks in advance.

how to limit my rows in a mysql table and overrite new rows to insert from 1st row?

e.g:i created a logtable and i want to keep limit of rows with 1000 records
...if i insert a record after 1000 rows my row should over ride from 1st row by deleteting or what ever it is.
is der any solution in mysql
anyone plz help
There is no super simple solution, but this is possible. Basically, you need to do two things.
The first is to keep track of the order that rows are inserted into the table. SQL tables represent unordered sets, so this ordering needs to be included explicitly. One simple method is an auto-incrementing id for the table.
The second is to add a trigger to insert. When a new row is inserted, then this trigger will delete a row. I would recommend that this trigger be an after insert trigger.
You can also do this at the application layer. For instance, if you insert new rows using a stored procedure, then the stored procedure can do the delete as well as the the insert.
Finally, if the goal is just to get rid of older data, there are other options. For instance, you might consider partitioning the table or scheduling a job (event) to periodically delete excess rows.

In MySQL is this possible to query the database in specify time using SQL statement?

For example, the data is like this:
[id][name]
at 10:00, the data is like this:
[1][John]
at 11:00, the user edit the data change to this:
[1][Johnson]
So, user use the alter command to change the data, but it is possible for the database query back the data on 10:00 in MySQL? Thanks.
What you are talking about is versioning. Having time stamp and version number would help but storing multiple records in same table with same id would cause a decrease in data integrity - what about a trigger on the table and insert into some form of audit table?
In general you have two options:
Add a colum version with a timestamp and expand the primary key to your currient PK and the version. To grep the data you just need to select the least version e.g. with max. Or add another colum with a bit value for the newest version. This should be faster to select.
Create a table with historical values. To implement this you need to add a trigger to the orginal table on update and copy the old value in your history table. So you can see all changes.

How can I sum two fields and store the result in a third field on a MySQL table autonomously?

I created a simple UPDATE AFTER INSERT trigger to sum two DECIMAL(10,2) fields and update a 3rd DECIMAL(10,2) field with the summed value.
The trigger code is:
delimiter |
CREATE TRIGGER calc_ttl_cost AFTER INSERT ON buybox_rec
FOR EACH ROW
BEGIN
UPDATE buybox_rec SET total_cost = (price+shipping_cost);
END;
|
I'm using phpMyAdmin to run the SQL commands, so I know that the trigger is being created successfully. Furthermore, if I run the UPDATE buybox_rec SET total_cost = (price+shipping_cost); SQL statement alone, it works as expected. (I've also tried the same code with back-ticks around the field names, but I wouldn't be writing all this if that worked)
The problem is that when I insert a new row, the trigger doesn't work and this error is thrown:
#1442 - Can't update table 'buybox_rec' in stored function/trigger
because it is already used by statement which invoked this stored
unction/trigger.
The error seems like some sort of recursive or circular reference problem, but I can't figure out where/what the problem is.
I also tried creating a stored procedure and calling it from within the trigger to try obtaining the same result, but I got the same error.
I checked a bunch of other SO questions related to MySQL UPDATE triggers and did some Googling, but here I am.
Is this out of the scope of MySQL? It seems like such a common and easy task to allow for.
Does anyone know how I accomplish this task autonomously? (AKA I don't want to hear about summing the fields after the fact via PHP, for example)
THanks for any help
The problem is that you're trying to modify the contents of a table which is already being used by the UPDATE + TRIGGER operation. This simply can't be done, but you have alternatives.
For example, if your meaningful data (or independent variables) are price and shipping cost while the total cost depends on them, you could keep only the first two in your table, and maybe have a very simple VIEW (something like SELECT price, shipping_cost, price+shipping_cost total_cost FROM buybox_rec, or whichever other fields you need) if you want to keep an eye at the total.

How do I get the original values of in an Update SQL Trigger

I'm not very familiar with triggers so thank you for your patience.
I have a database table with four columns for user text input and just four date columns showing when the user text input was last changed. What I want the trigger to do is to compare the original and new values of the user text input columns and if they are different update the date column with getdate(). I don't know how to do this. The code I wrote can't get the pre-update value of the field so it can't be compared to the post-update value. Does anyone know how to do it?
(Normally I would do this in a stored procedure. However this database table can also be directly edited by an Access database and we can't convert those changes to use the stored procedure. This only leaves us with using a trigger.)
In sql server there are two special tables availble in the trigger called inserted and deleted. Same structure as the table on which the trigger is implemented.
inserted has the new versions, deleted the old.