W3docs

MySQL query to get column names?

To get the column names of a table in MySQL, you can use the SHOW COLUMNS FROM command.

To get the column names of a table in MySQL, you can use the SHOW COLUMNS FROM command.

How to get the column names of a table in MySQL?

SHOW COLUMNS FROM my_table;

This will return a result set containing the column names of the my_table table.

You can also use the DESCRIBE command to get the same information:

How to get the column names of a table in MySQL using the DESCRIBE command?

DESCRIBE my_table;

Both of these commands will return a result set with the following columns:

  • Field: the name of the column
  • Type: the data type of the column
  • Null: whether the column allows NULL values
  • Key: the type of index used for the column (if any)
  • Default: the default value for the column
  • Extra: any additional information about the column (such as whether it is AUTO_INCREMENT)

For broader compatibility across different database systems, you can query the information_schema.columns table:

SELECT column_name
FROM information_schema.columns
WHERE table_name = 'my_table';

To filter results by a specific database, add AND table_schema = 'my_database' to the WHERE clause.