how to know the date of insertion of record in SQL - mysql

I am using phpMyAdmin for my project and I have a table without date/datetime field.
I am wondering if there's a way to know when I inserted each record on the database

While designing your database if you forgot to keep an extra field to store the insertion time of a row then you have only one option left to know the the insertion time.
But the condition is you must have binary logging enabled prior to it.
Reference
You may check if binary logging is enabled in your part by executing this query :
SHOW VARIABLES LIKE 'log_bin';
Further Note:
And from now on you may keep an extra field which will track the insertion time of each row.
Change your table design :
Add an extra field in your table with datatype "timestamp/datetime" with default value CURRENT_TIMESTAMP and on update CURRENT_TIMESTAMP.

Related

Can I use ON UPDATE on a MySQL Table for String - VARCHAR Column

I am developing a web application, where my database is updated through the application Front End.
However, sometimes, due to some errors/ support, I do update my database from queries.
Like, for Last Modified Time column, the timestamp is automatically updated using the, 'ON UPDATE CURRENT_TIMESTAMP', so I have accurate information regarding time of update.
Similarly, is there a way, I can change a column which stores Last Modified By (username) of the user, using the 'ON UPDATE' or anything similar on the VARCHAR field, so that, whenever it is updated from the BackEnd, the Last Modified By column is automatically updated as 'SYSTEM'.
Now the database has correct time of update, but Last Modified By column, stores the username of the user who actually last updated it from the Front End and not something that says it was modified through a query, which is causing a bit of confusion.
Hence, doing this will help me avoid confusion that the Entry was modified from the backend and not the application.
As I read in many places, the ON UPDATE was only used on TimeStamps and did not find anything similar to UPDATE Varchar fields.
Thanks for the help!

MySQL MD5(CURRENT_TIMESTAMP) On Update

I'm creating a user registration form and when I get to confirming the user's email by emailing them, I need some sort of unique string to confirm against.
Instead of generating one in PHP and inserting it into the database, I wanted to try and add a column to my table that would hold a unique value that I could use whenever I needed to confirm something.
What I want to do is set the value to an MD5 of the current timestamp. I tried just doing SELECT MD5(CURRENT_TIMESTAMP) in phpMyAdmin just to see if it would let me and it did so I thought I'd add that to an update condition but it doesn't seem to be letting me.
ALTER TABLE users ADD confirmation VARCHAR(40) DEFAULT NULL ON UPDATE MD5(CURRENT_TIMESTAMP);
The above is what I've tried. I get an error and I don't know how else to do it.
Is there anyway I can do this or something similar? Side question, does the ON UPDATE trigger on a row that just got inserted?
The syntax you are trying to use doesn't exist. It looks like you are thinking of ON UPDATE CURRENT_TIMESTAMP but that is a rather specific command, as per https://dev.mysql.com/doc/refman/5.0/en/timestamp-initialization.html
Use of DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP is
specific to TIMESTAMP.
So the ON UPDATE clause only works with CURRENT_TIMESTAMP and only on fields of type TIMESTAMP.
If you want to use the MD5 of the current timestamp either set a trigger, or just manually set the value (e.g. UPDATE users SET confirmation=MD5(CURRENT_TIMESTAMP()) WHERE user_id=123).
Bare in mind that the MD5 of the current timestamp is something that could be quite easily guessed / brute forced, so don't rely on it for security.
Use a universal unique identifier for this purpose. It's a 128-bit unique number; it's designed for this kind of thing.
It has a string representation that fits in 36 bytes.
aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee
You can generate these things in most programming languages. In MySQL you use the UUID() function to get one. Every time you call UUID(), you're guaranteed to get a new value.
Add a CHAR(36) column to your database, or just use your VARCHAR(40) column.
You can't use data definition language (ALTER TABLE) to declare ON UPDATE except for a native timestamp. You'll need application code to set your UUID values, just like you do for MD5(CURRENT_TIMESTAMP).

Timestamp current time is great to record the user registration, but will it change when I update the user?

I have a users table in my mysql database and there I record the time user signed up by having a column timeOfUserSignup (or something like that) of the type TIMESTAMP and default is set to CURRENT_TIME.
But what will happen if I update that user's info. Will that column value change to the time when the update occurred?
No, it will not be changed on updates.
A default value will only be generated if you don't pass any value on table insertion. If you need to change a value on updates too, then you can use an additional trigger for that.

Detecting database change

I have a database intensive application that needs to run every couple hours. Is there a way to detect whether a given table has changed since the last time this application ran?
The most efficient way to detect changes is this.
CHECKSUM TABLE tableName
A couple of questions:
Which OS are you working on?
Which storage engine are you using?
The command [http://dev.mysql.com/doc/refman/5.5/en/show-table-status.html](SHOW TABLE STATUS) can display some info depending on storage engine though.
It also depends on how large is the interval between runs of your intensive operation.
The most precise way I believe is with the use of triggers (AFTER INSERT/UPDATE) as #Neuticle mentioned, and just store the CURRENT_TIMESTAMP next to the table name.
CREATE TABLE table_versions(
table_name VARCHAR(50) NOT NULL PRIMARY KEY,
version TIMESTAMP NOT NULL
);
CREATE TRIGGER table_1_version_insert AFTER INSERT
ON table_1
FOR EACH ROW
BEGIN
REPLACE INTO table_versions VALUES('table_1', CURRENT_TIMESTAMP);
END
Could you set a trigger on the tables you want to track to add to a log table on insert? If that would work you only have to read the log tables on each run.
Use timestamp. Depending upon your needs you can set it to update on new rows, or just changes to existing rows. Go here to see a reference:
http://dev.mysql.com/doc/refman/5.0/en/timestamp-initialization.html
A common way to detect changes to a table between runs is with a query like this:
SELECT COUNT(*),MAX(t) FROM table;
But for this to work, a few assumptions must be true about your table:
The t column has a default value of NOW()
There is a trigger that runs on UPDATE and always sets the t column to NOW().
Any normal changes made to the table will then cause the output of the above query to change:
There are a few race conditions that can make this sort of check not work in some instances.
Have used CHECKSUM TABLE tablename and that works just splendid.
Am calling it from an AJAX request to check for table updates. If changes are found a screen refresh is performed.
For database "myMVC" and table "detail" it returns one row with fields "table" and "Checksum" set to "mymvc.detail" and "521719307" respectively.

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.