- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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)
Advertisements