PostgreSQL – DROP VIEWS Statement
Last Updated :
18 Jul, 2024
A view in PostgreSQL can be seen as a virtual table that can contain all rows of a table or selected rows from one or more tables. Views allow us to see limited data instead of the complete information stored in a table. A view can be created from one or many base tables (the table from which the view is created). Various operations can be performed on views, such as creating, updating, and deleting views. Here, we will primarily focus on dropping or deleting views.
What is a View?
A view is a virtual table that provides a way to present data from one or more tables in a specific format. It can include all rows from a table or a subset of rows with specific columns. Views help in simplifying complex queries, enhancing security by restricting access to specific data, and maintaining data consistency.
To understand the DROP VIEW statement, we’ll take a sample table named “Students” as shown here:
PostgreSQL
CREATE TABLE Students (
admit_id SERIAL PRIMARY KEY,
stu_name VARCHAR(255) NOT NULL,
branch VARCHAR(255) NOT NULL,
marks INTEGER
);
INSERT INTO Students (stu_name, branch) VALUES
('101', 'Ravi', 'Science', '95'),
('102', 'Rohan', 'Science', '90'),
('103', 'Rajesh', 'Commerce', '95'),
('104', 'Ritika', 'Commerce', '92')
('105', 'Sagar', 'Arts', '99')
('106', 'Sahil', 'Arts', '90')
('107', 'Sakshi', 'Science', '95');

Students table
PostgreSQL DROP VIEW Statement
As the name suggests, The DROP VIEW statement removes a view from the database.
Syntax:
The basic DROP VIEW syntax is as follows:
DROP VIEW [IF EXISTS] view_name
[CASCADE | RESTRICT]
Parameters:
Now let us understand the meaning of the keywords used in the above syntax one by one:
- DROP VIEW: Keywords used to specify the name of the view that needs to be dropped.
- IF EXISTS: Ensures that the view is dropped only if it exists. If the view does not exist, no error is thrown.
- view_name: The name of the view to be dropped.
- RESTRICT: Does not allow the view to be deleted if any other objects depend on it. This is the default option.
- CASCADE: Automatically drops objects that depend on the view and all objects that depend on those objects.
Dropping Multiple Views
It is also possible to delete multiple views using the single DROP VIEW statement using the following syntax:
DROP VIEW [IF EXISTS] view_name1, view_name2, ...;
Creating Views
Now to understand the DROP VIEW statement, we need to first create a view that we can drop/delete later. Below is the statement that creates a view named “StudentsView” based on the information stored in the table “Students“.
CREATE OR REPLACE VIEW StudentsView
AS
SELECT
admit_id,
stu_name,
branch
FROM
Students
WHERE branch ='Science';
Output :

StudentsView
So we have successfully created a view that contains three columns (‘admit_id’, ‘stu_name’, ‘branch’)from the original table. Let us create one more view named “myview” that is based on the ‘StudentsView’ using the following statement:
CREATE OR REPLACE VIEW myview
AS
SELECT
admit_id,
stu_name
FROM
StudentsView
WHERE stu_name ='Sakshi';
Output :

myview
Since this view has been created from the first view named “StudentsView“, hence other objects depend on “StudentsView“. Now let us try to drop the view “StudentsView” using the DROP VIEW statement as shown here:
DROP VIEW StudentsView;
Output:
ERROR: cannot drop view studentsview because other objects depend on it
DETAIL: view myview depends on view studentsview
HINT: Use DROP … CASCADE to drop the dependent objects too.
Since RESTRICT was the default option hence it will not allow the view to be deleted if any other objects are depending on it and hence an error has been raised here. However, if we try to drop the ‘myview’, it can be simply dropped.
DROP VIEW myview;
Output:
DROP VIEW
Query returned successfully in 73 msec.
So this how we can create and drop views in PostgreSQL. To check whether the view has been successfully dropped or not, simply run the ‘SELECT * FROM view_name’ if it still exists or has been dropped. In this case, if we run the below Query:
SELECT * FROM myview
then an error will be raised showing that myview does not exist as we have already dropped “myview“.
Similar Reads
PostgreSQL UPDATE Statement
The PostgreSQL UPDATE statement is an important SQL command used to modify existing data in one or more rows of a table. It allows users to update specific columns or multiple columns at once, using conditions defined in the WHERE clause. This command is highly flexible, enabling dynamic data manage
5 min read
SQL CREATE VIEW Statement
The SQL CREATE VIEW statement is a very powerful feature in RDBMSs that allows users to create virtual tables based on the result set of a SQL query. Unlike regular tables, these views do not store data themselves rather they provide a way of dynamically retrieving and presenting data from one or ma
4 min read
PostgreSQL EXPLAIN Statement
When we work with large databases, query performance becomes critically important to ensure efficient data retrieval. One of the most effective tools to analyze and improve query performance in PostgreSQL is the EXPLAIN statement. EXPLAIN provides detailed information on how PostgreSQL executes quer
5 min read
PostgreSQL - DROP TABLE
In PostgreSQL, the DROP TABLE statement is a powerful and permanent command used to delete one or more tables from a database. Since this operation cannot be undone, it is essential to understand how to use it safely and to be aware of its options to prevent accidental data loss. In this article, we
5 min read
MySQL CREATE VIEW Statement
MySQL, an open-source relational database management system, offers a variety of features to manage and manipulate data efficiently. One of these features is the CREATE VIEW statement, which allows you to create a virtual table known as a view. A view provides a way to simplify complex queries, enha
5 min read
PostgreSQL - DROP SCHEMA
PostgreSQL offers a powerful schema management system, allowing database administrators to organize and manage objects within specific schemas. The DROP SCHEMA statement in PostgreSQL provides a straightforward way to delete entire schemas and the associated objects, making it a valuable tool for da
4 min read
MySQL - ALTER VIEW Statement
The ALTER VIEW statement in MySQL is a powerful tool that allows users to modify the definition of an existing view without the need to drop and recreate it. This statement is particularly useful for changing the query or structure of a view to better help the needs of the application or database de
5 min read
PostgreSQL - DROP TRIGGER
The DROP TRIGGER statement in PostgreSQL is essential for managing database triggers effectively. Triggers in PostgreSQL are database callbacks that automatically execute functions in response to certain events, such as INSERT, UPDATE, or DELETE. DROP TRIGGER provides database administrators and dev
5 min read
MySQL DROP TABLE Statement
MySQL is a free and open-source relational database management system written in C and C++ that is extremely popular among developers. Like other relational database management systems, MySQL provides various rich features to create databases and tables, insert data in them, and further manipulate t
6 min read
PostgreSQL - Create updatable Views
Views in PostgreSQL provide a way to represent a subset of a real table, selecting certain columns or rows from an ordinary table. They are particularly useful for restricting access to the original table, allowing users to see only a specific portion of it. The table from which a view is created is
3 min read