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

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

This question is related to mysql database-schema database-metadata

The answer is


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.


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

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

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

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

Show columns from tablename


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


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'

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;

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.


Examples related to mysql

Implement specialization in ER diagram How to post query parameters with Axios? PHP with MySQL 8.0+ error: The server requested authentication method unknown to the client Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver' phpMyAdmin - Error > Incorrect format parameter? Authentication plugin 'caching_sha2_password' is not supported How to resolve Unable to load authentication plugin 'caching_sha2_password' issue Connection Java-MySql : Public Key Retrieval is not allowed How to grant all privileges to root user in MySQL 8.0 MySQL 8.0 - Client does not support authentication protocol requested by server; consider upgrading MySQL client

Examples related to database-schema

How can I initialize a MySQL database with schema in a Docker container? What are OLTP and OLAP. What is the difference between them? How to store arrays in MySQL? When to use MyISAM and InnoDB? How to SELECT in Oracle using a DBLINK located in a different schema? How do I set the default schema for a user in MySQL Difference Between Schema / Database in MySQL Differences between key, superkey, minimal superkey, candidate key and primary key How to get all columns' names for all the tables in MySQL? Difference between database and schema

Examples related to database-metadata

How to get all columns' names for all the tables in MySQL? How to find out when a particular table was created in Oracle? How to get all table names from a database? List of foreign keys and the tables they reference in Oracle DB