DEV Community

Muhammad Wasi Naseer
Muhammad Wasi Naseer

Posted on

What are Views in Database

Database View is a virtual/logical table that is made up of a SQL query on the physical tables. A simple view itself does not exist physically at all but a query under a name.

Reading data from View

When we read data from view, it's basically a query which gets executed under a name

Updating data from View

Insert query

We can insert data into simple views if Primary key and Not null fields are included in the View. If they are not included, constraint error will be thrown

Update query

We can update data in simple views

Delete query

We can also delete data in simple View. Behind the scenes, it executes delete query on the physical table

Types of Views

  1. Simple Views
    They are made up of single table.

  2. Complex Views
    They are made up of more than one table.

Why we use it?

  • To simplify complex queries
  • To hide the complexity of underlying tables to the end user or external apps
  • To enable backward compatibility. If we make changes in the schema, we can provide views to the existing apps so that they won't break.
  • To provide extra security layer using read-only views
  • To provide computed columns as database should not save computed columns

What are Materialized Views?

In order to increase performance in reading data, the data is actually stored on disk. The next time user queries the data from the view, the result wouldn't be calculated on the fly from multiple tables but it's a query on the view.

It is used when the view is made up of multiple tables using JOIN. As we know that joining multiple table decreases performance.

Top comments (0)