how to retrieve views creation time? - mysql

Is it possible to know when a view has been created or updated?
If I run a query on information_schema.tables I get these infos only for "base table" while for view everything is equals to null. Thanks

No, it's not possible because view is not a table and not contain any physical data. MySQL creates view on the current data which exist in other tables, so you only can get update time for tables.
View is only a definition, and that definition are stored in INFORMATION_SHEMA. You can only get information about definition:
SELECT VIEW_DEFINITION FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_SCHEMA = 'test' AND TABLE_NAME = 'v';
But, you can't get update or created time.

Related

Puzzled by MySQL information_schema.tables info

For some unknown reason, the info of 'information_schema.tables' is not match to real tables:
First, find table-name from information_schema.tables, there is one result;
Then, select * from table-name, it shows table not exist! /(愒o愒)/~~
I try to create a new table(MyISAM engine) with same name, it create OK!
Then, find table-name from information_schema.tables, there are two result!
And select * from table-name shows empty set, it's normally.
Until now, table can be use, but can not be drop completely.
If drop table, it return the "First".
As the following image:
all the step
I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this......
The image shows nothing wrong to me. When you run this:
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE
TABLE_NAME = 'report_instance_performance_20160614
and get a result, you are probably getting a result for a table of that name in a different schema from where you ran that query. It would be more helpful to run this to see if something were wrong:
SELECT TABLE_SCHEMA, TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE
TABLE_NAME = 'report_instance_performance_20160614'
My assertion that that table exists in another schema but not that one that you're running in the query from is that you're getting the doesn't exist error when you try and SELECT from it.
Next, when you create a new instance of that table and then run your INFORMATION_SCHEMA.TABLES query, you are getting two results because you've now created a table with that name in your current schema. If that table had existed in your current schema, you would've gotten an error that a table of that name already existed in the schema. This further supports my assertion that report_instance_performance_20160614 exists in a different schema.
Lastly, since the table is newly created and you haven't INSERTed anything into the table before SELECTing from it, it's totally normal that the table would be empty.

Create table on every database

The way this database is setup (not my design) is that every user has their own database with many tables, so if a user ever decides to leave us we can just give them their DB and they have all of their data. The issue is, when I create a new form or feature I need to create a table in each one of these databases (50+) and it takes a huge amount of my time. Is there any way to create a table in each database?
Run the following query, then execute the output.
If the table already exists in the schema it will still run. However, if the table already exists and you want to redefine it, you can tune this statement to drop those table and recreate them.
SELECT DISTINCT CONCAT('CREATE TABLE IF NOT EXISTS ', TABLE_SCHEMA, '.define table here')
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA NOT IN
('mysql', 'information_schema', 'performance_schema');
You could do it programmatically using a programming language like PHP or Python.
Here's the pseudocode that would need to be implemented.
get array of user databases by selecting from information_schema
for each database:
SQL : "create table if not exists <database>.tableName ..."
end for each

MySQL - is there any way to check if a view is referenced by other views?

I have a development MySQL db which i am cleaning up. I am deleting some test views.
How can I check if a view is referenced by other views?
There is an information schema table which stores view definitions. You could use something like:
SELECT * FROM information_schema.VIEWS
WHERE table_schema = 'your schema'
AND view_definition LIKE '%referenced_view%';
Which will give you back all views that reference 'referened_view' in their definition

mysql tell difference between table and view

I'm surprised I didn't find this answer out there.
I know very well what the difference between a table and a view is. BUT..how do I DETERMINE whether a db object is a table or view? Since
show tables;
will show both tables and views - and there is no "show views" command.
to determine in my coding (which has to read multiple objects and may not "know" better), I do this:
show create view my_table_or_view
and if I get an error, which I prevent from dying, it's a table. Pretty clumsy, is there a better way?
try this variation instead ...
show full tables;
the Table_type column will give the info you require :)
You can use the following query and if it returns a record it's a table
SELECT *
FROM information_schema.tables
WHERE 'TABLE_TYPE' = 'BASE TABLE'
AND table_name = 'your table name'

MySQL store checksum of tables in another table

CONTEXT:
we have big databases with loads of tables. Most of them (99%) are using innodb.
we want to have a daily process that monitors which table has been modified. As they use innodb the value of Update_time from
SHOW table STATUS from information_schema;
is null.
For that reason we want to create a daily procedure that will store the checksum (and other stuffs for that matters) of each table somewhere (preferably another table). On that, we will do different checks.
PROBLEM:
I'm trying to use
checksum table from db_schema.table_name;
which returns a resultset-table with 2 columns: "table","checksum".
It gives me the value I want but I'm not able to use it in a select or insert statement.
I tried a lot of things like:
select `checksum` from (checksum table from db_schema.table_name);
or other similar queries. But I'm not able to extract the data from the resultset.
Is there a way I can do that?
Thanks in advance for your help.
EDIT: in the end what I want is to build a more complex resultset having different informations in it (table schema, table name, count, checksum, datetime:now()...)
Then I'll use this resultset to compare with the values of yesterday and draw my own statistics. That's why I want to get the checksum from that resultset.
There is no possibility to save the result of CHECKSUM TABLE directly using SQL. Neither can you use prepared statements or cursors in stored procedures to use the checksum result.
You best make a script around it, or download some popular tools doing it for you.
For MyISAM tables using the CHECKSUM=1 table argument, you can simply use INFORMATION_SCHEMA like this:
SELECT TABLE_NAME,CHECKSUM FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'test' AND ENGINE='MyISAM'
AND CHECKSUM IS NOT NULL;