MySQL Show Columns
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
SHOW [FULL] {COLUMNS | FIELDS} {FROM | IN} table_name [{FROM | IN} db_name]
[LIKE 'pattern' | WHERE condition];
SHOW COLUMNS FROM patients;
Result: 9 rows
| Field | Type | Null | Key | Default | Extra |
|---|---|---|---|---|---|
| patient_id | int unsigned | NO | PRI | NULL | auto_increment |
| mrn | char(10) | NO | UNI | NULL | |
| full_name | varchar(80) | NO | MUL | NULL | |
| birth_date | date | YES | NULL | ||
| gender | enum('F','M','X') | YES | NULL | ||
| phone | varchar(15) | YES | NULL | ||
| doctor_id | int | YES | MUL | NULL | |
| is_active | tinyint(1) | NO | 1 | ||
| created_at | timestamp | YES | CURRENT_TIMESTAMP | DEFAULT_GENERATED |
2. Understanding the Output
| Column | Meaning | Examples from above |
|---|---|---|
Field | Column name | patient_id, mrn |
Type | Data type with length and attributes | int unsigned, char(10), enum('F','M','X') |
Null | YES if NULL is allowed | full_name is NO |
Key | PRI 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) |
Default | Default value, if any | is_active defaults to 1 |
Extra | Other details | auto_increment, DEFAULT_GENERATED |
3. DESCRIBE and DESC
DESCRIBE (or DESC) is a shortcut for SHOW COLUMNS FROM:
DESC doctors;
Result: 2 rows
| Field | Type | Null | Key | Default | Extra |
|---|---|---|---|---|---|
| doctor_id | int | NO | PRI | NULL | |
| name | varchar(50) | YES | NULL |
You can also describe a single column:
DESCRIBE patients phone;
Result: 1 row
| Field | Type | Null | Key | Default | Extra |
|---|---|---|---|---|---|
| phone | varchar(15) | YES | NULL |
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:
SHOW FULL COLUMNS FROM patients WHERE Comment <> '';
Result: 2 rows
| Field | Type | Collation | Privileges | Comment |
|---|---|---|---|---|
| patient_id | int unsigned | NULL | select,insert,update,references | internal id |
| mrn | char(10) | utf8mb4_0900_ai_ci | select,insert,update,references | medical record number |
5. Filtering Columns
SHOW COLUMNS FROM patients LIKE '%date%';
Result: 1 row
| Field | Type | Null | Key | Default | Extra |
|---|---|---|---|---|---|
| birth_date | date | YES | NULL |
SHOW COLUMNS FROM patients WHERE `Null` = 'NO';
Result: 4 rows
| Field | Type | Null |
|---|---|---|
| patient_id | int unsigned | NO |
| mrn | char(10) | NO |
| full_name | varchar(80) | NO |
| is_active | tinyint(1) | NO |
SHOW COLUMNS FROM patients FROM clinic WHERE `Type` LIKE 'varchar%';
Result: 2 rows
| Field | Type |
|---|---|
| full_name | varchar(80) |
| phone | varchar(15) |
6. Using information_schema.columns
For full control (sorting, joining, searching across tables), query the metadata directly:
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_NAME | DATA_TYPE | max_len | IS_NULLABLE | COLUMN_DEFAULT |
|---|---|---|---|---|
| patient_id | int | NULL | NO | NULL |
| mrn | char | 10 | NO | NULL |
| full_name | varchar | 80 | NO | NULL |
| birth_date | date | NULL | YES | NULL |
| gender | enum | 1 | YES | NULL |
| phone | varchar | 15 | YES | NULL |
| doctor_id | int | NULL | YES | NULL |
| is_active | tinyint | NULL | NO | 1 |
| created_at | timestamp | NULL | YES | CURRENT_TIMESTAMP |
Find every table in a database that has a given column, handy before renaming it:
SELECT table_name, column_type
FROM information_schema.columns
WHERE table_schema = 'clinic' AND column_name = 'doctor_id';
Result: 2 rows
| TABLE_NAME | COLUMN_TYPE |
|---|---|
| doctors | int |
| patients | int |
Count the columns of each table:
SELECT table_name, COUNT(*) AS columns
FROM information_schema.columns
WHERE table_schema = 'clinic'
GROUP BY table_name;
Result: 2 rows
| TABLE_NAME | columns |
|---|---|
| doctors | 2 |
| patients | 9 |
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:
SHOW CREATE TABLE doctors;
Result: 1 row
| Table | Create Table |
|---|---|
| doctors | CREATE 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
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?
| Need | Use |
|---|---|
| Quick look at a table | DESC table; |
| Comments and collations too | SHOW FULL COLUMNS FROM table; |
| Search or report across many tables | information_schema.columns |
| Exact DDL including indexes and foreign keys | SHOW CREATE TABLE table; |
10. Practice Exercises
Exercise 1: Show only the columns of patients that have a default value.
SHOW COLUMNS FROM patients WHERE `Default` IS NOT NULL;
Result: 2 rows
| Field | Type | Default |
|---|---|---|
| is_active | tinyint(1) | 1 |
| created_at | timestamp | CURRENT_TIMESTAMP |
Exercise 2: Using information_schema, list the columns of patients that are part of an index (Key is not empty).
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_NAME | COLUMN_KEY |
|---|---|
| patient_id | PRI |
| mrn | UNI |
| full_name | MUL |
| doctor_id | MUL |
11. Summary
SHOW COLUMNS FROM t;,DESCRIBE t;andDESC t;show name, type, nullability, key, default and extra.SHOW FULL COLUMNSadds collation, privileges and comments.- Filter with
LIKEorWHERE. information_schema.columnssupports searching across tables.SHOW CREATE TABLEgives the complete definition.