What is the syntax in MySQL to get the column names of a table?


The syntax is as follows to get the column names of a table −

select column_name from information_schema.columns
where table_schema='yourDatabaseName' and table_name=’yourTableName’;

Let us first create a table −

mysql> create table DemoTable
(
   EmployeeId int,
   EmployeeFirstName varchar(20),
   EmployeeLastName varchar(20),
   EmployeeAge int,
   EmployeeCountryName varchar(40),
   IsMarried tinyint(1),
   isActive ENUM('ACTIVE','INACTIVE')
);
Query OK, 0 rows affected (0.65 sec)

Following is the query to get the column names of a table. Here, we are fetching the column names of DemoTable

mysql> select column_name from information_schema.columns
   where table_schema='web' and table_name='DemoTable';

This will produce the following output −

+---------------------+
| COLUMN_NAME         |
+---------------------+
| EmployeeId          |
| EmployeeFirstName   |
| EmployeeLastName    |
| EmployeeAge         |
| EmployeeCountryName |
| IsMarried           |
| isActive            |
+---------------------+
7 rows in set (0.00 sec)

Updated on: 10-Oct-2019

297 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements