JIYIK CN >

Current Location:Home > Learning > DATABASE > MySQL >

Deleting a column from a table in MySQL

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

Sometimes we may need to delete single or multiple columns from a database table. ALTER TABLE DROP COLUMNThe command statement in MySQL can delete a column from a table.

Here is the common syntax to accomplish this:

ALTER TABLE table_name DROP COLUMN column_name;

Here is an explanation of the above syntax:

  • The name of the table from which the column will be dropped.
  • DROP COLUMNThe phrase defines the columns to be dropped and their names.

This article will guide you through deleting a column from a table in MySQL.

Create MySQL Database

Step 1: Create a MySQL database and table

The first step is to create a database and a table to demonstrate the above methods.

CREATE DATABASE CountryDB;
USE CountryDB;

CREATE TABLE tbl_Country
 (
     CountryId INT NOT NULL AUTO_INCREMENT,
     CountryCode varchar(50),
     CountryName varchar(50),
     IsActive bit,
     IsDeleted bit,
     PRIMARY KEY (CountryId) 
 );

Step 2: Insert data into MySQL table

To insert data into the created tbl_Countrytable, paste and run the provided code snippet.

INSERT INTO tbl_Country (CountryCode,CountryName,IsActive,IsDeleted)
VALUES ("A","Country A",1, 1), 
		("B","Country B",1,0),
		("C","Country C",1, 1), 
		("D","Country D",1,1);

To view the output, run SELECTthe command.

SELECT * FROM tbl_Country;

Output:

| CountryId | CountryCode | CountryName | IsActive | IsDeleted |
| :-------- | :---------- | :---------- | :------- | :-------- |
| 1         | A           | Country A   | 1        | 1         |
| 2         | B           | Country B   | 1        | 0         |
| 3         | C           | Country C   | 1        | 1         |
| 4         | D           | Country D   | 1        | 1         |

Step 3: Remove columns from the table in MySQL

You can alter tabledelete a column from the following table using MySQL command.

The general syntax for dropping a table column is as follows.

alter table <tblname> drop column <colname>

In this case, to tbl_Countrydelete IsDeletedthe column from the table, paste the following code.

ALTER TABLE tbl_Country DROP COLUMN IsDeleted;

Deleting a single column from a MySQL table

The syntax to delete a single column from a MySQL table is as follows.

ALTER TABLE tbl_Country DROP COLUMN IsDeleted;

To view the output of the above command, run SELECTthe command.

SELECT * FROM tbl_Country; 
| CountryId | CountryCode | CountryName | IsActive |
| :-------- | :---------- | :---------- | :------- |
| 1         | A           | Country A   | 1        |
| 2         | B           | Country B   | 1        |
| 3         | C           | Country C   | 1        |
| 4         | D           | Country D   | 1        |

Deleting multiple columns from a MySQL table

MySQL also provides multiple column deletion. Suppose you want to delete multiple columns at the same time, use the following query, with the column names separated by commas.

ALTER TABLE tbl_Country
  DROP COLUMN IsActive,
  DROP COLUMN CountryName;

Use the following code to display the results:

SELECT * FROM tbl_Country; 

Output:

| CountryId | CountryCode |
| :-------- | :---------- |
| 1         | A           |
| 2         | B           |
| 3         | C           |
| 4         | D           |

in conclusion

Columns in a database contain cells that are used to store values ​​for a particular row in a table. The above discussion covers the use of Delete ALTER TABLEcommand in MySQL to show you how to delete a column from a table given a column name.

Because MySQL is a relational database, it is important to be aware that column deletion affects relational constraints within the database system and its performance during query execution.

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:169 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:70 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:65 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:168 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:156 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:153 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