How to extract column name and type from MySQL?


To extract column name and type, use INFORMATION_SCHEMA.COLUMNS −

select concat(column_name,'=',data_type) as anyAliasName from information_schema.columns
where table_schema= yourDatabaseName and table_name= yourTableName;

Let us first create a table −

mysql> create table DemoTable1812
     (
     Id int,
     FirstName varchar(20),
     Age int,
     isMarried boolean,
     status ENUM('ACTIVE','INACTIVE')
     );
Query OK, 0 rows affected (0.00 sec)

Here is the query to extract column name and type from MySQL:

mysql> select concat(column_name,'=',data_type) as COLUMNNAMEANDTYPE from information_schema.columns
     where table_schema= 'web' and table_name= 'DemoTable1812';

This will produce the following output −

+-------------------+
| COLUMNNAMEANDTYPE |
+-------------------+
| Id=int            |
| FirstName=varchar |
| Age=int           |
| isMarried=tinyint |
| status=enum       |
+-------------------+
5 rows in set (0.00 sec)

Updated on: 24-Dec-2019

383 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements