Delete the column if it exists in MySQL
In this article, we will learn how to delete single or multiple columns present in MySQL.
If the column exists in MySQL, then delete the column
In a table, a column is a row of cells that can contain text, numbers, and graphics. For each row in the table, a value is stored in each column.
We may want to remove single or multiple columns from a table. ALTER TABLE
Columns of a table can be added, modified or deleted/removed using MySQL commands.
When columns are dropped from a table, they are also dropped from any indexes they were a part of. If you drop all the columns that make up an index, the index is also dropped.
The IF EXISTS clause is used only for deleting databases, tables, and views. When using IF EXISTS, if the object we are trying to delete does not exist, the execution will be interrupted as soon as MySQL notices that the entity does not exist and sends a warning.
The system schema is the schema that MySQL uses. It includes the tables and columns that contain the data needed by a running MySQL server.
The MySQL schema is broadly divided into system tables for general operational purposes and data dictionary tables for storing metadata about database items. The COLUMNS table contains detailed information about table columns.
grammar:
DROP COLUMN column_name ON table_name;
ALTER TABLE table_name DROP COLUMN column_name;
ALTER TABLE table_name DROP COLUMN column_name_1, DROP COLUMN column_name_2;
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = table_name AND COLUMN_NAME = column_name
Here, column_name is the name of the column to be dropped, and table_name is the name of the table from which the column is to be dropped. Because the term COLUMN in the DROP COLUMN clause is optional, you can use the following shorter statement:
ALTER TABLE table_name DROP column_name;
The second-to-last phrase demonstrates how you can ALTER TABLE
perform a large number of delete operations with a single query if you separate the operations with commas.
To further understand the previous concepts, consider the following example:
DROP COLUMN email ON Employees;
ALTER TABLE Employees DROP COLUMN email;
IF EXISTS (SELECT 1
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Employees'
AND COLUMN_NAME = 'email')
BEGIN
ALTER TABLE Employees
DROP COLUMN email
END
GO
In the above example, we are deleting the Email column of the Employees table. This will delete the Email column, any saved data, and any associated indexes.
If you want to make sure that the Email column is in the Employees table, use the last command, which INFORMATION_SCHEMA
checks for it and deletes if it returns true.
Run the above lines of code in any MySQL compatible browser. It will display the following result.
Output:
Query executed successfully.
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.
Related Articles
Display tables and database structure in MySQL
Publish Date:2025/04/23 Views:97 Category:MySQL
-
Today, we will learn about queries in MySQL that can display the table and database structure. We will use the mysqldump utility, DESCRIBE the , SHOW TABLES and SHOW CREATE TABLE the statements. We are using MySQL version 8.0.28 while writi
Select first row from MySQL table
Publish Date:2025/04/23 Views:112 Category:MySQL
-
Today, we will explore three scenarios and their solutions where we want to select the first row from a MySQL table. In the first scenario, we will learn to get the first row from a MySQL table where there are multiple instances of a partic
Insert timestamp into MySQL table
Publish Date:2025/04/23 Views:77 Category:MySQL
-
Today, we will learn how to TIMESTAMP insert date and time into a type column of a MySQL table according to the table definition. Create a MySQL table First, we will create the tables that we will use in this tutorial. Sample code: CREATE T
The difference between two tables in MySQL
Publish Date:2025/04/23 Views:102 Category:MySQL
-
In this article, we will learn how to find the difference between two tables in MySQL. The difference between two tables in MySQL We often need to compare two tables to find records in one table that have no matching records in the other ta
MySQL sorts data alphabetically
Publish Date:2025/04/23 Views:129 Category:MySQL
-
In this article, we will learn about various ways to sort data alphabetically in MySQL. Sort MySQL data alphabetically When you use the SELECT command to query data from a table, the rows in the result set are in arbitrary order. To order t
Display the current database in MySQL
Publish Date:2025/04/23 Views:199 Category:MySQL
-
This article focuses on the various queries that can be used to display the current database in MySQL. We will learn by using the Windows command line and MySQL Workbench. Display the current database in MySQL We can use the following query
Check if a database exists in MySQL
Publish Date:2025/04/23 Views:179 Category:MySQL
-
In this article, we will introduce many ways to check if a database exists in MySQL. Check if the database exists in MySQL The system schema is the schema that MySQL uses. It includes tables that contain data needed by a running MySQL serve
Get the sum of multiple columns in MySQL
Publish Date:2025/04/23 Views:125 Category:MySQL
-
In this article, we will learn how to sum multiple columns in MySQL. Sum multiple columns in MySQL You can use aggregate functions SUM() to calculate the total value in a collection. SUM() The function calculation does not consider NULL val
MySQL ForEach Loop
Publish Date:2025/04/23 Views:164 Category:MySQL
-
This article describes how to simulate a foreach loop in MySQL using INSERT, SELECT, WHERE, and JOIN in one statement. MySQL foreach loop To understand foreach loop simulation, let us create three tables with the following names and attribu