Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use database views and when not?

This question is about database views, not materialized-views.

Pros:

  • Query simplification.
  • Avoid repeat the same joins on multiples queries.
  • Avoid magic numbers.

Cons:

  • Hiding real queries (may be you are repeating joins).

What else?

like image 567
FerranB Avatar asked Mar 10 '09 13:03

FerranB


People also ask

Should I use DB views?

Views are virtual tables that can be a great way to optimize your database experience. Not only are views good for defining a table without using extra storage, but they also accelerate data analysis and can provide your data extra security.

Why do we use database views?

Views can provide advantages over tables: Views can represent a subset of the data contained in a table. Consequently, a view can limit the degree of exposure of the underlying tables to the outer world: a given user may have permission to query the view, while denied access to the rest of the base table.

When should I use a views in SQL?

Views are generally used to focus, simplify, and customize the perception each user has of the database. Views can be used as security mechanisms by letting users access data through the view, without granting the users permissions to directly access the underlying base tables of the view.

When should you use a view?

Views are acceptable when you want to restrict users to a particular subset of data. For instance, if you do not delete records but only mark the current one as active and the older versions as inactive, you want a view to use to select only the active records.


2 Answers

Pros: Allows you to change the underlying data structures without affecting the queries applications are using (as long as your view can hide the data structures)

like image 85
Andy White Avatar answered Oct 03 '22 21:10

Andy White


Security. Grant access on a view to the users who should be able to see the columns returned from it.

like image 29
John Saunders Avatar answered Oct 03 '22 19:10

John Saunders