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

MysqlDatabase SchemaDatabase Metadata

Mysql Problem Overview


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

Mysql Solutions


Solution 1 - Mysql

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

Solution 2 - Mysql

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'

Solution 3 - Mysql

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

Show columns from tablename

Solution 4 - Mysql

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().

Solution 5 - Mysql

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.

Solution 6 - Mysql

<?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>';
?>

Solution 7 - Mysql

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;

Solution 8 - Mysql

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

Solution 9 - Mysql

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.

Solution 10 - Mysql

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>";

Solution 11 - Mysql

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;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondieterView Question on Stackoverflow
Solution 1 - MysqlNicola CossuView Answer on Stackoverflow
Solution 2 - MysqlsuganyaView Answer on Stackoverflow
Solution 3 - Mysqlrajesh satputeView Answer on Stackoverflow
Solution 4 - MysqlappelView Answer on Stackoverflow
Solution 5 - Mysqltrapper_hagView Answer on Stackoverflow
Solution 6 - MysqlAli NawazView Answer on Stackoverflow
Solution 7 - MysqlPhilip KirkbrideView Answer on Stackoverflow
Solution 8 - MysqlJ.BizMaiView Answer on Stackoverflow
Solution 9 - MysqllysdexiaView Answer on Stackoverflow
Solution 10 - MysqlScot NeryView Answer on Stackoverflow
Solution 11 - MysqlAlon BaradView Answer on Stackoverflow