Open In App

PostgreSQL – Create updatable Views

Last Updated : 23 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 includeGROUP 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:

PostgreSQL Create Updatable Views Example

Original table

Syntax 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:

PostgreSQL Create Updatable Views Example

my_view

Explanation: 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;
Inserting in the created view

Updated table

Explanation: 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 table

Explanation: 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.



Next Article
Article Tags :

Similar Reads