Introduction

SQL allows a user to remove one or more columns from a given table in the database if they are no longer needed or become redundant. To do so, the user must have ALTER permission on the object.

Let’s begin with the syntax for using the ALTER TABLE DROP COLUMN statement.

Syntax

ALTER TABLE table_name

DROP COLUMN column_name;

In the syntax mentioned above, 

First, you define the table name from which you wish to remove or delete the column.

Second, you write the column name you want to delete in the DROP clause.

A user can also drop multiple columns simultaneously from a table. You can do this by listing out the column names that you want to drop, separated by a comma. Refer to the syntax below for a clear understanding.

Syntax

ALTER TABLE table_name     

DROP COLUMN   

    column_name1,  

    column_name2,  

    ...,  

    column_name_n;  

Now that you have looked at the syntax, it’s time to see an example for each of the cases above.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Dropping One Column

To drop a column from the table, first, create a table named ‘Students’ with the following statements.

DropInSQL_1

DropInSQL_2

As a result of these statements, you’ll have the table created as given below:

DropInSQL_3.

Now, you will have to use the ALTER command to drop a column from the table.

DropInSQL_4

Note that when you use the ALTER command, it gives the output as Table altered. Now, if you check your table, it would look like how it is depicted below.

DropInSQL_5.

Now, the AGE column has been dropped out or deleted from the STUDENTS table. Next up, you will look at how to delete multiple columns simultaneously. 

Learn From The Best Mentors in the Industry!

Automation Testing Masters ProgramExplore Program
Learn From The Best Mentors in the Industry!

Dropping Multiple Columns

In this example, you will drop the Name and Address columns from the table created.

DropInSQL_6.

Output-

DropInSQL_7

Now, as you can see, you are only left with the ID column in the ‘Students’ table.

Drop a Column Which is a Foreign Key

If you have created a foreign key in a table, and if you wish to drop that foreign key, you can easily do this using the ALTER command along with the DROP constraint statement. Let’s have a look at the syntax for dropping a foreign key.

Syntax:

ALTER TABLE table_name

DROP CONSTRAINT fk_name;

Here, table_name represents the table from which the foreign key has to be dropped, and fk_name represents the column name that is the foreign key in the table to be altered.

DropInSQL_8

This is our parent table that has product_id as its primary key.

DropInSQL_9

This statement represents the child table “shop” comprising shop_id as its primary key and fk_shop_product_id as its foreign key that relates to the product_id field in the parent table.

DropInSQL_10

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Using the above statement, you dropped the foreign key in the child table.

Wrapping up

Here’s a quick summary of the topics covered in this tutorial. First, you learned that SQL provides the user with permission to drop a column that is no longer required using the ALTER command. Next, you saw that there are two ways to drop a column in SQL, you can either drop a single column or multiple columns simultaneously. After that, you covered how you can drop a foreign key in the table using the DROP constraint in the ALTER command.

Leran SQL and other top programming tools with out PGP Full Stack Web Development Program. This course provides a high-engagement learning experience with real-world applications and is designed for individuals who want to start a new, more fulfilling career.

Have any questions for us? Leave them in the comments section of this article. Our experts will get back to you on the same, as soon as possible!

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 17 Jun, 2024

6 Months$ 8,000
Full Stack Developer - MERN Stack

Cohort Starts: 30 Apr, 2024

6 Months$ 1,449
Automation Test Engineer

Cohort Starts: 1 May, 2024

11 Months$ 1,499
Full Stack Java Developer

Cohort Starts: 14 May, 2024

6 Months$ 1,449