×

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

SQL SELECT AND Operator

This SQL tutorial explains and helps us understand how to use the AND Operator in the SELECT query with examples. The AND Operator is used to fetch the table’s records if...

2 minutes read.

SQL Syntax

In this tutorial, we will help you understand about the different SQL Syntax concepts with the help of an example. Every Structured Query Language starts with Keywords like select, insert, update,...

5 minutes read.

SQL CRUD Operation

In any computer language, CRUD operation is a foundation. CRUD operation rules are applied in databases as well. These operations are the basic operations used to perform with any database...

7 minutes read.

SQL Tutorial for Beginners

SQL tutorial provides basic and advanced concepts of Structured Query Language and how you deploy SQL to work with a relational database system. Our SQL tutorial is designed for beginners...

3 minutes read.

SQL GROUP BY CLAUSE

The GROUP BY clause is used to arrange similar records into the groups in the Structured Query Language queries. The records are arranged with the help of functions which is...

4 minutes read.

TCL Commands in SQL

In Structured Query Language, TCL is an abbreviation for Transaction Control Language. A single unit of work in a database is formed after the consecutive execution of commands is known...

6 minutes read.

Trigger in SQL

In this article, we will learn about the concept of trigger in SQL and its implementation with the help of an example. A Trigger in Structured Query Language is a set...

4 minutes read.

SQL Insert multiple rows

This article will deal with a new insertion method in SQL which inserts multiple rows at a time. Multiple records can be inserted at a time into a specific table with...

4 minutes read.

Commit and Rollback in SQL

In Structured Query Language, we have Data Definition Language (DDL) and Data Manipulation Language (DML) commands the same way we have Transaction Control Language (TCL) commands in the Structured Query...

4 minutes read.

SQL Queries

In a database, queries are used to request the result set of data from the table or action on the records. A Query can answer your simple or complicated question, perform...

5 minutes read.

SQL Data Manipulation Language

Data Manipulation Language manipulates/make changes in data present in a table. It only affects data/records of table, not on the schema/structure of table. INSERT, UPDATE, DELETE are the commands of DML. INSERT: Stores...

2 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 use GROUP BY clause in SQL

In this SQL article, we will learn about the GROUP BY clause and how to use it in SQL. We will also discuss using the GROUP BY clause with the...

6 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.

How to delete a row in SQL

Introduction To delete or remove the unused/unwanted rows from a table, DELETE command is used in SQL.DELETE command removes the entire record from a table.The DELETE statement can delete one or...

6 minutes read.

Difference between Delete, Drop and Truncate in SQL

What is the Delete command in SQL? In DML (Data Manipulation Language), we use the delete command that allows us to delete the some entries and modify the databases in SQL....

4 minutes read.

How to remove duplicates in SQL

Introduction There are some specific rules that needs to be followed while creating the database objects. To improve the performance of a database, a primary key, clustered and non-clustered indexes, and...

9 minutes read.

DDL Commands in SQL

DDL stands for Data Definition Language. Data Definition Language is a bunch of SQL commands used to create, modify and delete Database schema but not the records or data. Data Definition...

5 minutes read.

Where vs Having

Difference Between Where and Having The WHERE and HAVING clauses in SQL are used to filter records stored in tables in the databases. The dissimilarities between the WHERE and HAVING clause...

3 minutes read.

SQL Operators

Arithmetic Operators Arithmetic operators are +, -, *, /, % performs addition, subtraction, multiplication, division, modulo respectively. Example:   Select 100+222; Select salary+100 From teacher Where teacher_id=1; Following output screen shows different arithmetic operations. We...

2 minutes read.