Get the date/time of the last change to a MySQL database?


You can get the date/time of the last change to a MySQL database with the help of INFORMATION_SCHEMA.TABLES. The syntax is as follows −

SELECT update_time
FROM information_schema.tables
WHERE table_schema = 'yourDatabaseName’'
AND table_name = 'yourTableName’;

To understand the above syntax, let us create a table. The query to create a table is as follows −

mysql> create table TblUpdate
   -> (
   -> Id int not null auto_increment primary key,
   -> Name varchar(20)
   -> );
Query OK, 0 rows affected (0.49 sec)

Insert some records in the table using insert command. The query is as follows −

mysql> insert into TblUpdate(Name) values('John');
Query OK, 1 row affected (0.18 sec)
mysql> insert into TblUpdate(Name) values('Carol');
Query OK, 1 row affected (0.22 sec)

Now you can display all records from the table using select statement. The query is as follows −

mysql> select *from TblUpdate;

Output

+----+-------+
| Id | Name |
+----+-------+
| 1 | John |
| 2 | Carol |
+----+-------+
2 rows in set (0.00 sec)

Now you can update the table using the following query −

mysql> update TblUpdate set Name = 'James' where Id=2;
Query OK, 1 row affected (0.15 sec)
Rows matched: 1 Changed: 1 Warnings: 0

Therefore, we just updated our table above. Now get the date/time of the last change to a MySQL database using the following query −

mysql> SELECT update_time
   -> FROM information_schema.tables
   -> WHERE table_schema = 'sample'
   -> AND table_name = 'TblUpdate'
   -> ;

The following is the output displaying we updated the database on 2019-02-09 22:49:44 −

+---------------------+
| UPDATE_TIME         |
+---------------------+
| 2019-02-09 22:49:44 |
+---------------------+
1 row in set (0.89 sec)

Updated on: 30-Jul-2019

945 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements