Explain Codes LogoExplain Codes Logo

How do I find the creation date of a MySQL table?

sql
metadata
database-performance
Anton ShumikhinbyAnton Shumikhin·Sep 10, 2024
TLDR

Fetch the creation date of a MySQL table using the CREATE_TIME attribute from information schema:

SELECT CREATE_TIME FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'YourDatabaseName' AND TABLE_NAME = 'YourTableName';

This returns the creation timestamp of the table YourTableName from the database YourDatabaseName.

Understanding INFORMATION_SCHEMA.TABLES

MySQL's INFORMATION_SCHEMA.TABLES is a robust source of metadata for your tables. To leverage this data, understanding is key.

Retrieving complete metadata

Obtain table metadata with the following SQL command, and yes, we're using wildcards because, like Pokemon, we gotta catch 'em all:

SELECT * FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'YourDatabaseName' AND TABLE_NAME = 'YourTableName';

Deconstructing the command

In this INFORMATION_SCHEMA command, the stars of the show are:

  • CREATE_TIME: The all-important timestamp of table creation.
  • TABLE_SCHEMA: Used to specify the database name.
  • TABLE_NAME: Tells SQL which table you're interested in.

Beware the 'database beast'

For large databases, querying metadata can be time-consuming (like waiting for your code to compile). Check out tips on Percona Database Performance Blog to mitigate this.

The SHOW TABLE STATUS trick

Direct and to-the-point, the SHOW TABLE STATUS command is a great alternative for those who prefer the express lane:

SHOW TABLE STATUS WHERE Name = 'YourTableName';

Why is this an alluring option?

  • It's fast: Queries on steroids, already loaded and ready to go.
  • Offers simplicity: No jumping through (boolean) hoops.

Command tailoring

Narrowing down to Create_time keeps it trim and fit:

SHOW TABLE STATUS LIKE 'YourTableName' \G

Find Create_time in the output. The \G - short for "Gee, this is a handy SQL formatter" - presents the results in an easily readable vertical format.

Tips for interpreting CREATE_TIME

The CREATE_TIME generally holds the original creation timestamp. But remember, SQL is a sneaky sly: various events can alter it:

More than meets the eye

  • Renaming a table might reset the CREATE_TIME
  • Restoring a table from a backup is another timestamp trickster

The fandom of INFORMATION_SCHEMA

INFORMATION_SCHEMA is the SQL equivalent of a cool kids' club - everyone uses it because it's super reliable:

Compelling capabilities

  • Insights: Offers more than just CREATE_TIME.
  • Compatibility: Works with most MySQL versions.
  • Standardization: Adheres to SQL system tables standard.

Making the most of it

  • Be a sniper, not a shotgun: Specify your needs in your query to avoid performance drawbacks.
  • Use it wisely when you need a deep dive into database analysis.