Get table column names in MySQL?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Getting column names from a MySQL table is a common metadata task. You might need it for schema inspection, dynamic SQL generation, migrations, or tools that build forms and exports automatically. MySQL gives you both quick interactive commands and SQL-standard metadata views, and the right choice depends on whether you want a human-readable summary or queryable schema data.
The Quick Interactive Commands
For manual inspection in the MySQL client, SHOW COLUMNS is the simplest answer.
That returns one row per column, including the column name, type, nullability, key information, default value, and any extra flags.
DESCRIBE is essentially the same idea with shorter syntax:
These commands are convenient when you are exploring a schema manually, but they are not always the best fit for application code because they return more than just the column names and are less composable than a normal SELECT query.
The Queryable Approach: INFORMATION_SCHEMA.COLUMNS
If you want only column names or want to filter them as part of a query, use INFORMATION_SCHEMA.COLUMNS.
This is usually the most practical approach for scripts and tooling because:
- it returns structured metadata
- you can filter and sort it like any other query
- it works well in application code
The ORDINAL_POSITION sort matters if you want the columns in table order instead of alphabetical order.
When You Need More Than the Name
Often the real task is not only "give me column names," but "give me the usable schema." In that case, ask for more metadata in the same query.
This is especially useful for admin dashboards, migration checks, ETL jobs, and generic import or export tools.
Using It From Application Code
If you need the column names inside an application, query INFORMATION_SCHEMA from your database client library. Here is a small Python example using mysql-connector-python:
This is preferable to parsing SHOW COLUMNS output as plain text because it keeps the result structured and parameterized.
SHOW CREATE TABLE Is Different
Another command people reach for is:
This is useful when you want the full DDL, but it is the wrong tool if you only need column names. It returns the table definition as SQL text, which you then have to parse yourself. That is unnecessary work when the schema metadata is already available in INFORMATION_SCHEMA.
Common Pitfalls
A common mistake is forgetting the schema filter in INFORMATION_SCHEMA.COLUMNS. If several databases contain a table with the same name, your query may return columns from the wrong table or from multiple tables.
Another mistake is assuming the result order is always the table order. Add ORDER BY ORDINAL_POSITION when order matters.
Developers also sometimes parse SHOW CREATE TABLE output to get column names. That is much more fragile than querying metadata directly.
Summary
- Use
SHOW COLUMNSorDESCRIBEfor quick manual inspection. - Use
INFORMATION_SCHEMA.COLUMNSwhen you need queryable, script-friendly metadata. - Filter by both
TABLE_SCHEMAandTABLE_NAMEso you get the right table. - Sort by
ORDINAL_POSITIONif you want the original column order. - Prefer metadata queries over parsing raw DDL text.

