customize result of show table status from dbname - mysql

Is it possible to get the custom result when executing below query:
show table status from dbname
I customized "show processlist" query in this way:
SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST where time > 4 order by TIME desc;
In the same way, I want to get the custom result from above query.
Many thanks for your suggestions...

After going through information_schema, I got my answer which I want to share here.
SELECT * FROM information_schema.tables WHERE table_schema = 'dbname';
If want to list down only some specific columns, we can mention the column names separated by comma just after SELECT key. Also, we can add filter records by adding conditions in WHERE clause. For example:
SELECT table_name,table_type,Engine,version,table_rows FROM information_schema.tables WHERE table_schema = 'jprod';
There are only two differences between below queries:
(a)show table status from dbname;
(b)SELECT * FROM information_schema.tables WHERE table_schema = 'dbname';
Query (b) provides 4 extra columns - (i) Table_catalog (ii) Table_schema (iii) Table_type (iv)Checksum
Some column names in query (a) is brief likewise table_name as name, Table_rows as rows, table_comment as comment.

Related

Show only the filled tables' list on a MySQL database schema

I'm searching a way to show only the filled tables' list on a MySQL database schema, in other words by filtering out all the empty tables.
I know that I can perform something like that for show all the tables within a database schema:
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'myd_db_schema_name'
What if I want to add an additional WHERE condition for show only NOT NULL tables's list?
I don't know if it helps but you can use two WHERE Clause with AND
And to filter out the empty Tables you can check if the Table has a row like this:
WHERE table_rows >= 1
So the full Query would be
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'myd_db_schema_name' AND table_rows >= 1
I haven't tried it out but hopefully it works.
Okay, this is the solution:
SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'my_db_schema_name' AND table_rows >= 1
Instead of using COLUMNS I used TABLES.

Counting MySQL tables always returns zero?

I'm most certainly missing something really obvious, but I have this really basic MySQL query:
SELECT count(*) from information_schema.tables WHERE table_schema == "my_table";
However, this query always returns zero, even when "my_table" exists. What am I missing here?
To search table in specific schema (database). You've to provide TABLE_SCHEMA in your query.
SELECT count(*) from information_schema.tables where table_name = 'my_table' and table_schema = 'database_name'
Also execute SELECT * from information_schema.tables to see what other information table holds.
The double == is the problem. This is not C/C++ :-)
When I run SELECT table_schema from information_schema.tables, it returns database names, not table name.
use single = operator in where condition. Try This query
SELECT count(*) from information_schema.tables WHERE table_schema = 'my_table';
Read it for more information Official Link

Find Table with maximum number of rows in a database in mysql

As the question title suggests, I want to find the table having maximum number of rows (entries) in a particular database. I have been able to extract the names of all the tables in a particular database using the query below.
SELECT TABLE_NAME
FROM information_schema.tables
WHERE TABLE_SCHEMA="Some_Database";
How do I proceed beyond this?? I have been trying to formulate a nested query for the above purpose but couldn't come up with something (I am not very comfortable with them). Please Help.
EDIT: As given in this link the table_rows field does not give an accurate result. That is why I need to do something like a MAX (Count(*)) for each table.
Try this one......
SELECT TABLE_NAME,MAX(TABLE_ROWS)
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = "DB_Name";
OR
please try the following two queries for actual result.
query 1:
SELECT CONCAT('SELECT COUNT(*) as cnt FROM ', table_name, ' union all')
FROM information_schema.tables WHERE table_schema = 'your_db_name';
query 2:
select max(cnt) from (paste the result of first query and remove
last union all keyword) as tmptable;
What about this:
SELECT TABLE_NAME
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = "Some_Database"
ORDER BY TABLE_ROWS DESC
LIMIT 1;
information_schema.tables has a column named table_rows, so:
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'Some_Database'
ORDER BY table_rows DESC
LIMIT 1;
We can obtain the table name having max number of rows in MySQL using the this query
SELECT
TABLE_NAME,MAX(TABLE_ROWS)
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'DB_Name'
GROUP BY TABLE_NAME ORDER BY MAX(TABLE_ROWS) DESC LIMIT 1;

Get table names using SELECT statement in MySQL

In MySQL, I know I can list the tables in a database with:
SHOW TABLES
However, I want to insert these table names into another table, for instance:
INSERT INTO metadata(table_name) SHOW TABLES /* does not work */
Is there a way to get the table names using a standard SELECT statement, something like:
INSERT INTO metadata(table_name) SELECT name FROM table_names /* what should table_names be? */
To get the name of all tables use:
SELECT table_name FROM information_schema.tables;
To get the name of the tables from a specific database use:
SELECT table_name FROM information_schema.tables
WHERE table_schema = 'your_database_name';
Now, to answer the original question, use this query:
INSERT INTO table_name
SELECT table_name FROM information_schema.tables
WHERE table_schema = 'your_database_name';
For more details see: http://dev.mysql.com/doc/refman/5.0/en/information-schema.html
Try:
select * from information_schema.tables
See: http://dev.mysql.com/doc/refman/5.0/en/information-schema.html
if we have multiple databases and we need to select all tables for a particular database we can use TABLE_SCHEMA to define database name as:
select table_name from information_schema.tables where TABLE_SCHEMA='dbname';
Besides using the INFORMATION_SCHEMA table, to use SHOW TABLES to insert into a table you would use the following
<?php
$sql = "SHOW TABLES FROM $dbname";
$result = mysql_query($sql);
$arrayCount = 0
while ($row = mysql_fetch_row($result)) {
$tableNames[$arrayCount] = $row[0];
$arrayCount++; //only do this to make sure it starts at index 0
}
foreach ($tableNames as &$name {
$query = "INSERT INTO metadata (table_name) VALUES ('".$name."')";
mysql_query($query);
}
?>
Take a look at the table TABLES in the database information_schema. It contains information about the tables in your other databases. But if you're on shared hosting, you probably don't have access to it.
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'DATABASE'
MySQL INFORMATION_SCHEMA.TABLES table contains data about both tables (not temporary but permanent ones) and views. The column TABLE_TYPE defines whether this is record for table or view (for tables TABLE_TYPE='BASE TABLE' and for views TABLE_TYPE='VIEW'). So if you want to see from your schema (database) tables only there's the following query :
SELECT *
FROM information_schema.tables
WHERE table_type='BASE TABLE'
AND table_schema='myschema'
I think you can get the data you want from INFORMATION_SCHEMA TABLES.
You can find more info here: http://dev.mysql.com/doc/refman/5.0/en/tables-table.html
For fetching the name of all tables:
SELECT table_name
FROM information_schema.tables;
If you need to fetch it for a specific database:
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'your_db_name';
Output:
+--------------------+
| table_name |
+--------------------+
| myapp |
| demodb |
| cliquein |
+--------------------+
3 rows in set (0.00 sec)
There is yet another simpler way to get table names
SHOW TABLES FROM <database_name>
I think it may be helpful to point out that if you want to select tables that contain specific words you can easily do it using the SELECT (instead of SHOW). Below query easily narrows down the search to tables that contain "keyword"
SELECT *
FROM information_schema.tables
WHERE table_name like "%keyword%"
This below query worked for me. This can able to show the databases,tables,column names,data types and columns count.
**select table_schema Schema_Name ,table_name TableName,column_name ColumnName,ordinal_position "Position",column_type DataType,COUNT(1) ColumnCount
FROM information_schema.columns
GROUP by table_schema,table_name,column_name,ordinal_position, column_type;**
Yes, using information_schema.TABLES.
This works even on a cloud solution like Skyvia shown below:
With this you can simply use a SELECT statement like the above to add it up for your INSERT statement. But change the table_schema value to match the database name for your actual setup.
To insert, update and delete do the following:
$teste = array('LOW_PRIORITY', 'DELAYED', 'HIGH_PRIORITY', 'IGNORE', 'INTO', 'INSERT', 'UPDATE', 'DELETE', 'QUICK', 'FROM');
$teste1 = array("\t", "\n", "\r", "\0", "\x0B");
$strsql = trim(str_ireplace($teste1, ' ', str_ireplace($teste, '', $strsql)));
$nomeTabela = substr($strsql, 0, strpos($strsql, ' '));
print($nomeTabela);
exit;

With mysql show tables; can I sort by table name while ignoring case?

Is there a way to sort the list of tables returned by mysql's 'show tables' command?
mysql> show tables;
I'd like to sort alphabetically by the table name.
EDIT:
As pointed out by one of the answers, they are already in alphabetical order. However, A != a. Is there a way to ignore case in the sort?
Query information_schema and replace database_name with the name of the database you want to return the tables from
SELECT table_name, engine
FROM information_schema.tables
WHERE table_type = 'BASE TABLE' AND table_schema='database_name'
ORDER BY table_name ASC;
They are already in alphabetical order!
SELECT CONCAT(`table_name`, '')
FROM information_schema.tables
order by 1 asc
All you need, just transform the table_name to regular varchar type. And then order it as usual string.
Please try this one and replace database name accordingly.
SELECT table_name FROM INFORMATION_SCHEMA.tables WHERE table_schema =
'database_name' ORDER BY table_name ASC;