Use SHOW COLUMNS to view all columns of a table in MySQL
In MySQL, SHOW COLUMNS statement is used to show information about all the columns of a table
Sometimes, you may want to view information about all columns in a table. MySQL provides two commands to help you do this:
- List all columns in a table using the
DESCstatement - List all columns in a table using the
SHOW COLUMNSstatement
This article describes the usage of the SHOW COLUMNS statement.
SHOW COLUMNS usage
To list all the columns of a table, use the SHOW COLUMNS statement. This is the syntax of MySQL SHOW COLUMNS:
SHOW [FULL] COLUMNS
FROM [database_name.]table_name
[LIKE pattern]
Here,
- The
database_nameis the name of the database. It can be omitted when you have selected a database as the default database. - The
table_nameis the name of the table whose clolums do you want to list. - The
FULLKeyword are used to output more information, including 3 more columns:Collation,PrivilegesandComment. It is optional. - The
LIKE patternis optional, it is used to filter the column names according to the pattern.
Returned columns
The MySQL SHOW COLUMNS statement can return 9 columns, of which Collation, Privileges and Comment these 3 columns are returned only if the keyword FULL is used:
Field- The name of this column.
Type- The data type of this column.
NullYESorNOindicates whether this column can be NULL.Key- Index definition for this column
Default- Default value for this column
Extra- Other additional information. Such as
auto_increment. Collation- Collation for non-binary string columns, otherwise it is
NULL. Privileges- The privileges you have on this column.
Comment- Comment for this column
MySQL DESC Examples
The following example shows how to use MySQL SHOW COLUMNS statement list columns of the actor table in the Sakila sample database.
-
Connect to the MySQL server using the mysql client tool:
mysql -u root -pEnter the password for the
rootaccount and pressEnter:Enter password: ******** -
Just run the following command to try to show all the columns of the
actortable:SHOW COLUMNS FROM actor;At this point, MySQL will return an error: ERROR 1046 (3D000): No database selected . Because you haven’t specified a default database.
-
Specify the database and table name in the
SHOW COLUMNSstatement as following:SHOW COLUMNS FROM sakila.actor;Here is the output:
+-------------+-------------------+------+-----+-------------------+-----------------------------------------------+ | Field | Type | Null | Key | Default | Extra | +-------------+-------------------+------+-----+-------------------+-----------------------------------------------+ | actor_id | smallint unsigned | NO | PRI | NULL | auto_increment | | first_name | varchar(45) | NO | | NULL | | | last_name | varchar(45) | NO | MUL | NULL | | | last_update | timestamp | NO | | CURRENT_TIMESTAMP | DEFAULT_GENERATED on update CURRENT_TIMESTAMP | +-------------+-------------------+------+-----+-------------------+-----------------------------------------------+ -
Use the
SHOW FULL COLUMNSstatement to show complete information:SHOW FULL COLUMNS FROM sakila.actor;Here is the output:
+-------------+-------------------+--------------------+------+-----+-------------------+-----------------------------------------------+---------------------------------+---------+ | Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment | +-------------+-------------------+--------------------+------+-----+-------------------+-----------------------------------------------+---------------------------------+---------+ | actor_id | smallint unsigned | NULL | NO | PRI | NULL | auto_increment | select,insert,update,references | | | first_name | varchar(45) | utf8mb4_0900_ai_ci | NO | | NULL | | select,insert,update,references | | | last_name | varchar(45) | utf8mb4_0900_ai_ci | NO | MUL | NULL | | select,insert,update,references | | | last_update | timestamp | NULL | NO | | CURRENT_TIMESTAMP | DEFAULT_GENERATED on update CURRENT_TIMESTAMP | select,insert,update,references | | +-------------+-------------------+--------------------+------+-----+-------------------+-----------------------------------------------+---------------------------------+---------+Here, compared to the above output, after using the
FULLkeyword, there are 3 more columns in the output:Collation,PrivilegesandComment. -
Use the
USEcommand to set the default database:USE sakila; -
Just run the following command to try to list all columns:
SHOW FULL COLUMNS FROM actor;At this point, the output of this command is the same as
SHOW FULL COLUMNS FROM sakila.actor;. This is because the default database issakilanow. -
Use
LIKEclause to filter columns whose column names start witha:SHOW COLUMNS FROM actor LIKE 'a%';Here is the output:
+----------+-------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------------+------+-----+---------+----------------+ | actor_id | smallint unsigned | NO | PRI | NULL | auto_increment | +----------+-------------------+------+-----+---------+----------------+
Conclusion
In this article, you learned how to use the SHOW COLUMNS statement :
- Use the
FULLkeyword to output more information - Use
LIKEclause to filter results by field name