What is the SQL command to return the field names of a table?


To return the field names of a table, you can use desc command. The syntax is as follows −

desc yourTableName;

Or you can use column_name field from information_schema.columns table. The syntax is as follows −

select column_name from information_schema.columns where table_name = ’yourTableName’;

To understand both the syntax, let’s say we have a table ‘ExtractCommentDemo1’.

Using the first syntax −

mysql> desc ExtractCommentDemo1;

The following is the output displaying the fields −

+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| UserId   | int(11)      | YES  |     | NULL    |       |
| UserName | varchar(200) | YES  |     | NULL    |       |
+----------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

Using the second syntax:

mysql> select column_name from INFORMATION_SCHEMA.COLUMNS
   −> where table_name = 'ExtractCommentDemo1';

The following is the output displaying the field names −

+-------------+
| COLUMN_NAME |
+-------------+
| UserId      |
| UserName    |
+-------------+
2 rows in set (0.00 sec)

Updated on: 30-Jul-2019

79 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements