What is View and How to Create View in SQL Server

Updated on     Kisan Patel

This tutorial will explain you what is view and how to create view in SQL Server.

A view in sql server is a virtual object whose result is derived from SQL query. View doesn’t contains any physical data, but all data comes from the database table. View is only stored physically in the disk when it is indexed.

Views are used to

  1. Filter the table data
  2. Hide some columns data from a database table and provide only limited set of columns
  3. Create a reusable set of data

To create a View in SQL Server, explore the Database and right click the Views folder and click New View as shown in below screenshot:

create-view-sql-server

That shows an Add Table dialog box like below:

create-view-sql-server-2

Now click on the table we want to create View for, in this example we are going to select Categories and Products table by holding the ctrl key and clicking on the table name. Now click on Add button. then, click Close button.

As the CategoryID column of Categories and Products table are same (and relationship is created) so we will see a relationship between these two columns.

Now, check the columns checkbox from the list of tables selected from the 1st panel that will add those columns into the 2nd panel and corresponding SQL statements are written into the 3rd panel. When we want to see the result of the query automatically built after selecting the relationship and column names, we can click on! icon (Execute SQL) from the toolbar at the top-left and see the result in the 4th panel.

create-view-sql-server-3

Once everything is done, then save the view by clicking on the Save icon from the toolbar in the top-left or from File menu, then write the View name to save.

Now, refresh the Views folder again and we will see the View just created. To see the result of this Views, right click the View name and choose “Select Top xxx Rows” that will write the SQL select element for this View as if it is a database table and show the data in the below panel.

create-view-sql-server-4

In this view result, notice that without writing the Joins statement between Products and Categories table, we are able to get columns of Products and Categories table because these join statements are already written when we had created the View.

 


SQL Server

Leave a Reply