Finding column creation time in MySQL - mysql

I wonder if is it possible to get creation date of column in a table? I want to see the creation times of columns that added later.

So you can get this information for the table quite easily. You would query the information_schema for the create_time of the table.
For instance:
SELECT create_time FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'your_schema'
AND table_name = 'your_table'
Check here for more information:
https://dev.mysql.com/doc/refman/5.7/en/tables-table.html
For a specific column this is not as simple because none of the system tables (that is, nothing in the INFORMATION_SCHEMA database) exist that has that kind of information recorded anywhere. In other words, there is no native mechanism to put any timestamps on column changes.
Any time that:
one or more columns change in any row
a new row is added
an old is deleted
an ALTER TABLE of any kind
the UPDATE_TIME column in INFORMATION_SCHEMA.TABLES is updated.
You could find all changes in the last hour (or your selected interval) quite simply though:
SELECT CONCAT(TABLE_SCHEMA, '.', TABLE_NAME) AS Table, UPDATE_TIME AS Updated
FROM INFORMATION_SCHEMA.TABLES
WHERE DATE_SUB(NOW(), INTERVAL 1 HOUR) < UPDATE_TIME
AND TABLE_SCHEMA != 'INFORMATION_SCHEMA'
AND TABLE_TYPE = 'BASE TABLE';
I hope this helps.

Related

MySql - How to get datetime of last ALTER TABLE?

I'm looking for a way to get datetime of last ALTER TABLE (structure edit) of a table. I'm currently using MySql 5.6. Checking information_schema db I can get only the last generic edit of the table (INSERT, UPDATE, etc.). Thanks in advance.
Try this
SELECT
create_time
FROM
INFORMATION_SCHEMA.TABLES
WHERE
table_schema = 'databasename'
AND table_name = 'tablename';
Above query gives the last table structure edited datetime.

How to quickly retrieve the table with the most rows in MySQL?

The database is on a remote Linux server, I am using Windows.
I want to retrieve the table with the most rows in MySQL, but I am on a Windows client.
I have about 200 tables. I have to click their table name one by one to figure out the row count.
The databases has many tables, I can get their rows by executing
select count(*) from table
This will retrieve the rowcount one by one.
My Question
Is there a quick method to get the table with the most rows in MySQL workbench on Windows?
how about:
SELECT TABLE_NAME
,TABLE_ROWS
FROM INFORMATION_SCHEMA.TABLES
ORDER BY TABLE_ROWS DESC
SELECT MAX(TABLE_ROWS)
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'db_name'
=> This will return the maximum number of rows a table have.
SELECT table_name, MAX(TABLE_ROWS)
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'db_name'
=> This will return the maximum number of rows a table have with the table name
Quick Links
The INFORMATION_SCHEMA TABLES Table
Get record counts for all tables in MySQL database
SELECT TABLE_ROWS, TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = '{{schema_name}}'
ORDER BY TABLE_ROWS DESC
LIMIT 1;
This will tell you the table name with most number of rows. Replace schema_name with your database before executing query.

How to get the last modified date of an mysql table

I would ask how to get the last modified date of an mysql table? For the last modified date of an mysql table, I mean the last time that the table is changed,for example,due to table insert, delete,update,truncate, etc.
As you can read in mysql documentation for The INFORMATION_SCHEMA TABLES Table,
you can get the update_time (and other information) with a simple query:
SELECT update_time
FROM information_schema.tables
WHERE table_name = 'table_name';
It gives you the last modified one.
SELECT UPDATE_TIME
FROM information_schema.tables WHERE
TABLE_SCHEMA = 'your_dbname' AND TABLE_NAME = 'your_tablename'

How to get mysql last update/delete/insert datetime for a table?

In mysql, how can you get the datetime for the last time a certain table had a row which got deleted or inserted or updated?
Thanks
For a MyISAM table do this:
SELECT UPDATE_TIME FROM information_schema.tables
WHERE TABLE_SCHEMA = 'databasename' AND TABLE_NAME = 'tablename';
For innodeb, you're apparently out of luck, since there's been a long standing bug about this... see this question: How can I determine when an InnoDB table was last changed?

Query to find tables modified in the last hour

I want to find out which tables have been modified in the last hour in a MySQL database. How can I do this?
MySQL 5.x can do this via the INFORMATION_SCHEMA database. This database contains information about tables, views, columns, etc.
SELECT *
FROM `INFORMATION_SCHEMA`.`TABLES`
WHERE
DATE_SUB(NOW(), INTERVAL 1 HOUR) < `UPDATE_TIME`
Returns all tables that have been updated (UPDATE_TIME) in the last hour. You can also filter by database name (TABLE_SCHEMA column).
An example query:
SELECT
CONCAT(`TABLE_SCHEMA`, '.', `TABLE_NAME`) AS `Table`,
UPDATE_TIME AS `Updated`
FROM `INFORMATION_SCHEMA`.`TABLES`
WHERE
DATE_SUB(NOW(), INTERVAL 3 DAY) < `UPDATE_TIME`
AND `TABLE_SCHEMA` != 'INFORMATION_SCHEMA'
AND `TABLE_TYPE` = 'BASE TABLE';
For each table you want to detect change, you need to have a column that holds the last change's timestamp.
For every insert or update in the table, you need to update that column with the current date and time.
Alternatively, you can set up a trigger which updates the column automatically on each insert or modify. That way you don't have to modify all of your query.
Once this works, to find out if rows from a table have been modified in the last hour, perform the query
select count(*) from mytable where datemod>subtime(now(),'1:0:0')
Repeat for every table you want to check.
InnoDB still currently lacks a native mechanism to retreive this information. In the related feature request at MySQL, someone advises to set AFTER [all events] triggers on each table to be monitored. The trigger would issue a statement such as
INSERT INTO last_update VALUE ('current_table_name', NOW())
ON DUPLICATE KEY UPDATE update_time = NOW();
in a table like this:
CREATE TABLE last_update (
table_name VARCHAR(64) PRIMARY KEY,
update_time DATETIME
) ENGINE = MyISAM; -- no need for transactions here
Alternatively, if a slight inaccuracy in this data (in the range of one second) is acceptable, and if you have read access to the MySQL data files, you could switch to a setting where inndb_files_per_table = ON (recommended in any case) and check the last modification time of the underlying data files.
These files are found under /var/lib/mysql/[database_name]/*.ibd in most default installations.
Please note, if you decide to take this route, you need to recreate existing tables for the new setting to apply.
I have answered a question like this in the DBA StackExchange about 1.5 years ago: Fastest way to check if InnoDB table has changed.
Based on that old answer, I recommend the following
Flushing Writes to Disk
This is a one-time setup. You need to set innodb_max_dirty_pages_pct to 0.
First, add this to /etc/my.cnf
[mysqld]
innodb_max_dirty_pages_pct=0
Then, run this to avoid having to restart mysql:
mysql> SET GLOBAL innodb_max_dirty_pages_pct = 0;
Get Timestamp of InnoDB table's .ibd file
ls has the option to retrieve the UNIX timestamp in Seconds. For an InnoDB table mydb.mytable
$ cd /var/lib/mysql/mydb
$ ls -l --time-style="+%s" mytable.ibd | awk '{print $6}'
You can then compute UNIX_TIMESTAMP(NOW()) - (timestamp of the .ibd file) and see if it is 3600 or less.
Give it a Try !!!
SELECT *
FROM information_schema.tables
WHERE UPDATE_TIME >= SYSDATE() - INTERVAL 1 DAY && TABLE_TYPE != 'SYSTEM VIEW'
SELECT *
FROM information_schema.tables
WHERE UPDATE_TIME >= DATE_SUB(CURDATE(), INTERVAL 1 DAY) && TABLE_TYPE != 'SYSTEM VIEW'