Introduction to Postgres Create View
Views are the pseudo-tables that in reality are not materialized. The records of view do not actually consume any memory in the physical database. The views are created on the basis of certain existing temporary or permanent tables. The columns are combined or are specified from the existing tables while view creation which is in turn called as the base tables. The views which are created on the basis of temporary tables are considered as temporary and are dropped once the session associated with it is closed. Whenever a view is referred, internally a query is run on the base tables to retrieve the records of the view.
In this article, we will learn about how to create views and change or replace them and fetch the records from the view, and finally how to delete the view using the DROP command. All the permission allocation and ownership rules and manipulations of the views in PostgreSQL are the same and remain unchanged. Also, you should note that no updation, insertion, and deletion operations are permitted on the views. However, we can internally write the triggers on the view where we can write the queries for changing the base tables whenever the corresponding change is done on the view. Views can be created from one or more base tables and may contain one or more columns in it. We can retrieve the records from the view in the same fashion as we do for the tables. We will begin by learning the syntax to create the view in PostgreSQL.
Syntax:
CREATE [ OR REPLACE ] [ TEMP | TEMPORARY ] VIEW nameOfView [ ( nameOfColumn [, ...] ) ]
[ WITH ( view_option_name [= view_option_value] [, ... ] ) ]
AS querySpecifyingRecords
Parameters:
- TEMPORARY or TEMP – This is the optional statement that can be used in the query if you want to declare the target view as the temporary view. By default, the views are permanent in nature. Only in the case when the views are created using temporary base tables. the view is also considered temporary even if not mentioned in the query. Otherwise, until and unless specified in the query they are permanent in nature.
- NameOfView – This is used for specifying the name of the target view that you want to create.
- NameOfColumn – These are optional column names that can be specified for preparing the view that will contain columns named as specified. If not specified then they are deduced from the querySpecifyingRecords query which refers in turn to the base tables.
- view_option_name – We can provide any optional parameters while creating the view using this syntax. For now, only one type of parameter is available to pass named security_barrier that helps to provide row-level security to the view when enabled.
- QuerySpecifyingRecords – This can be SELECT query or the VALUES that help to specify rows and columns to be inserted in the view.
Example of Postgres Create View
To create the view we need to have the base tables. Let us check by typing the command \dt on psql prompt. As can be seen, only one table named educba exists in my postgres database. Let us create two tables named teams and developers and insert some values in both of them.
CREATE TABLE teams (
id SERIAL PRIMARY KEY,
team_count INTEGER,
department VARCHAR (100)
);
Now, we will create a table for developers that will act as the referencing or child table. There will be one to many relationships between teams and developers’ tables. team_id will be our referencing key which will refer to the id of the team’s table.
CREATE TABLE developers (
developer_id INTEGER NOT NULL,
team_id INTEGER REFERENCES teams (id),
name VARCHAR (100),
position VARCHAR (100),
technology VARCHAR (100),
PRIMARY KEY (developer_id,team_id)
);
INSERT INTO teams (id, team_count, department) VALUES('1','5','Accounting');
INSERT INTO teams (id, team_count, department) VALUES('2','6','Inventory');
INSERT INTO teams (id, team_count, department) VALUES('3','5','Human Resource');
INSERT INTO teams (id, team_count, department) VALUES('4','7','CRM');
INSERT INTO teams (id, team_count, department) VALUES('5','9','Bug Solver');
INSERT INTO teams (id, team_count, department) VALUES('6','4','Document');
INSERT INTO developers (developer_id, team_id, name, position, technology) VALUES(1,2,'Payal','senior SD','Java');
INSERT INTO developers (developer_id, team_id, name, position, technology) VALUES(2,1,'Heena','Developer','Angular');
INSERT INTO developers (developer_id, team_id, name, position, technology) VALUES(3,2,'Sayali','Developer','Hibernate');
INSERT INTO developers (developer_id, team_id, name, position, technology) VALUES(4,3,'Rahul','Support','Digital Marketing');
INSERT INTO developers (developer_id, team_id, name, position, technology) VALUES(5,3,'Siddhesh','Tester','Maven');
as shown below –
Now, let us check the contents of both tables teams and developers using query statements –
SELECT * FROM teams;
SELECT * FROM developers;
Firstly we will create a simple view which will retrieve the records from one table only developers with name and position columns with records whose team is “Inventory” means its team id is 2. For doing this our query statement will be as follows –
CREATE VIEW inv_team AS SELECT name,position FROM developers WHERE team_id = 2;
which will result into following output. To verify our view’s contents let us check them by firing the query
SELECT * FROM inv_team;
Note: that I closed the session and again opened the psql prompt and fired the select query which retrieved correct records which means that the view that we created is permanent one as we didn’t specified TEMP or TEMPORARY in the create view query.
Now, we will create a temporary view that will contain the name of the developer and its team’s department. That means there will be two base tables. Let us begin by framing or query statements.
CREATE or REPLACE TEMPORARY VIEW team_details AS SELECT a.department, b.name FROM teams a INNER JOIN developers b ON a.id = b.team_id;
which results in the following output –
Let us check its contents using the query
SELECT * FROM team_details;
Now, as it is a temporary view let us close our session and check whether we get the same output after beginning a new session.
As it can be seen the team_details view does not exist anymore as the session in which that temporary table was created was closed and hence the view deleted.
Now, let us check contents of inv_team view inn this new session.
SELECT * FROM inv_team;
To delete this view, we will need to use the DROP VIEW command as follows –
DROP VIEW inv_team;
and check its contents by using the SELECT statement as above which gives output –
So, the view is deleted permanently.
Conclusion
The views can be created on one or more base tables having one or more columns which can be either temporary or permanent in nature in PostgreSQL. DROP VIEW command is used to remove the view completely.
Recommended Articles
This is a guide to Postgres Create View. Here we discuss the introduction of Postgres Create View, syntax, parameters, and examples with code implementation. You may also have a look at the following articles to learn more –