Home >Database >Mysql Tutorial >What is the SQL command that returns the field names of a table?

What is the SQL command that returns the field names of a table?

王林
王林forward
2023-08-27 15:21:051214browse

返回表的字段名称的 SQL 命令是什么?

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

desc yourTableName;

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

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

To understand these two syntaxes, assume we have a table "ExtractCommentDemo1".

Using the first syntax -

mysql> desc ExtractCommentDemo1;

The following is the output of the display field-

+----------+--------------+------+-----+---------+-------+
| 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 display field Output of name -

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

The above is the detailed content of What is the SQL command that returns the field names of a table?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete