Chronological listing of tables in MySQL database - mysql

While using phpMyAdmin I noticed suddenly that the table I frequently used had shifted to a different page, it used to be on page 2 where now it is on page 3. I'm the only one working on this database and find this quite peculiar.
There are quite a number of tables in this database so one or two new ones aren't going to stand out to me.
Is there somehow I can list the tables in this database in chronological order of being added?

Your (and my) first inclination might be to use SHOW TABLES, but alas there does not appear to be a way to do an ORDER BY, q.v. the official MySQL documentation. However, you can query information_schema.tables and order by the update_time:
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'db_name'
ORDER BY update_time DESC;
The above query will give you all tables in the db_name database sorted with the most recent first.

Related

INFORMATION_SCHEMA.COLUMNS sorted alphabetically initially, then a few days later sorted by ORDINAL_POSITION

After MySQL 8.0.25 (InnoDB engine on Windows) upgrade from MariaDB (10.1.26, and I don't think we were using InnoDB engine on Linux), every time our DB server restarts, we run into this issue:
SELECT * INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = {insert table name here};
returns the column names alphabetically, though the values in the ORDINAL_POSITION column are correct.
SELECT * FROM {insert table name here} WHERE 1=0;
This returns columns ordered alphabetically rather than by ORDINAL_POSITION in some clients (LabView queries seem to be mostly affected by this).
After a few days, everything goes back to normal, with SELECT statements always ordering columns by ORDINAL_POSITION, and INFORMATION_SCHEMA.COLUMNS being sorted by ORDINAL_POSITION as expected.
Unfortunately, due to some of our programs depending on column order being consistent in SQL query results, this has been causing a lot of trouble, and we seem to be going in circles trying to figure it out.
Any thoughts as to what would cause this delayed sorting of INFORMATION_SCHEMA.COLUMNS, and why some clients would return the column order based on the order of INFORMATION_SCHEMA.COLUMNS rather than by ORDINAL_POSITION?
Any known server/DB settings to help resolve this?
Thanks, and apologies, this is way out of my knowledge base, so this is not a great post for content or question style.
======= UPDATE/EDIT =======
Apologies, I think I accidentally lumped in a different issue/behavior that was due to a separate thing that was already fixed. The column order in the SELECT queries may not have been incorrect this time, as that was a different issue that was previously resolved, and was due to a configuration issue during the upgrade.
Essentially the issue seems to be due to the assumption that the INFORMATION_SCHEMA.COLUMNS view would always have the same sort order, and I think #Tangenially Perpendicular's answer verifies that cannot be assumed.
Side note, it looks like FlySpeed SQL Query (issue submitted) was also assuming this, as did Heidi SQL (fixed) at one point.
In SQL order is not an inherent property of a data set. The wording that appears in the SQL-92 spec is
If an <order by clause> is not specified, then the ordering of the rows ... is implementation-dependent.
You'll find that in some form in a number of places.
There's no server-wide or database-wide option to set it. You need to specify the required order in your query. Your example above becomes
SELECT * from INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = <your table name> order by ORDINAL_POSITION;
You'll need to update your queries.

What exactly does SELECT database(); mean?

So I'm really new to learning SQL and I'm interested about the command
SELECT DATABASE();
At my point in learning, I know that command is used to show what database I'm currently working with. I accidentally made the typo,
SELECT DATABASE() L; and the MySQL terminal displayed the letter L above the name of the database I was working with. Why does this happen? And what exactly does SELECT DATABASE() mean?
I tried this again and did the command
SELECT DATABASE() HI; and it did the same thing and showed "HI" above the name of my selected database. Using the correct syntax, it normally shows, "DATABASE()" above the db that I'm currently working with.
database() is an information function provided by MySQL to return the name of the current database. It can be used anywhere a string constant would be expected.
MySQL does not require a from clause, so a select is a complete query that returns one row -- the values calculated in the select.
Databases allow you to assign a name ("column alias") to an expression in the select. Normally, this would be written as:
select database() as database_name
However, the as is optional (although I strongly recommend using it for column aliases). And nothing enforces a reasonable name.
So:
select database() l
returns a result set with one row and one column. The column is called l and the value in the one row is the database name.
You are selecting a dataset with one column. The contents of the column is the current database. You are naming that column L.
Compare with
select 4 label;
I am not expert in database. But I am quite sure that show database(); will show you the selected database in other words the database which you have chosen.

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;

Searching in several mysql tables

There is a MySQL database with several(but we don't know how much) tables. The only thing I know about them, is that all of them have a prefix 'pref'. So how can I search in every table, if a don't know their names ? Can you help me with the query ?
Sorry for my bad english
Do you want to take out all the tables names starting with prefix 'pref'.
If YEs, you can run following query:
SELECT table_name FROM information_schema.tables WHERE table_name LIKE 'pref%'
You can query information_schema.tables to know all the tables starting with 'pref' and query them individually.
Use a stored procedure if this is a frequent task.