How to can I get the names of my MySQL table columns?

MySQLMySQLi Database

You can use SHOW command for this. 

Following is the syntax −

show columns from yourTableName;

Let us first create a table −

mysql> create table DemoTable
(
   StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   StudentFirstName varchar(20),
   StudentLastName varchar(20),
   StudentAge int,
   StudentAddress varchar(200)
);
Query OK, 0 rows affected (0.54 sec)

Following is the query to get the names of my MySQL table columns −

mysql> show columns from DemoTable;

This will produce the following output −

+------------------+--------------+------+-----+---------+----------------+
| Field            | Type         | Null | Key | Default | Extra          |
+------------------+--------------+------+-----+---------+----------------+
| StudentId        | int(11)      | NO   | PRI | NULL    | auto_increment |
| StudentFirstName | varchar(20)  | YES  |     | NULL    |                |
| StudentLastName  | varchar(20)  | YES  |     | NULL    |                |
| StudentAge       | int(11)      | YES  |     | NULL    |                |
| StudentAddress   | varchar(200) | YES  |     | NULL    |                |
+------------------+--------------+------+-----+---------+----------------+
5 rows in set (0.01 sec)
raja
Published on 29-Apr-2019 14:21:33
Advertisements