JIYIK CN >

Current Location:Home > Learning > DATABASE > MySQL >

Get column names in MySQL

Author:JIYIK Last Updated:2025/04/25 Views:

In this article, we aim to explore how to get the column names of a specific table in MySQL database.

Often, when working with data in MySQL, we tend to forget the column names of a particular table in the database and the data types of different columns.

MySQL helps us get this information to read, get, update, or delete any information with the correct definition and name. There are several ways to do this.

Following are the different methods that can be used to get the column names of a particular table in MySQL:

  • Use DESCRIBEthe statement.
  • Use SHOWthe statement.

Let us understand how each of these methods works.

Before we begin, we create a dummy dataset to work with. We create a table student_informationand a few rows.

-- create the table student_information
CREATE TABLE student_information(
  stu_id int,
  stu_firstName varchar(255) DEFAULT NULL,
  stu_lastName varchar(255) DEFAULT NULL,
  primary key(stu_id)
);
-- insert rows to the table student_information
INSERT INTO student_information(stu_id,stu_firstName,stu_lastName) 
 VALUES(1,"Preet","Sanghavi"),
 (2,"Rich","John"),
 (3,"Veron","Brow"),
 (4,"Geo","Jos"),
 (5,"Hash","Shah"),
 (6,"Sachin","Parker"),
 (7,"David","Miller");

DescribeUse the statement to get the column name in MySQL

MySQL provides us with DESCRIBEstatements to obtain information related to a specific table. DESCor Describecan help us understand the overall structure of the table. This information includes column names, data types, default values, column types, etc.

The basic syntax of the statement is as follows.

DESCRIBE name_of_the_table;

DESCRIBEAnother way to use the statement is as follows.

DESC name_of_the_table;

Now let us use this statement to get student_informationthe names of all the columns of the table. We can do this using the following query.

DESCRIBE student_information;

The output of the above query is as follows.

Field			Type			Null		Key		Default		Extra
stu_id			float			NO			PRI		-			-
stu_firstName	varchar(255)	YES			-		-			-
stu_lastName	varchar(255)	YES			-		-			-

As we can see above, we have Field, Type, Null, Key, Defaultand Extra.

FieldIndicates student_informationthe column name of . TypeIndicates the data type of each column.

NullIndicates whether the values ​​in the column can take null values. KeyIndicates the type of the key associated with the column name.

As we can see, stu_idshows that PRIit represents the primary key of our table. Finally, we have DEFAULT, which describes any default value set for the column, while Extrastores any extra information associated with the column.

ShowUse the statement to get the column name in MySQL

ShowThe statement is Describean alternative to the statement. showThe basic syntax of the statement is as follows.

SHOW COLUMNS FROM `name_of_the_table`;

Here, name_of_the_tablerepresents the name of the table that contains the columns to be retrieved. We can get the names of all the columns showfrom the table using the statement student_informationas shown below.

SHOW COLUMNS FROM `student_information`;

The output of the above query will give us the following result.

Field			Type			Null		Key		Default		Extra
stu_id			float			NO			PRI		-			-
stu_firstName	varchar(255)	YES			-		-			-
stu_lastName	varchar(255)	YES			-		-			-

As shown in the output above, SHOWand DESCRIBEcan help us get the column names and other useful information of the table. Therefore, with the help of the above techniques, we can efficiently get the column names of the table in MySQL.

Previous:Get the version in MySQL

Next: None

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Get the version in MySQL

Publish Date:2025/04/24 Views:170 Category:MySQL

In this article, we will learn how to get the current version of MySQL on Windows systems. It is necessary for businesses to maintain versions of different tools and software used to run their business in order to keep systems compatible. T

Full Join in MySQL

Publish Date:2025/04/24 Views:71 Category:MySQL

This article aims to explore how to perform a full join or full outer join in MySQL. Full outer join is used to merge or combine the entire data from two separate tables. For example, suppose we have two tables named student_id and student_

Grouping by month in MySQL

Publish Date:2025/04/24 Views:166 Category:MySQL

In this article, we will learn how to group values ​​by month in MySQL database. Businesses and organizations have to find user or customer data based on purchase or usage trends over a few months. If a particular business achieves its

Enabling the slow query log in MySQL

Publish Date:2025/04/24 Views:177 Category:MySQL

Today, we will enable MySQL in MySQL using MySQL shell on Windows and Ubuntu 20.04 slow_query_log . For this tutorial, we are using MySQL version 8.0 and Ubuntu 20.04. MySQL slow_query_log MySQL slow_query_log contains SQL statements that t

Update multiple tables in MySQL with one query

Publish Date:2025/04/24 Views:66 Category:MySQL

In some cases, users want to update logically related tables at the same time. These logically related tables are linked to each other through some attributes. Advantages of updating multiple tables in one MySQL query Similar attributes in

Checking MySQL version in macOS

Publish Date:2025/04/24 Views:60 Category:MySQL

In this article, we aim to explore how to check the current version of MySQL on macOS. Checking MySQL version in macOS When trying to figure out the version, you must follow these steps. Each time a person logs into the MySQL server, the ve

Common table expressions in MySQL

Publish Date:2025/04/24 Views:169 Category:MySQL

This article aims to understand how to use common table expressions in MySQL. Most data analysts need to store the results of different queries in order to merge them with a separate query. With the help of common tables, expressions can ma

Sorting by date in MySQL

Publish Date:2025/04/24 Views:157 Category:MySQL

This article aims to understand how to sort values ​​by date in MySQL. Most of the businesses and organizations that use MySQL for data analysis or data visualization need to sort different table values ​​of their users based on dat

Sort MySQL data alphabetically

Publish Date:2025/04/24 Views:154 Category:MySQL

In this article, we aim to explore how to sort data alphabetically in MySQL database. Sorting is the ordering of elements or values ​​in an array or column based on a particular criteria. In this tutorial, we will set the criteria as al

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial