Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Views in SQL

Tags:

sql

views

I have a view that is created from a base table.This view is basically the exact copy of table without any filter conditions and it has all the columns and records of the table.

Is there any advantage in using view (which is a direct copy of table) instead of the table directly in my application or stored procedures.

like image 829
Avg Avatar asked Mar 05 '10 11:03

Avg


People also ask

What is use of 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.

Is it good to use views in SQL?

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 views instead of tables?

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.

Can you select from a view in SQL?

Views are a special version of tables in SQL. They provide a virtual table environment for various complex operations. You can select data from multiple tables, or you can select specific data based on certain criteria in views.


2 Answers

But you need to be aware that the way views work is a bit different, as you end up having some overkill on the DB side.

The way a view works, is by doing a Select *, and then filtering itself for the columns you are adding to it.

I'd be really weary to use views for this, unless there were some serious security concerns.

The way to go is to create a Stored Procedure that grabs the data directly from the table. That way, you can take the maximum of index and all that.

Cheers

like image 157
Marcos Placona Avatar answered Sep 29 '22 10:09

Marcos Placona


One advantage (or disadvantage, depending on your viewpoint) is that Views allow you to store business logic within the SQL server, instead of within your code. If you need to change the business easily without recompiling your code, modifying the view is a quick and easy way of doing so.

I personally prefer having the business logic of the application defined within the code however :)

like image 43
Ben Rowe Avatar answered Sep 29 '22 09:09

Ben Rowe