MySQL: sort order "SHOW TABLES" - mysql

How are the tables ordered returned by "SHOW TABLES"?
For example the output for the information_schema database looks like this:
CHARACTER_SETS
COLLATIONS
COLLATION_CHARACTER_SET_APPLICABILITY
COLUMNS
COLUMN_PRIVILEGES
ENGINES
EVENTS
FILES
GLOBAL_STATUS
GLOBAL_VARIABLES
KEY_COLUMN_USAGE
PARAMETERS
PARTITIONS
PLUGINS
PROCESSLIST
PROFILING
REFERENTIAL_CONSTRAINTS
ROUTINES
SCHEMATA
SCHEMA_PRIVILEGES
SESSION_STATUS
SESSION_VARIABLES
STATISTICS
TABLES
TABLESPACES
TABLE_CONSTRAINTS
TABLE_PRIVILEGES
TRIGGERS
USER_PRIVILEGES
VIEWS
INNODB_CMP_RESET
INNODB_TRX
INNODB_CMPMEM_RESET
INNODB_LOCK_WAITS
INNODB_CMPMEM
INNODB_CMP
INNODB_LOCKS

See Sergei Golubchik's answer from SHOW DATABASES does not order infomation_schema correct: "no SHOW command sorts the result".
If you need the tables names sorted you can query information_schema.tables, something like:
select table_name from information_schema.tables
where table_schema = 'your_db_name' order by table_name;

Try this:
SELECT * FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'information_schema' ORDER BY TABLE_NAME

The below code works pretty well for me to list specific tables on any of my database and sort them by their name as asked.
SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'your_database_here' AND TABLE_NAME LIKE '%letters_from_your_table_here%' ORDER BY TABLE_NAME DESC

Related

MySQL get list of tables ending with specific name and it's (table's) comment

I have multiple tables in my multiple databases.
On different servers, i use MySQL / PostgreSQL / MS SQL.
I keep short table namesbut the comments given to the tables are with full explanation.
I want query that will show me tables ending with "com" and also the comment given to each table (table's comment).
In MySQL, I know:
SELECT table_name FROM information_schema.tables where table_name like "%com"
But this shows all tables from all databases.
For MySQL, check out following:
SELECT table_name FROM information_schema.tables;
will show all table names in all databases;
SELECT table_name,table_comment FROM information_schema.tables
will show all table names + comment in all databases;
interesting thing, you can fire
SELECT * FROM information_schema.tables;
to know what all info you can get of a table.
SELECT table_name,table_comment FROM information_schema.tables
where
table_schema = 'sifr_b';
will show all table names + comment in "sifr_b" database;
SELECT table_name,table_comment FROM information_schema.tables
where
table_schema = 'sifr_b' and
table_name like "%com";
will show those table names + comment in "sifr_b" database, that have table name ending with "com";

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 can I get a list of all tables in database with selected prefix?

As in subject title.
My database contains a huge amount of auxiliary tables.
Would like to build a dropdown list to select table I would like to load content from.
This code ... does not work:
SELECT table_name FROM sys.tables WHERE table_name LIKE 'Mytable_%'
In mysql you could access to information_schema filter your db name
SELECT table_name
FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'your_db_name'
and table_name LIKE 'Mytable_%'
and table_type = 'BASE TABLE';

How to query for MySQL database schematic

In Sqlite, I can query for tables names and their structure like this:
SELECT name, sql
FROM sqlite_master
WHERE type = 'table' AND Name NOT LIKE '%sqlite%'
I want to query for database schematic for MySQL database. Some digging in phpMyAdmin, I end with this:
SELECT SCHEMA_NAME, TABLE_NAME
FROM SCHEMATA, TABLES
WHERE SCHEMA_NAME = TABLE_SCHEMA AND SCHEMA_NAME ='myDb'
Unfortunately, this will only output tables names. Is there any sql query do the some work of:
mysqldump -u root myDb --no-data=true --add-drop-table=false > myDb.sql
select *
from information_schema.columns
where table_schema = 'sqlite_master'
order by table_name, ordinal_position
based on the accepted answer from this SO question
How about
SHOW CREATE TABLE tablename;

Alias to Show Tables MySQL Result

When executing SHOW TABLES FROM your_db_name_here inside MySQL, the result has Tables_in_{your_db_name_here} as its indexes. I'd like to change that, like this:
SHOW TABLES as _tables FROM mydb;
or something like
SELECT Tables_in_mydb AS _tables FROM (SHOW TABLES FROM mydb);
Any ideas?
It's usually easier to interact with the INFORMATION_SCHEMA database:
http://dev.mysql.com/doc/refman/5.0/en/tables-table.html
Example:
SELECT TABLE_NAME AS _tables
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'mydb'