PostgreSQL - Create updatable Views
Last Updated :
23 Jul, 2024
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 known as the base table. PostgreSQL supports both updatable and non-updatable views.
Updatable views
Updatable views are particularly useful when you want certain users to update specific columns of certain tables. However, for a view to be updatable, it must meet specific requirements:
- There should be only one entry in the FROM clause of the defining query of the view
- The selection list must not contain any aggregate function such as 'SUM', 'MIN', 'MAX', etc.
- The query defining the view must not include '
GROUP BY'
, 'HAVING'
, 'LIMIT'
, 'OFFSET'
, 'DISTINCT'
, 'WITH'
, 'UNION'
, 'INTERSECT'
, or 'EXCEPT'
statements.
PostgreSQL Create Updatable Views Example
Let us see the following example. Below is the table named "example" that holds some basic data about the employees:
Original tableSyntax to Create the Updatable View
CREATE OR REPLACE VIEW view_name AS
SELECT column1, column2.....
FROM table_name
WHERE [condition];
Creating an updatable view
Now we can create a view from the original table "example", A view can take one or more than one column in the selection list depending on how much access you want to give to your users. Let us create an updatable view "my_view" with three columns 'id', 'name', and 'dept' respectively.
CREATE OR REPLACE VIEW my_view AS
SELECT id,name,dept from example
WHERE dept='Sales';
Output:
CREATE VIEW Query returned successfully in 220 msec.
Querying the results
We can simply see the results of the created view by simply running a SELECT query as follows:
SELECT * FROM my_view;
Output:
my_viewExplanation: The result should show only the rows where dept
is 'Sales'. If there was a single row with 'dept' as 'Sales', you would get only that row in the results.
Inserting in the created view
Now let us try to perform the INSERT operation in the created view using the following syntax and example as well.
Syntax:
INSERT INTO view_name (column1,column2,...columnN)
VALUES(Val1, val2...valN);
Example:
INSERT INTO my_view (id,name,dept)
VALUES(106,'Johnson','Health');
Running the SELECT
query on my_view
will show no change because the new dept
value is 'Health', which does not match the 'WHERE'
clause condition (dept = 'Sales'
). However, this new row will be added to the original table 'example'
.
To verify, run the query:
SELECT * FROM example;
Updated tableExplanation: The output will show the new row added to the 'example'
table, with a NULL
value in the 'dept'
column for the new row and an incremented total number of rows. This demonstrates that the updatable view has been successfully created.
Updating the created view
We can also update the created view using the following syntax :
Syntax:
UPDATE view_name SET column = "New Value";
Example:
UPDATE my_view SET dept = "Health";
Output:
UPDATE VIEW
Query returned successfully in 180 msec.
To verify the update, run the query:
SELECT * FROM example;
Final tableExplanation: The output will show that the row previously with dept
as 'Sales' has been updated to 'Health'. The order of the rows may have changed, with the updated row now appearing in the last position.
So this how we can create and perform operations on the updatable view.
Similar Reads
PostgreSQL - CREATE TABLE In PostgreSQL, the CREATE TABLE statement is used to define a new table within a database. It allows us to specify the table's structure, including column names, data types, and constraints, ensuring data integrity and consistency. Understanding the PostgreSQL table creation process is essential for
5 min read
PostgreSQL - CREATE TABLE AS The CREATE TABLE AS statement in PostgreSQL is a powerful tool used to create a new table and populate it with data returned by a query. This functionality allows you to generate tables on the fly based on query results, which can be very useful for reporting, analysis, and other tasks.Let us better
3 min read
PostgreSQL - Create Tables in Python Creating tables in PostgreSQL using Python is an essential skill for developers working with databases. This article will explore the process of creating new tables in the PostgreSQL database using Python.Why Create PostgreSQL Tables with Python?Using Python to create PostgreSQL tables is beneficial
4 min read
PostgreSQL - Create table using Python Creating tables in a PostgreSQL database using Python is a common task for developers working with databases. This process involves defining the structure of your data and ensuring that your database is optimized for efficient storage and retrieval. In this article, we will walk through the steps of
3 min read
PostgreSQL - ALTER TABLE In PostgreSQL, the ALTER TABLE statement is a powerful and essential tool that allows us to modify the structure of an existing table to meet evolving database needs. With PostgreSQL ALTER TABLE, we can perform various modifications on the table without disrupting the ongoing operations of our datab
6 min read
PostgreSQL - DROP VIEWS Statement 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 vi
4 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 - Creating Updatable Views Using WITH CHECK OPTION Clause PostgreSQL is the most advanced general purpose open source database in the world. pgAdmin is the most popular management tool or development platform for PostgreSQL. It is also an open source development platform. It can be used in any Operating Systems and can be run either as a desktop applicatio
4 min read
PostgreSQL - Materialized Views Materialized Views in PostgreSQL are a powerful tool that can significantly enhance query performance by physically storing the result of a complex query. Unlike standard views, which compute their result set every time they are queried, materialized views store the query result and can be refreshed
3 min read
How to Create Tables on Heroku Postgresql Heroku is a Platform as a Service (PaaS) used by developers to deploy their projects with different other requirements provided by the platform like database and all, whereas PostgreSQL is an open-source object-relational database system used for database management in many projects. This article is
4 min read