Updated on 2025-05-29 GMT+08:00

DROP VIEW

Description

DROP VIEW deletes a view from a database.

Precautions

The owner of a view, owner of the schema of the view, users granted the DROP permission on the view, or users granted the DROP ANY TABLE permission can run the DROP VIEW command. When separation of duties is disabled, system administrators have this permission by default.

Syntax

DROP VIEW [ IF EXISTS ] view_name [, ...] [ CASCADE | RESTRICT ];

Parameters

  • IF EXISTS

    Reports a notice instead of an error if the specified view does not exist.

  • view_name

    Specifies the name of the view to be deleted.

    Value range: an existing view name

  • CASCADE | RESTRICT
    • CASCADE: automatically deletes the objects (such as other views) that depend on the view.
    • RESTRICT: refuses to delete the view if any objects depend on it. This is the default action.

Examples

-- Create the test_tb1 table and insert 100 data records into the table.
gaussdb=# CREATE TABLE test_tb1(col1 int, col2 int);
gaussdb=# INSERT INTO test_tb1 VALUES (generate_series(1,100),generate_series(1,100));

-- Create a view whose value of col1 is less than 3.
gaussdb=# CREATE VIEW test_v1 AS SELECT * FROM test_tb1 WHERE col1 < 3;

-- Query the view.
gaussdb=# SELECT * FROM test_v1;
 col1 | col2 
------+------
    1 |    1
    2 |    2
(2 rows)

-- Delete the table and the view.
gaussdb=# DROP VIEW test_v1;
gaussdb=# DROP TABLE test_tb1;

Helpful Links

ALTER VIEW and CREATE VIEW