Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of views in SQL? [duplicate]

Tags:

sql

views

Possible Duplicate:
What are views good for?

I'm trying to figure out what the purpose of views in an SQL database is?

Why, when and how would I create and use views?

like image 474
Johan Alkstål Avatar asked Mar 04 '10 17:03

Johan Alkstål


2 Answers

  • Many different perspectives of the same table.

  • Can hide certain columns in a table. For example you may want to allow employees to see other employees' phone number column, but only certain employees to be able to access an employee's salary column!

  • Can provide huge time savings in writing queries by already having a group of frequently accessed tables joined together in a view.

  • Views allow you to use functions and manipulate data in ways that meet your requirements. For example, you store a person's birth date, but you like to calculate this to determine their age.

like image 135
Yada Avatar answered Oct 08 '22 21:10

Yada


It can be used to simplify complex joins that you use repeatedly across your DB.

CREATE view [dbo].[V_MyComplexJoin] 
As    
    Select TableA.A, TableB.B
    From TableA
        Inner Join TableB On TableA.BID = TableB.ID  
like image 26
ChaosPandion Avatar answered Oct 08 '22 19:10

ChaosPandion