Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • PostgreSQL - Get Started
  • Install PostgreSQL
  • Connect to PostgreSQL DB
  • Create Database
  • Create Table
  • Copy Table
  • Drop Table
  • Drop Database
  • Truncate Table
  • ALTER Table
  • Rename Table
  • Rename Columns
  • Add Columns
  • Modify Column Type
  • Set Default Value of Column
  • Remove Columns
  • Add Constraints to Table
  • Insert Data
  • Upsert Data
  • Update Data
  • Delete Data
  • SELECT Statement
  • WHERE Clause
  • GROUP BY Clause
  • HAVING Clause
  • ORDER BY Clause
  • DISTINCT Clause
  • Inner Join
  • Left Outer Join
  • Right Outer Join
  • Full Outer Join
  • Self Join
  • Natural Join
  • Cross Join
  • LIMIT OFFSET Clause
  • GROUPING SETS
  • GROUPING() Function
  • GROUP BY CUBE
  • GROUP BY ROLLUP
  • Sub Query
  • ALL Operator
  • ANY Operator
  • UNION Operator
  • INTERSECT Operator
  • EXCEPT Operator
  • IS NULL Operator
  • BETWEEN Operator
  • LIKE Operator
  • CAST Operator
  • CASE Expressions
  • NULLIF()
  • COALESCE()
  • GREATEST(), LEAST()
  • WITH Queries (CTE)
  • Constraints
  • NOT NULL Constraint
  • Unique Constraint
  • Check Constraint
  • Primary Key
  • Foreign Key
  • Sequence
  • Serial Type
  • Identity Columns
  • Generated Columns
  • Data Types
  • Boolean Type
  • Character Type
  • Integer Type
  • Numeric Type
  • Date Type
  • Time Type
  • TimeStamp Type
  • Interval Type
  • Array Type
  • Json Type
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

Remove Columns of a Table in PostgreSQL

Use the ALTER TABLE DROP COLUMN statement to drop a column of a table along with existing data, indexes, and constraints created on a column.

Syntax:
ALTER TABLE <table_name>
DROP COLUMN [IF EXISTS] <column_name> [CASCADE | RESTRICT];

Consider that you already have the following employee table.

The following ALTER TABLE statement will remove the email column of the employee table with all its data.

Example: Drop a Column
ALTER TABLE employee
DROP COLUMN email;

Note: If you try to remove column that does not exists, then use IF EXISTS clause. It will remove column only if column exists, otherwise PostgreSQL will just give the warning and ignore the drop command.

Remove Dependent Objects

If a column you are trying to remove is being used in any other database objects like views, triggers, procedures, functions etc., then PostgreSQL will raise an error and will not remove a column. Use the CASCADE clause with the DROP COLUMN statement to remove a column from a table and along with dependent DB objects where it is being used.

For example, the following employee_view view created on the employee table, as shown bellow.

Example: Drop a Column from All Objects
CREATE OR REPLACE VIEW employee_dtl
AS SELECT * FROM employee;

Now, if you try to remove the email column of the employee table, then PostgreSQL will raise an error, as shown below.

To remove a column which is being used in other DB objects, use the CASCADE clause with the DROP command, as shown below.

Example: Drop a Column from All Objects
ALTER TABLE employee
DROP COLUMN email CASCADE;

The CASCADE clause will remove the dependent objects too. The above statement removes the employee_dtl view also.

Remove Multiple Columns

Use multiple DROP COLUMN clause to delete multiple columns in the table.

Example: Drop Multiple Columns
ALTER TABLE employee
DROP COLUMN email CASCADE,
DROP COLUMN gender;

Remove columns using pgAdmin

You can remove one or more columns in pgAdmin by right clicking on the table name and select 'Properties'. In the popup, go to 'Columns' tab wherein you can click on the delete icon infront of the columns to remove it, as shown below.

Click on the Save button to save the changes.

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.