SQL Standard

SQL Delete Column from Table

It’s not uncommon for you to modify an existing table schema, like from time to time you may need to add or remove columns.

This tutorial will help you learn how to remove a column from a database table using Standard SQL.

SQL Drop Column

In Standard SQL, you can manually remove a column from a table using the ALTER TABLE DROP COLUMN statement.

This DDL statement will attempt to delete the specified column from the table.

The syntax for dropping a column in Standard SQL is shown below:

ALTER TABLE "table_name"
DROP COLUMN "column_name";

Take the example table below:

We can remove the active column as shown in the following query:

ALTER TABLE customer DROP COLUMN active;

Remember that the command will fail if foreign key constraints reference the column.

In some database engines, the statement to remove a column may vary. For example, the syntax above is supported by Standard SQL and database engines, such as Oracle, SQL Server, and PostgreSQL.

However, the command varies slightly in MySQL. The syntax is shown below:

ALTER TABLE "table_name"
DROP "column_name";

In this case, we do not need to specify DROP COLUMN although supported. Example usage is shown below:

ALTER TABLE customer DROP active;

Conclusion

This short article discusses how to delete a column from a database table using Standard SQL and MySQL variants. We hope you found this article helpful. Check the other Linux Hint articles for more tips and tutorials.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list