Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is database view used?

Is using "view" in db design right method or we should handle it code side? What are the advantages or disadvantages?

like image 236
Sessiz Saat Avatar asked Mar 16 '10 12:03

Sessiz Saat


People also ask

What is the benefit of using view?

Design Flexibility: By using a view instead of a query in an application, it is easier to make changes to the underlying table structure. Improved Security: By using a view to return data from tables instead of a SELECT, you can hide the WHERE clause or other columns to which you do not want the user to have access.

Why view is important in DBMS?

Restricting data access – Views provide an additional level of table security by restricting access to a predetermined set of rows and columns of a table. Hiding data complexity – A view can hide the complexity that exists in a multiple table join.

What is view and why it is used in SQL?

A VIEW in SQL Server is like a virtual table that contains data from one or multiple tables. It does not hold any data and does not exist physically in the database. Similar to a SQL table, the view name should be unique in a database. It contains a set of predefined SQL queries to fetch data from the database.


1 Answers

I see a couple of reasons to use views :

  • Provide a simpler interface : just query the view, and not a dozen tables, doing joins and all
  • Provide an interface that doesnt change (or less often) :
    • Even if you change the structure of the tables, you might be able to modify your view so it still returns the same thing
    • Which means no change is needed in your application's code : it'll still work, as it's using the view, and not directly accessing the tables
  • Only provide an interface to some fields of the tables
    • No need for the users to see some data they won't use
    • Or to access some data they should not use
  • With some database engines (I think MS SQL Server supports that), some type of views can have indexes
    • Which is a good thing for performances : if you have some complex query, store it as a view, and define the required indexes on that view
like image 136
Pascal MARTIN Avatar answered Oct 13 '22 08:10

Pascal MARTIN