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?

王林
Release: 2023-08-27 15:21:05
forward
1217 people have browsed it

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

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

desc yourTableName;
Copy after login

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’;
Copy after login

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

Using the first syntax -

mysql> desc ExtractCommentDemo1;
Copy after login

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)
Copy after login

Using the second syntax:

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

The following is the display field Output of name -

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

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template