How to get all columns' names for all the tables in MySQL?
Asked Answered
D

11

250

Is there a fast way of getting all column names from all tables in MySQL, without having to list all the tables?

Drawers answered 13/4, 2011 at 11:27 Comment(1)
I only need a quick overview over the database. Its not gonna be code in an app.Drawers
K
406
select column_name from information_schema.columns
where table_schema = 'your_db'
order by table_name,ordinal_position
Knowles answered 13/4, 2011 at 11:50 Comment(0)
P
62

To list all the fields from a table in MySQL:

select * 
  from information_schema.columns 
 where table_schema = 'your_DB_name' 
   and table_name = 'Your_tablename'
Preponderate answered 22/11, 2012 at 5:49 Comment(0)
P
42

it is better that you use the following query to get all column names easily

Show columns from tablename

Physic answered 16/7, 2016 at 12:55 Comment(2)
Sometimes you need a solution that doesn't rely on information_schemaHendry
Also this gives only the columns of the given table tablename which is not what OP asked for.Swingletree
S
32
SELECT * FROM information_schema.columns
WHERE table_schema = DATABASE()
ORDER BY table_name, ordinal_position

Since I don't have enough rep to comment, here's a minor improvement (in my view) over nick rulez's excellent answer: replacing WHERE table_schema = 'your_db' with WHERE table_schema = DATABASE().

Shortcoming answered 10/6, 2015 at 13:56 Comment(1)
That's helpful. It always annoys me to hardcode things like that when it can be avoided. Thanks.Leila
K
25

On the offchance that it's useful to anyone else, this will give you a comma-delimited list of the columns in each table:

SELECT table_name,GROUP_CONCAT(column_name ORDER BY ordinal_position)
FROM information_schema.columns
WHERE table_schema = DATABASE()
GROUP BY table_name
ORDER BY table_name

Note : When using tables with a high number of columns and/or with long field names, be aware of the group_concat_max_len limit, which can cause the data to get truncated.

Kurtis answered 22/12, 2015 at 17:11 Comment(0)
M
6

The question was :

Is there a fast way of getting all COLUMN NAMES from all tables in MySQL, without having to list all the tables?

SQL to get all information for each column

select * from information_schema.columns
where table_schema = 'your_db'
order by table_name,ordinal_position

SQL to get all COLUMN NAMES

select COLUMN_NAME from information_schema.columns
where table_schema = 'your_db'
order by table_name,ordinal_position
Mithraism answered 26/1, 2018 at 13:33 Comment(0)
B
5

Similar to the answer posted by @suganya this doesn't directly answer the question but is a quicker alternative for a single table:

DESCRIBE column_name;
Bruch answered 26/1, 2017 at 19:10 Comment(1)
This will print a table and not just the table's column names.Cuthbertson
I
3

I wrote this silly thing a long time ago and still actually use it now and then:

https://gist.github.com/kphretiq/e2f924416a326895233d

Basically, it does a "SHOW TABLES", then a "DESCRIBE " on each table, then spits it out as markdown.

Just edit below the "if name" and go. You'll need to have pymysql installed.

Intercross answered 30/11, 2015 at 20:32 Comment(0)
Q
2

Piggybacking on Nicola's answer with some readable php

$a = mysqli_query($conn,"select * from information_schema.columns
where table_schema = 'your_db'
order by table_name,ordinal_position");
$b = mysqli_fetch_all($a,MYSQLI_ASSOC);
$d = array();
foreach($b as $c){
    if(!is_array($d[$c['TABLE_NAME']])){
        $d[$c['TABLE_NAME']] = array();
    }
    $d[$c['TABLE_NAME']][] = $c['COLUMN_NAME'];
}
echo "<pre>",print_r($d),"</pre>";
Quail answered 14/10, 2016 at 2:14 Comment(1)
'some readable php'..Naming variables with a,b ,c is not what I would call readableSudor
O
2

You can query all columns per table in specific database using this query

select
    TABLE_SCHEMA,
    TABLE_NAME,
    COLUMN_NAME
from
    information_schema.columns
where
    table_schema = 'database_name'
order by 
    table_name,
    ordinal_position;
Obumbrate answered 24/6, 2021 at 8:4 Comment(0)
J
0

You can simply get all columns of a table using information_schema.columns just add group_concat to get a comma separated list of columns.

select group_concat( column_name ) as ColumnNames from information_schema.columns where table_schema = 'ur_db_name' and table_name = 'ur_tbl_name'
Jollenta answered 28/12, 2022 at 19:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.