Ticker

6/recent/ticker-posts

Drop Columns from Table in SQL

Drop Columns from Table


Introduction Dropping columns from a table is a common operation in database management systems. It involves removing one or more columns from an existing table. This documentation provides a brief explanation and code examples on how to drop columns from a table using different database management systems.

Table of Contents

  1. Dropping Columns in SQL Server

    1. Syntax
    2. Example
  2. Dropping Columns in MySQL

    1. Syntax
    2. Example
  3. Dropping Columns in PostgreSQL

    1. Syntax
    2. Example

1. Dropping Columns in SQL Server Syntax To drop a column from a table in SQL Server, you can use the ALTER TABLE statement with the DROP COLUMN clause.

sql
ALTER TABLE table_name DROP COLUMN column_name;

Example Consider a table named employees with columns id, name, and age. To drop the age column from the table, you can use the following code:

sql
ALTER TABLE employees DROP COLUMN age;

This code will remove the age column from the employees table.

2. Dropping Columns in MySQL Syntax To drop a column from a table in MySQL, you can use the ALTER TABLE statement with the DROP COLUMN clause.

sql
ALTER TABLE table_name DROP COLUMN column_name;

Example Assume we have a table named customers with columns id, name, and email. To drop the email column from the table, you can use the following code:

sql
ALTER TABLE customers DROP COLUMN email;

This code will remove the email column from the customers table.

3. Dropping Columns in PostgreSQL Syntax To drop a column from a table in PostgreSQL, you can use the ALTER TABLE statement with the DROP COLUMN clause.

sql
ALTER TABLE table_name DROP COLUMN column_name;

Example Let's say we have a table named products with columns id, name, and price. To drop the price column from the table, you can use the following code:

sql
ALTER TABLE products DROP COLUMN price;

This code will remove the price column from the products table.

Conclusion Dropping columns from a table is a straightforward task in most database management systems. By using the appropriate syntax and the ALTER TABLE statement with the DROP COLUMN clause, you can easily remove unwanted columns from your tables.

Post a Comment

0 Comments