Counting MySQL tables always returns zero? - mysql

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

Related

MySQL query for getting all column names from all tables from a specific DB

I would need to find all %phone% instances from all tables in a specific DB.
This is my script i'm trying with yet still getting all the DBs (I need only from DB1):
use DB1;
SELECT *
FROM
information_schema.columns
WHERE
column_name LIKE '%phone%';
I also tried writing like this:
SELECT *
FROM
DB1.information_schema.columns
WHERE
column_name LIKE '%phone%';
but I got a SQL Error [1064] [42000] for that syntax.
What would be the correct way to query this?
There's only one INFORMATION_SCHEMA database for all the databases on the server. The table database is in the table_schema column of the columns table.
SELECT *
FROM information_schema.columns
WHERE table_schema = 'DB1' AND column_name LIKE '%phone%';
I used the following for '%email%'
and it worked.
So i guess it would for for you too:
SELECT
DISTINCT TABLE_NAME
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
COLUMN_NAME LIKE ('%phone%') AND TABLE_SCHEMA='DB1';
use the parameter TABLE_SCHEMA, to select your scheam/database
SELECT *
FROM
information_schema.columns
WHERE
column_name LIKE '%phone%'
AND TABLe_SCHEMA = 'DB1';

How to find all invalid views in mysql?

So, we managed to do interesting things to our database that created invalid views. We just want to drop these views from the database and move on.
What I could not find is an easy way to find all invalid views in the database so that I can work from there. Is there an easy way to do this?
Recipe to create an invalid view
create table some_table (some_column varchar(20));
insert into some_table(some_column) values('some_data');
create view some_view as (select some_column from some_table);
select * from some_view;
# Now drop the table and test the view
drop table some_table;
select * from some_view;
The solution from Ralph works fine. If you want a query without subselect, try this:
SELECT table_schema, table_name
FROM information_schema.tables
WHERE table_type = 'view'
AND table_rows is null
AND table_comment like '%invalid%'
The condition table_rows is null is important as it will force an evaluation of the view and the error message in the table_comment column.
If afterwards you want to fix your view, you can see the original definition with
SELECT view_definition
FROM information_schema.views
WHERE table_schema = 'your_database'
AND table_name = 'your_view'
A better way of finding broken views within your MySQL database:
SELECT vws.table_schema,vws.table_name
FROM (
SELECT *
FROM information_schema.tables
WHERE table_type='VIEW'
AND table_comment LIKE '%invalid%'
) vws;
Original source of query
SELECT TABLE_NAME
FROM information_schema.VIEWS
WHERE TABLE_NAME NOT IN (
SELECT TABLE_NAME
FROM information_schema.TABLES
)
Based on an answer that was somehow deleted.
SELECT CONCAT('CHECK TABLE ', table_name, ';') AS my_view_check_statements
FROM information_schema.views
WHERE table_schema = 'your_database_name'
INTO OUTFILE '/tmp/chkstmts.sql';
source '/tmp/chkstmts.sql';

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;

MySQL Show Datadictionary of tables

I'd like to show DataDictionary for entire tables in database.
SHOW COLUMNS
FROM `MyDataBase`.`MyTables`
WHERE IN ( SELECT TABLE_NAME
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'MyDataBase'
);
Can i use query something like this?
I want to see entire column data using a single query
Here is what I use to generate a data dictionary when I have to:
SELECT t.table_schema AS db_name,
t.table_name,
(CASE WHEN t.table_type = 'BASE TABLE' THEN 'table'
WHEN t.table_type = 'VIEW' THEN 'view'
ELSE t.table_type
END) AS table_type,
c.column_name,
c.column_type,
c.column_default,
c.column_key,
c.is_nullable,
c.extra,
c.column_comment
FROM information_schema.tables AS t
INNER JOIN information_schema.columns AS c
ON t.table_name = c.table_name
AND t.table_schema = c.table_schema
WHERE t.table_type IN ('base table', 'view')
AND t.table_schema LIKE '%'
ORDER BY t.table_schema,
t.table_name,
c.ordinal_position
This will list all of the databases on the server that the logged in user has access to. You may want to change the where clause to only look at the specific table schema you want.
is this what you want:
SELECT `COLUMN_NAME`
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`='yourdatabasename'
AND `TABLE_NAME`='yourtablename';
From MySQL 5.7 Manual
Many sections indicate what SHOW statement is equivalent to a SELECT that retrieves information from INFORMATION_SCHEMA. For SHOW statements that display information for the default database if you omit a FROM db_name clause, you can often select information for the default database by adding an AND TABLE_SCHEMA = SCHEMA() condition to the WHERE clause of a query that retrieves information from an INFORMATION_SCHEMA table.
Usually I prefer to take this with multiple DESC. I feel SHOW COLUMNS is bit slower than DESC table_name.
So if want to get all the columns in some databases
Loop thru SHOW TABLES FROM DB_NAME
Loop thru all tables as DESC table_name
In the same way, SHOW INDEXES is slower when compared to SHOW CREATE TABLE if you just want to see the indexes on a table
describe table_name;
Retrieving information from INFORMATION_SCHEMA, but need DBA privilege.
The shortest syntax is this:
SHOW COLUMNS
FROM `MyDataBase`.`MyTables`;
Full SHOW Syntax for columns:
SHOW [FULL] COLUMNS
FROM tbl_name [FROM db_name]
[like_or_where]
I like this one,
it's simple with elemental info.
SELECT
table_name,
column_name,
column_type,
is_nullable,
column_comment
FROM
information_schema.COLUMNS
WHERE
table_schema = 'YOUR_SCHEMA_NAME'
ORDER BY
table_name,
ordinal_position ASC;

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;