MySQL 8: How to query column names of a table

Updated: January 26, 2024 By: Guest Contributor Post a comment

Introduction

Understanding the structure of a database is crucial for developers working with databases. MySQL 8, like its predecessors, allows users to query the schema to obtain information about the composition of tables. In this tutorial, we will explore various ways to retrieve column names from a table using MySQL 8.

You must have access to a MySQL 8 database and a table for which you want to retrieve the column names. We will use MySQL 8’s Information Schema and SHOW COLUMNS command to achieve our goal. Moreover, we’ll cover using the newer Data Dictionary, as compared to the older INFORMATION_SCHEMA introduced in previous versions of MySQL.

Using the INFORMATION_SCHEMA

The INFORMATION_SCHEMA is a meta-database that provides information about all other databases that the MySQL server maintains. The COLUMNS table in INFORMATION_SCHEMA is what we will use to get the column names for a particular table.

Note: Replace ‘your_table’ with the name of your actual table and ‘your_database’ with the name of your database.

SELECT COLUMN_NAME 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME = 'your_table' 
  AND TABLE_SCHEMA = 'your_database';

Executing this query will return a list of all the column names in ‘your_table’ as a single column of results.

Using SHOW COLUMNS

Another way to get the column names is to use the SHOW COLUMNS command. This command provides a concise way to obtain both the column names and additional information such as type, nullability, default values, and more.

SHOW COLUMNS FROM your_database.your_table;

The output of this command will be a table with the columns: Field, Type, Null, Key, Default, and Extra. ‘Field’ is the column that lists the column names.

Filtering Column Names with LIKE

We can also filter the results to only include column names that meet specific criteria. For example, to get column names that start with ‘user’:

SHOW COLUMNS FROM your_database.your_table LIKE 'user%';

Accessing the Columns Programmatically

If you need to access column names programmatically, you can use your language of choice connected to MySQL and run the previously mentioned queries. Below is an example using Python and the MySQL Connector/Python:

import mysql.connector

# Configure the connection
cnx = mysql.connector.connect(user='your_username', password='your_password',
                              host='127.0.0.1', database='your_database')

cursor = cnx.cursor()
query = ("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS "
         "WHERE TABLE_NAME = %s AND TABLE_SCHEMA = %s")
cursor.execute(query, ('your_table', 'your_database'))

for column in cursor:
    print(column[0])

cursor.close()
cnx.close()

Running the Python script above will print each column name from ‘your_table’ to the console.

Querying Data Dictionary Tables in MySQL 8

With MySQL 8, there is a new feature known as the Data Dictionary, which is more efficient than INFORMATION_SCHEMA because it is transactional. Here is how you would access the column names using the data dictionary:

SELECT COLUMN_NAME 
FROM performance_schema.columns_info 
WHERE table_name = 'your_table' 
AND table_schema = 'your_database';

This new feature not only provides the column names but also offers performance improvements over using INFORMATION_SCHEMA.

Conclusion

In this tutorial, we learned how to retrieve column names of a MySQL table using the INFORMATION_SCHEMA, SHOW COLUMNS command, and the new Data Dictionary in MySQL 8. By understanding these methods, developers can better navigate and manipulate database schema, aiding in both development and database management tasks.