What are views in PostgreSQL?

Overview

Have you ever wanted to group a related set of rows of a table and store it somewhere to use later? Well, this is what views are used for.

Views cannot be categorized as real tables, but as a subset of some rows or columns of an existing table.

Uses of views in a database

  • User access to the entire table can be controlled, and only the rows specified in a view can be displayed.

  • Users are able to understand the database better, as they do not need to go through the entire table to get the needed data.

  • It is not necessary for a view to contain data from only one table. Data from different tables can be selected and joined in a view.

  • DML operations (DELETE, INSERT, UPDATE) are not applicable on views.

Syntax for creating views


CREATE View viewname AS query;

Example


CREATE View interviewPrep AS
SELECT *
FROM courses
WHERE kind = 'interview';

Syntax for deleting views


DROP View viewname;

Example


DROP View interviewPrep;

Free Resources