Quantcast
Channel: CodeFari
Viewing all articles
Browse latest Browse all 265

PostgreSQL: VIEW

$
0
0

In this article, we will learn about VIEW and how it can be managed in PostgreSQL. Firstly, we should know why we need a VIEW, while it just a group of SQL statements like function and procedure.

View in PostgreSQL


As per PostgreSQL documentation, A VIEW is a database object that is of a stored query. A view can be accessed as a virtual table in PostgreSQL. It means VIEWS is a pseudo-tables that represent the data/subset of selective columns of a table or more joined tablethroughSelect Statement;

The concept behind VIEWS is the security of sensitive data, If some tables have sensitive data so we cannot give them permission to access those tables publically, using VIEW we can give the permission specific rows or columns of a table to see.

For example, we have a customer table that has some sensitive information like password, Income, etc. I cannot give the permissions to access this table of our web developer, and then here the best option is VIEW.

Another thing is De-normalization, In relational databases, when we work on database design, the things that remain in your mind are data integrity and data consistency. And RDBMS provides lots of concepts to achieve the best database design, one of them is Normalization. There will a time comes when we work on the SQL statement and at that time we need to de-normalize data, then we have difficulty in writing the statement again and again, here we can avoid writing the SQL statement repeatedly by creating a VIEW.

Now, the conclusion of VIEW is described below.
  • A view helps simplify the complexity of a query because you can query a view, which is based on a complex query, using a simple SELECT statement.
  • Like a table, you can grant permission to users through a view that contains specific data that the users are authorized to see.
  • A view provides a consistent layer even the columns of underlying table changes. 

Create VIEW in PostgreSQL


Syntax:


CREATE VIEW view_name AS query;


view_name is the name of the view
query is the select SQL Statement like SELECT col1, col2.. From Table

Example of VIEW in PostgreSQL


Suppose we have table as below.

Employee table


CREATETABLEpublic.employee
(
    empidserialNOTNULL,-- primary key
    fnametext,
    lnametext,
    usernametext,
    passwordtext,
    contacttext,
    salarynumericNOTNULLDEFAULT 0.0,
    dojdateNOTNULL,
    manageridbigint,
    ageinteger,
    CONSTRAINTemployee_primarykeyPRIMARYKEY (empid),
    CONSTRAINT"uk_UserName"UNIQUE (username),
    CONSTRAINTchk_employee_ageCHECK (age> 18)
);


Employee Address table


CREATETABLEpublic.address
(
    idserialNOTNULL,
    empidintegerNOTNULL,-- reference of employee
    address1text,
    address2text,
    citytext,
    statetext,
    countrytext,
    CONSTRAINTpk_address_idPRIMARYKEY (id)
);


I am creating customer VIEW as vw_customer


CREATEVIEWvw_customerAS
SELECT
  e.fname||' '||e.lnameASname,
  e.contact,
  a.city,
  a.state,
  a.country
FROMemployeee
  INNERJOINaddressa
  ONe.empid=a.empid;

 
Now you can fetch the customers needed records using VIEW.


SELECT*FROMvw_customer;



Viewing all articles
Browse latest Browse all 265

Trending Articles