SQL DROP COLUMN
The Data Definition Language (DDL) command DROP COLUMN is used in SQL (Structured Query Language). The structure of database objects like tables can be defined, altered, or deleted using DDL statements in SQL. A column can be removed from a database table using the DROP COLUMN command. The table is altered permanently by this command. There is no going back after you make any modifications using this command. So it’s better to be sure before making any changes. Rearranging the database may benefit from dropping or deleting a column. Making the database more efficient involves removing any superfluous or unneeded columns that have no bearing on the table’s context.
The Syntax for the DROP COLUMN Command:
ALTER TABLE Name_of_the_table
DROP COLUMN Name_of_the_column ;
here “Name_of_the_table” and “Name_of_the_column” are placeholders.
Implementation of DROP COLUMN Command
Let’s consider a table named w3wiki. This table consists of rank, name, age, monthly score, questions solved, and overall score as the columns of the table w3wiki.
Query
CREATE TABLE w3wiki (
rank int,
name varchar(100),
age int,
monthly_score int,
questions_solved int,
overall_score int
);
---first we create a table name w3wiki
INSERT INTO w3wiki(rank,name,age,monthly_score,questions_solved,overall_score)
VALUES (01, 'Vishu', 20 ,272 ,415 ,1448);
INSERT INTO w3wiki(rank,name,age,monthly_score,questions_solved,overall_score)
VALUES (02, 'Kuntal', 20 ,271 ,410 ,1446);
INSERT INTO w3wiki(rank,name,age,monthly_score,questions_solved,overall_score)
VALUES (03, 'Priyam', 20 ,270 ,408 ,1440);
INSERT INTO w3wiki(rank,name,age,monthly_score,questions_solved,overall_score)
VALUES (04, 'Shailesh', 21 ,268 ,407 ,1438);
INSERT INTO w3wiki(rank,name,age,monthly_score,questions_solved,overall_score)
VALUES (05, 'Avirup', 20 ,267 ,406 ,1437);
INSERT INTO w3wiki(rank,name,age,monthly_score,questions_solved,overall_score)
VALUES (06, 'Neeraj', 21 ,265 ,405 ,1436);
---We insert values to the table
SELECT * FROM w3wiki;
---we are now displaying the values from the table
Output
Now, in the table “w3wiki,” we can figure out that the “age” column holds no value to the table data. It is irrelevant to the other columns data. So we decided to remove it from the table. With the DROP COLUMN command, we can remove the age column without hampering the table data.
Query
Here comes the DROP COLUMN command in the picture.
ALTER TABLE w3wiki
DROP COLUMN age ;
---Dropping the column age
SELECT * FROM w3wiki;
--- Displaying the table
Output
Note:
Remember that SQL does not care about case. The way SQL commands are typically written,
though, is as just described. But that does meant that you have to write it in that way though it is highly
recommended to use a standard way when you are dealing in professional scenarios.
Conclusion
DROP COLUMN is a Data Definition Language (DDL) command in SQL. For a variety of particular goals, such as database reorganization and deleting obsolete or unnecessary columns to boost overall productivity and data management, the DROP COLUMN command is used to remove a column from a table. This command permanently changes the structure of the table. Therefore, before doing the operation, it is imperative to confirm that you want to delete those particular columns.
Contact Us