MySQL query to get column names?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
MySQL is one of the most widely used database management systems, known for its reliability and ease of use. Often, when working with MySQL databases, it becomes necessary to fetch metadata about the tables themselves, such as the names of the columns in a given table. This article explores how to retrieve column names from a MySQL table using SQL queries, providing technical explanations and examples to clarify the process.
Retrieving Column Names Using the SHOW Statement
MySQL provides several ways to obtain column names, and one of the simplest methods is using the SHOW statement. The basic syntax is:
The SHOW COLUMNS command provides detailed information about each column in the specified table, including the column name, data type, if the column can be null, key information, default value, and extra details. Here's an example:
Assuming employees is a table in your database, this query would return information like:
| Field | Type | Null | Key | Default | Extra |
| id | int(11) | NO | PRI | NULL | auto_increment |
| first_name | varchar(50) | YES | NULL | ||
| last_name | varchar(50) | YES | NULL | ||
| varchar(100) | NO | UNI | NULL |
Understanding the Output
- Field: This is the name of the column.
- Type: Data type of the column.
- Null: Indicates whether the column can have NULL values.
- Key: Shows if the column is indexed.
- Default: Default value for the column.
- Extra: Any additional information about the column, such as
auto_increment.
Using INFORMATION_SCHEMA Tables
For a more programmatic approach, especially when dynamically generating SQL statements, the INFORMATION_SCHEMA database is invaluable. The COLUMNS table within this system database contains detailed metadata about all the tables in your database.
The syntax to query column names from INFORMATION_SCHEMA is:
Let's have an example to illustrate:
Assuming the database schema name is company, this query would return a list of column names for the employees table:
| COLUMN_NAME |
| id |
| first_name |
| last_name |
Advantages of Using INFORMATION_SCHEMA
- Database Agnostic: This approach adheres to SQL standards and is generally applicable across different RDBMS.
- Flexible Queries: Enables filtering and complex querying, such as sorting and joining with other metadata.
- Detailed Information: Can retrieve additional data besides column names, like default expressions or character set details.
Retrieving Column Index Information
In addition to column names, you might also want to understand how the table is indexed to optimize your queries. You can do this using the SHOW INDEX command.
This command will return a list of indexes on the employees table along with their type and other characteristics:
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Index_type |
| employees | 0 | PRIMARY | 1 | id | A | 5 | BTREE |
| employees | 0 | 1 | A | 5 | BTREE |
Key Points to Note
- Non_unique: Indicates if the index allows non-unique values.
- Key_name: Name of the index.
- Seq_in_index: Sequence number indicating the column's position within the index.
- Index_type: Type of index, like BTREE or HASH.
Summary Table
Here is a summary of the key methods to retrieve column information from a MySQL database:
| Method | Command Syntax | Use Cases | Additional Details |
SHOW COLUMNS | SHOW COLUMNS FROM table_name; | Quick overview of table schema | Simple and direct, but less flexible |
INFORMATION_SCHEMA | SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS ... | Dynamic querying | Standard SQL, database agnostic programming |
SHOW INDEX | SHOW INDEX FROM table_name; | Retrieve index info | Useful for performance tuning and optimization |
In conclusion, retrieving column names and other metadata from a MySQL database is a relatively straightforward task. Whether you're using the SHOW COLUMNS approach for quick insights or leveraging the INFORMATION_SCHEMA for more programmatic purposes, MySQL provides all the tools necessary to effectively manage and optimize your database tables.
Related reading
- MySQL Query to select data from last week?
- MySQL Quick breakdown of the types of joins
- Mysql remote connect over ssh to a kubernetes pod
- MySQL remove all whitespaces from the entire column
- MySQL Removing Some Foreign keys
- Mysql Replication, 2 databases, 2 ways?
- MySQL Replication 3 masters, 1 Slave
- MySQL replication monitor - Seconds_Behind_Master

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.