Is there a fast way of getting all column names from all tables in MySQL
, without having to list all the tables?
-
2I only need a quick overview over the database. Its not gonna be code in an app. – dieter Apr 13 '11 at 11:36
12 Answers
select column_name from information_schema.columns
where table_schema = 'your_db'
order by table_name,ordinal_position

- 14,289
- 10
- 80
- 109

- 54,599
- 15
- 92
- 98
-
that was what i was looking for :-) Wrote a java app doing the same stuff by now – dieter Apr 13 '11 at 11:57
-
1Thanks! To get just the column names, use this: select column_name from information_schema.columns where table_schema = 'your_db' order by table_name, ordinal_position – Phil Goetz Jun 01 '15 at 21:49
-
How about to get the count of filled data in front of the column names? I have the same problem plus data count – Gulmuhammad Akbari Jun 05 '18 at 05:55
-
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'
-
1Interesting - I get "Unknown column 'column_name' in 'where clause' – brianlmerritt Dec 03 '20 at 11:00
it is better that you use the following query to get all column names easily
Show columns from tablename

- 1
- 1

- 411
- 4
- 4
-
1Sometimes you need a solution that doesn't rely on information_schema – Bradley M. Davis Jul 22 '19 at 04:04
-
Also this gives only the columns of the given table `tablename` which is not what OP asked for. – jmizv Jul 07 '23 at 09:47
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()
.

- 423
- 4
- 10
-
That's helpful. It always annoys me to hardcode things like that when it can be avoided. Thanks. – David Aug 27 '15 at 22:01
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.

- 5
- 1
- 3

- 780
- 9
- 14
<?php
$table = 'orders';
$query = "SHOW COLUMNS FROM $table";
if($output = mysql_query($query)):
$columns = array();
while($result = mysql_fetch_assoc($output)):
$columns[] = $result['Field'];
endwhile;
endif;
echo '<pre>';
print_r($columns);
echo '</pre>';
?>
-
1Anyone reading this in 2016 - mysql_query has become deprecated. Best to use PHP's mysqli going forward. – JCutting8 Jul 31 '16 at 02:50
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;

- 1
- 1

- 21,381
- 38
- 125
- 225
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

- 2,621
- 3
- 25
- 49
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.

- 1,786
- 18
- 29
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>";

- 671
- 7
- 19
-
4'some readable php'..Naming variables with a,b ,c is not what I would call readable – Kaymaz May 03 '18 at 14:10
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;

- 1,491
- 1
- 13
- 26
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'