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;
Related
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
I'm astounded by the number of tables in Magento Enterprise 1.13 (over 200). I'm trying to get a handle on the way things are organized and I think it would be helpful to know the number of columns in each of the tables. The following query will get me a breakdown of columns and their data_types for each table:
SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = `<database_name>`
ORDER BY TABLE_NAME;
But I would also like to know the number of columns in each table.
SELECT COUNT(COLUMN_NAME)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = '<database_name>'
AND TABLE_NAME IN (
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = '<database_name>'
);
Unfortunately, the above query returns a count of the total number of columns in the database. I realize that my approach is too simplistic and a LOOP or a FOREACH statement is closer to the solution I'm looking for but I don't know how to make the leap to that point.
SELECT TABLE_NAME, COUNT(COLUMN_NAME) AS NoCOLUMNS
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = '<database_name>'
AND TABLE_NAME IN (SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = '<database_name>')
GROUP BY TABLE_NAME;
Basic GROUP BY DEMO
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';
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;
I need to view the number of tables existing under one particular Owner/Creator.
How would I do this?
Note:
I am using SQL Server 2008.
use yourdb;
SELECT count(*)
FROM INFORMATION_SCHEMA.Tables
where table_type like '%base table%'
select table_schema,
COUNT(*)
from information_schema.Tables
where table_type = 'BASE TABLE'
AND table_schema = 'myschema' -- replace this with the schema/owner you are looking for
group by table_schema