MySQL query to get the last created table name (most recent)?

MySQLMySQLi Database

You can use the concept INFORMATION_SCHEMA.TABLES for this. Let us first create a table. This would be our most recent table −

mysql> create table DemoTable1323
   -> (
   -> FirstName varchar(10)
   -> );
Query OK, 0 rows affected (0.43 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1323 values('Chris');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable1323 values('David');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable1323 values('Bob');
Query OK, 1 row affected (0.11 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable1323;

This will produce the following output:

+-----------+
| FirstName |
+-----------+
| Chris     |
| David     |
| Bob       |
+-----------+
3 rows in set (0.00 sec)

Following is the query to get the last created table name −

mysql> select table_name,create_time AS `Time` from information_schema.tables where table_schema = 'web' order by create_time desc limit 1;

This will produce the following output:

+---------------+---------------------+
| TABLE_NAME    | Time                |
+---------------+---------------------+
| demotable1323 | 2019-09-18 22:03:24 |
+---------------+---------------------+
1 row in set (0.02 sec)
raja
Published on 05-Nov-2019 06:22:25
Advertisements