MySql - How to get datetime of last ALTER TABLE? - mysql

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.

Related

how can we check if a column exists on Table in MySQL without using Stored Procedure

how we can check if a column exists on Table in MySQL without using Stored Procedure. MySQL v3.23 which won't support writing Store Procedure.
v3.23 ?? If You know the table name and column name then try describe tablename or show create tablename if you know only column name select * from information schema.columns where column_name = columnname. Show tables should show all tables then manually select column name from the listed tables.
But this version is so ancient I have no idea if any of these will work
Try this, counting the columns in your table using the information_schema.COLUMNS.
SELECT COUNT(*) FROM information_schema.`COLUMNS`
WHERE table_schema = 'your_database_name'
AND table_name='your_table_name'
AND column_name='your_column_name';
The INFORMATION_SCHEMA COLUMNS Tabletable provides information about columns in tables.
Link

Finding column creation time in 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.

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?

MySQL query where column may be missing

I have a Microsoft stored procedure that queries two MySQL databases using OpenQuery. The two MySQL databases should be have the same schemas, so I can run the same query on both.
However, we will soon alter the MySQL schemas, and add a column to a table. But the two MySQL databases won't happen at the same time, and I don't know the exact date of the releases.
I therefore want to write the query so that if the new column exists, then I use it in my select. If not, then I use a default value.
Is this possible? (That is have a query that handles differences in the table schema?)
(Not to be confused with 'coelesce' where the field definitely exists, but is simply null.)
You can use the following SELECT statement:
SELECT *
FROM information_schema.COLUMNS
WHERE
TABLE_SCHEMA = 'database name' AND TABLE_NAME = 'your table name'
AND COLUMN_NAME = 'the column name you want to check for'
If the above returns a value, your column is there. If not, then run your alternative SELECT statement
Updated statement:
IF EXISTS (SELECT *
FROM OPENQUERY(servername, 'SELECT *
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = ''database name''
AND TABLE_NAME = ''your table name''
AND COLUMN_NAME = ''the column name you want to check for'' ))