Wednesday, December 18, 2024

SQL DROP COLUMN Keyword

 The DROP COLUMN keyword in SQL is used to remove a column from an existing table. This operation permanently deletes the column and its data from the table, so it should be done with caution.

Syntax:

ALTER TABLE table_name
DROP COLUMN column_name;

Parameters:

  • table_name: The name of the table from which you want to remove the column.
  • column_name: The name of the column to be removed.

Example:

To remove a column named age from a table called employees, you would write:

ALTER TABLE employees
DROP COLUMN age;

Important Notes:

  • Data Loss: When a column is dropped, all the data stored in that column is lost.

  • Multiple Columns: Some SQL databases allow you to drop multiple columns at once. For example:

    ALTER TABLE employees
    DROP COLUMN age, DROP COLUMN address;
    
  • SQL Compatibility: Not all SQL databases may support dropping multiple columns at the same time. Be sure to check the specific syntax for your database system (e.g., MySQL, PostgreSQL, SQL Server).

  • Restrictions: You cannot drop columns that are part of a primary key or are referenced in foreign keys without first removing the constraints.

No comments:

Post a Comment