×

How to drop a column in SQL?

How to drop a column in SQL

Introduction

  • To delete a column from an already created table, one needs to use the ALTER command along with the DROP COLUMN clause.

Syntax:

ALTER TABLE tablename DROP COLUMN ColumnName;

Where,

  1. Tablename is the name of an already existing table from which you have to delete a column.
  2. ColumnName is the name of the column which is to be deleted from an already existing table.

Example 1:

First, we will create a database with name “studentdb”. Then in that database we will create a table “student” and insert records into the table.

Now, we will delete a column ‘City’ from an existing table.

 mysql> USE studentdb;
 Database changed
 mysql> SELECT *FROM student;
 +---------+-----------+-----------+-------------+------+
 | Stud_ID | Stud_Name | Course_ID | Course_Name | City |
 +---------+-----------+-----------+-------------+------+
 |       1 | Prajakta  |       101 | DBMS        | NULL |
 |       2 | Shweta    |       102 | CN          | NULL |
 |       3 | Nikita    |       103 | OS          | NULL |
 |       4 | Ankita    |       104 | C           | NULL |
 |       5 | Ashmita   |       105 | JAVA        | NULL |
 +---------+-----------+-----------+-------------+------+
 5 rows in set (0.00 sec)
 mysql> ALTER TABLE student DROP COLUMN City;
 Query OK, 5 rows affected (0.29 sec)
 Records: 5  Duplicates: 0  Warnings: 0
 mysql> SELECT *FROM student;
 +---------+-----------+-----------+-------------+
 | Stud_ID | Stud_Name | Course_ID | Course_Name |
 +---------+-----------+-----------+-------------+
 |       1 | Prajakta  |       101 | DBMS        |
 |       2 | Shweta    |       102 | CN          |
 |       3 | Nikita    |       103 | OS          |
 |       4 | Ankita    |       104 | C           |
 |       5 | Ashmita   |       105 | JAVA        |
 +---------+-----------+-----------+-------------+
 5 rows in set (0.00 sec) 
drop a column in SQL

Column ‘City’ is removed from an existing student table.

Example 2:

We will delete a column ‘Stud_ID’ along with its primary key.

 mysql> DESC student;
 +-------------+-------------+------+-----+---------+-------+
 | Field       | Type        | Null | Key | Default | Extra |
 +-------------+-------------+------+-----+---------+-------+
 | Stud_ID     | int(11)     | NO   | PRI | NULL    |       |
 | Stud_Name   | varchar(20) | YES  |     | NULL    |       |
 | Course_ID   | int(11)     | YES  |     | NULL    |       |
 | Course_Name | varchar(20) | YES  |     | NULL    |       |
 +-------------+-------------+------+-----+---------+-------+
 4 rows in set (0.02 sec)
 mysql> ALTER TABLE student DROP COLUMN Stud_ID;
 Query OK, 5 rows affected (0.28 sec)
 Records: 5  Duplicates: 0  Warnings: 0
 mysql> DESC student;
 +-------------+-------------+------+-----+---------+-------+
 | Field       | Type        | Null | Key | Default | Extra |
 +-------------+-------------+------+-----+---------+-------+
 | Stud_Name   | varchar(20) | YES  |     | NULL    |       |
 | Course_ID   | int(11)     | YES  |     | NULL    |       |
 | Course_Name | varchar(20) | YES  |     | NULL    |       |
 +-------------+-------------+------+-----+---------+-------+
 3 rows in set (0.01 sec) 
drop a column in SQL

We have used DESC command in SQL to view the structure of student table. Before executing the ALTER command, there exists a primary key on ‘Stud_ID’. After executing the ALTER command along with the DROP column clause, ‘Stud_ID’ is removed from the table structure along with its primary key.


Related Topics

Update Query in SQL

Update is an SQL command that is used to modify the data that in already present in database. Update is a command of DML. DML means Data Manipulation Language. Basically,...

3 minutes read.

SQL Aggregate Functions

In this tutorial, we will learn and understand the SQL Aggregate Functions concept with the help of examples. SQL Aggregate Function is a function used to perform calculation operations on one...

4 minutes read.

SQL INSERT INTO Statement

SQL INSERT INTO statement adds data to the newly created tables or existing tables. We can add single records or multiple records in a table by using this query. There are...

3 minutes read.

SQL SELECT WHERE Clause

In this SQL section, we will help you understand the concept of the SQL SELECT WHERE clause with a few examples. The WHERE clause in SELECT query displays those records as...

5 minutes read.

SQL SELECT IN

SQL SELECT IN is a logical operator in Structured Query Language. It is used in SQL queries to reduce the use of multiple 'OR' operators. s The IN operator in SQL...

6 minutes read.

SQL Aggregate Operators

In SQL, the aggregate operators perform a calculation on multiple rows or multiple values of a single column and give the output a single value. Here, multiple rows of data are...

4 minutes read.

SQL COPY Table

In this tutorial, we will learn how to copy tables in SQL with the help of examples. Using the SELECT INTO statement in the SQL we can copy one table into...

4 minutes read.

How to Update Table in SQL?

How to Update Table in SQL Introduction UPDATE query is used to update a record in a table. UPDATE is a DML command, which operates on the data of the table and not...

4 minutes read.

space() function in SQL

 This function returns a string with a specified number of spaces. If we specify a number, it returns the strings having that specified number of spaces. SPACE Function is available...

2 minutes read.

SQL Commands

SQL commands are classified into four groups on the basis of their nature. DDL (Data Definition Language) DML (Data Manipulation Language) DCL (Data Control Language) DQL (Data Query Language) NOTE: This...

1 minute read.

SQL Cloning Tables

Cloning a Table: To create a copy of the table. To perform the operations, without affecting the actual table. Steps for creating a Cloning Table: Step 1: Empty Table Creation The syntax for Creating...

3 minutes read.

SQL Comparision Operator

The Comparison Operator compares different data of the Structured Query Language table and checks whether the data are the same, less than, greater than, less than, or greater than equal....

14 minutes read.

SQL VIEW

The SQL VIEW concept helps to hide the difficulty of the records and provides limitations to access to the database. The SQL view is similar to the SQL tables. In SQL...

7 minutes read.

SQL CASE

This page contains all the information about SQL CASE. The CASE is an If-Else type of logical query used in the statement. The CASE in Structured Query Language is similar...

5 minutes read.

SQL IN vs SQL EXISTS

SQL IN vs SQL EXISTS This article discusses in detail about the IN and the EXISTS operators in SQL. It is a common question between developers that what is the difference...

3 minutes read.

Drop vs Truncate in SQL

In this article, we will learn and understand the Drop and Truncate commands and the difference between these two commands. What is Drop Command? Drop is a Data Definition Language command in...

6 minutes read.

SQL UNION ALL Operator

This page has all the information about UNION ALL operator, which is used to join all the values from the SELECT queries. Similar records were not considered in the result in...

3 minutes read.

SQL Left Join

The SQL Left Join query displays all the records from the table and displays similar records from the right table. The query displays zero records if it doesn’t find any...

4 minutes read.

SQL SELECT MAX

The SQL Max() function is an aggregate function in SQL. This function returns the values which are greater in the condition. The condition may be a number, or it may...

5 minutes read.

SQL CROSS Join

In this tutorial, we will help you understand the concept of the SQL CROSS Join clause with the few examples. The SQL CROSS Join query is used to join one or...

5 minutes read.