00:00

MySQL Show Columns

Last updated: 4 min read

Before writing a query or an INSERT, you often need to check a table's columns: their names, types, whether they accept NULL, and their defaults. MySQL offers SHOW COLUMNS, its shortcut DESCRIBE, and the information_schema.columns table. This tutorial explains each, using a patients table from a clinic database.

1. SHOW COLUMNS

syntax.sql
SHOW [FULL] {COLUMNS | FIELDS} {FROM | IN} table_name [{FROM | IN} db_name]
  [LIKE 'pattern' | WHERE condition];
query.sql
SHOW COLUMNS FROM patients;

Result: 9 rows

FieldTypeNullKeyDefaultExtra
patient_idint unsignedNOPRINULLauto_increment
mrnchar(10)NOUNINULL
full_namevarchar(80)NOMULNULL
birth_datedateYESNULL
genderenum('F','M','X')YESNULL
phonevarchar(15)YESNULL
doctor_idintYESMULNULL
is_activetinyint(1)NO1
created_attimestampYESCURRENT_TIMESTAMPDEFAULT_GENERATED

2. Understanding the Output

ColumnMeaningExamples from above
FieldColumn namepatient_id, mrn
TypeData type with length and attributesint unsigned, char(10), enum('F','M','X')
NullYES if NULL is allowedfull_name is NO
KeyPRI primary key, UNI unique, MUL a non-unique index (or first column of one)mrn is UNI; doctor_id is MUL (indexed for the foreign key)
DefaultDefault value, if anyis_active defaults to 1
ExtraOther detailsauto_increment, DEFAULT_GENERATED

3. DESCRIBE and DESC

DESCRIBE (or DESC) is a shortcut for SHOW COLUMNS FROM:

query.sql
DESC doctors;

Result: 2 rows

FieldTypeNullKeyDefaultExtra
doctor_idintNOPRINULL
namevarchar(50)YESNULL

You can also describe a single column:

query.sql
DESCRIBE patients phone;

Result: 1 row

FieldTypeNullKeyDefaultExtra
phonevarchar(15)YESNULL
DESCRIBE followed by a SELECT is something different: it's the same as EXPLAIN and shows the query plan.

4. SHOW FULL COLUMNS

FULL adds the collation, your privileges on each column and the column comment:

query.sql
SHOW FULL COLUMNS FROM patients WHERE Comment <> '';

Result: 2 rows

FieldTypeCollationPrivilegesComment
patient_idint unsignedNULLselect,insert,update,referencesinternal id
mrnchar(10)utf8mb4_0900_ai_ciselect,insert,update,referencesmedical record number

5. Filtering Columns

query.sql
SHOW COLUMNS FROM patients LIKE '%date%';

Result: 1 row

FieldTypeNullKeyDefaultExtra
birth_datedateYESNULL
query.sql
SHOW COLUMNS FROM patients WHERE `Null` = 'NO';

Result: 4 rows

FieldTypeNull
patient_idint unsignedNO
mrnchar(10)NO
full_namevarchar(80)NO
is_activetinyint(1)NO
query.sql
SHOW COLUMNS FROM patients FROM clinic WHERE `Type` LIKE 'varchar%';

Result: 2 rows

FieldType
full_namevarchar(80)
phonevarchar(15)

6. Using information_schema.columns

For full control (sorting, joining, searching across tables), query the metadata directly:

query.sql
SELECT column_name, data_type, character_maximum_length AS max_len, is_nullable, column_default
FROM information_schema.columns
WHERE table_schema = 'clinic' AND table_name = 'patients'
ORDER BY ordinal_position;

Result: 9 rows

COLUMN_NAMEDATA_TYPEmax_lenIS_NULLABLECOLUMN_DEFAULT
patient_idintNULLNONULL
mrnchar10NONULL
full_namevarchar80NONULL
birth_datedateNULLYESNULL
genderenum1YESNULL
phonevarchar15YESNULL
doctor_idintNULLYESNULL
is_activetinyintNULLNO1
created_attimestampNULLYESCURRENT_TIMESTAMP

Find every table in a database that has a given column, handy before renaming it:

query.sql
SELECT table_name, column_type
FROM information_schema.columns
WHERE table_schema = 'clinic' AND column_name = 'doctor_id';

Result: 2 rows

TABLE_NAMECOLUMN_TYPE
doctorsint
patientsint

Count the columns of each table:

query.sql
SELECT table_name, COUNT(*) AS columns
FROM information_schema.columns
WHERE table_schema = 'clinic'
GROUP BY table_name;

Result: 2 rows

TABLE_NAMEcolumns
doctors2
patients9

7. SHOW CREATE TABLE: the Complete Definition

SHOW COLUMNS doesn't show which index a column is in, foreign key targets or table options. SHOW CREATE TABLE prints the full statement you could use to recreate the table:

query.sql
SHOW CREATE TABLE doctors;

Result: 1 row

TableCreate Table
doctorsCREATE TABLE `doctors` ( `doctor_id` int NOT NULL, `name` varchar(50) DEFAULT NULL, PRIMARY KEY (`doctor_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

8. From the Command Line

Terminal
mysqlshow -u root -p clinic patients        # columns of one table
mysql -u root -p -e "DESCRIBE clinic.patients"   # one-off DESCRIBE

9. Which One Should You Use?

NeedUse
Quick look at a tableDESC table;
Comments and collations tooSHOW FULL COLUMNS FROM table;
Search or report across many tablesinformation_schema.columns
Exact DDL including indexes and foreign keysSHOW CREATE TABLE table;

10. Practice Exercises

Exercise 1: Show only the columns of patients that have a default value.
solution.sql
SHOW COLUMNS FROM patients WHERE `Default` IS NOT NULL;

Result: 2 rows

FieldTypeDefault
is_activetinyint(1)1
created_attimestampCURRENT_TIMESTAMP
Exercise 2: Using information_schema, list the columns of patients that are part of an index (Key is not empty).
solution.sql
SELECT column_name, column_key
FROM information_schema.columns
WHERE table_schema = 'clinic' AND table_name = 'patients' AND column_key <> ''
ORDER BY ordinal_position;

Result: 4 rows

COLUMN_NAMECOLUMN_KEY
patient_idPRI
mrnUNI
full_nameMUL
doctor_idMUL

11. Summary

  • SHOW COLUMNS FROM t;, DESCRIBE t; and DESC t; show name, type, nullability, key, default and extra.
  • SHOW FULL COLUMNS adds collation, privileges and comments.
  • Filter with LIKE or WHERE.
  • information_schema.columns supports searching across tables.
  • SHOW CREATE TABLE gives the complete definition.