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 - CREATE TRIGGER
Triggers in PostgreSQL are powerful tools that allow you to automate actions in your database whenever certain events occur, like inserting, updating, or deleting data. From this article, we will better understand the CREATE TRIGGER Statement in PostgreSQL.Steps to Create Triggers in PostgreSQLTo cr
3 min read
PostgreSQL - CREATE SEQUENCE
In database management, generating unique identifiers is vital for data integrity, and PostgreSQL provides a powerful feature called CREATE SEQUENCE to solve this. This command allows developers to create a sequence that automatically generates unique numeric values. In this article, we will explore
4 min read
PostgreSQL - CREATE PROCEDURE
PostgreSQL CREATE PROCEDURE allows developers to define stored procedures that apply complex business logic, transaction handling and multiple SQL statements in a reusable manner. Unlike functions, PostgreSQL stored procedures can manage transactions with commands like COMMIT and ROLLBACK. Understan
4 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
PostgreSQL - Describe Table
Unlike MySQL, PostgreSQL does not have a 'DESCRIBE' statement to view table column details. However, PostgreSQL provides several methods to access information about table columns. In this article, we'll learn two effective ways to Describe Tables in PostgreSQL.1. Using the pSQL shellThe 'psql' comma
2 min read