TSQL TipsnHints - SQL Server View Designer
TSQL TipsnHints - SQL Server View Designer
However I strongly recommend utilisng Views across related Tables and comes with more
features than the Query Designer. Unlike the Query Designer the View Designer can
executes the TSQL within its environment.
The View Designer is just like the Query Designer, in that it provides a visual way of
designing a query. It saves you the trouble of designing the query in Query Designer, then
doing the extra coding to convert the query into a view as in the previous Quick Guide –
Create a View.
Mansha Nawaz SQL Tips Hints : Tips and Hints: Using the View Designer Page 1 of 15
SCHOOL OF COMPUTING, ENGINEERING & DIGITAL TECHNOLOGIES
TIPS AND HINTS: USING THE VIEW DESIGNER
Mansha Nawaz SQL Tips Hints : Tips and Hints: Using the View Designer Page 2 of 15
SCHOOL OF COMPUTING, ENGINEERING & DIGITAL TECHNOLOGIES
TIPS AND HINTS: USING THE VIEW DESIGNER
You will now see the selected tables, and their relationships — just like in the Query
Designer.
One difference is that the View Designer is sitting inside a query window. When we
used the Query Designer, it was opened in a pop-up dialog, which prevented us from
accessing any of the toolbar options, etc.
But having the View Designer opened inside a query window allows us to access the
toolbar and other options as required.
Another difference is that we now have a fourth pane — the Results Pane. This allows
us to execute the SQL to see what effect it has on the results before we save the view.
Our Example
We'll keep it simple this time and select four columns from the three tables. We won't
provide any sort or filter criteria but you should look to also utilise these options.
Here's a close-up of the Criteria Pane:
Mansha Nawaz SQL Tips Hints : Tips and Hints: Using the View Designer Page 3 of 15
SCHOOL OF COMPUTING, ENGINEERING & DIGITAL TECHNOLOGIES
TIPS AND HINTS: USING THE VIEW DESIGNER
You can test the view before you save it by executing the SQL while in the View
Designer.
To do this, right-click anywhere in the design area and select Execute SQL (or press
Ctrl+R on your keyboard).
Mansha Nawaz SQL Tips Hints : Tips and Hints: Using the View Designer Page 4 of 15
SCHOOL OF COMPUTING, ENGINEERING & DIGITAL TECHNOLOGIES
TIPS AND HINTS: USING THE VIEW DESIGNER
5. The Results
Mansha Nawaz SQL Tips Hints : Tips and Hints: Using the View Designer Page 5 of 15
SCHOOL OF COMPUTING, ENGINEERING & DIGITAL TECHNOLOGIES
TIPS AND HINTS: USING THE VIEW DESIGNER
To save the view, click the Save icon on the toolbar, then name the view at the prompt.
Mansha Nawaz SQL Tips Hints : Tips and Hints: Using the View Designer Page 6 of 15
SCHOOL OF COMPUTING, ENGINEERING & DIGITAL TECHNOLOGIES
TIPS AND HINTS: USING THE VIEW DESIGNER
Mansha Nawaz SQL Tips Hints : Tips and Hints: Using the View Designer Page 7 of 15
SCHOOL OF COMPUTING, ENGINEERING & DIGITAL TECHNOLOGIES
TIPS AND HINTS: USING THE VIEW DESIGNER
Since views are an additional layer sure they do add an overhead but there is a tradeoff for
situations when they are of great help.
As we proceed, we will try to learn more about them not by theoretical explanation but by some
practical examples.
Uses
We begin with creating 3 tables PRODUCTS, Customer & BOOKING. These are fictitious tables for
our demo. The PRODUCTS stores data for a retail shop with a flag column IsSalable based on
whose value we treat the products as Salable.
Next, we have a Customer table which stores UserID and Password details for customers.
Lastly, I have created a BOOKING table which houses all the bookings from different customers.
Mansha Nawaz SQL Tips Hints : Tips and Hints: Using the View Designer Page 8 of 15
SCHOOL OF COMPUTING, ENGINEERING & DIGITAL TECHNOLOGIES
TIPS AND HINTS: USING THE VIEW DESIGNER
Our tables contents look like this. I know the tables are not completely normalized, for now
please ignore them, these are simple demo tables.
(3 row(s) affected)
(3 row(s) affected)
(3 row(s) affected)
A customer purchases/books a product and the same gets recorded into the BOOKING table
now to generate the Invoice on his name we can uses a VIEW which would help us do away with
Mansha Nawaz SQL Tips Hints : Tips and Hints: Using the View Designer Page 9 of 15
SCHOOL OF COMPUTING, ENGINEERING & DIGITAL TECHNOLOGIES
TIPS AND HINTS: USING THE VIEW DESIGNER
a physical table. Instead it would enable us to generate the Invoice based on the information
from these 3 tables itself. Let’s see how it’s possible.
(3 row(s) affected)
We have been able to generate the Invoice based on the 3 tables hence we have not only
optimized the Invoice generation also we have saved ourselves from hosting a physical table in
the database with this information.
This is the most credible use of a VIEW; it can not only reduce apparent complexity but also
prevent redundant hosting of data in the DB.
Next say there is some API which enables the Customer care executives to view
the customerinformation details. Now exposing the Password might be risky, it’s strictly
confidential info.
We have a created a View which can be used by the API to fetch customer details –(Minus)
the Password Column.
Views can be used to prevent sensitive information from being selected, while still allowing
other important data.
Mansha Nawaz SQL Tips Hints : Tips and Hints: Using the View Designer Page 10 of 15
SCHOOL OF COMPUTING, ENGINEERING & DIGITAL TECHNOLOGIES
TIPS AND HINTS: USING THE VIEW DESIGNER
Views do not have a physical existence, but still they do return a set of record set as a table
does, the differences is it is simply an additional layer which calls the underlying code which
finally returns the record set.
When I execute the code...
...I get recordsets as I would get in a table with the only difference that the data returned is as
per the below query:
Select CustID
,FNAME AS [FIRST NAME]
,LNME AS [LAST NAME]
,UserID
FROM dbo.Customer
But arguably, we still get a set of records, isn’t it? So say if there are 1 million customers in my
database, wouldn’t it be cool if I have clustered/non clustered index on my view for
optimized queries. But is it possible as the view doesn’t host data physically, the answer is
yes. It is possible to have indexes on views. But before we find ourselves capable of creating
index on views, we have to SCHEMABIND our VIEWs.
Also to be able to create an index on the view you need it essentially schema bound.
Let’s make the change:
Mansha Nawaz SQL Tips Hints : Tips and Hints: Using the View Designer Page 11 of 15
SCHOOL OF COMPUTING, ENGINEERING & DIGITAL TECHNOLOGIES
TIPS AND HINTS: USING THE VIEW DESIGNER
Now we have an index on the view, remember you need to have a UNIQUE CLUSTERED
INDEX to be able to create a NONCLUSTERED INDEX. By which I mean I can create the below
index mandatorily post the creation of the Invoice_View_Indx.
So next use of the View is to be able to create an additional index upon the db to speed up
your query performance.
Features
Are views only meant for reading data in a customized mode? Not really views also facilitate
DML (Insert/Update/Delete). But there is a set of rules which needs to be adhered to
enable DMLs.
o If you are using a view to insert data, then your view should have a single select and
also all the mandatory columns of the “being edited” table must be included in the
view unless the table has a default values for all NOT NULL columns of the table.
o Secondly don’t forget, for views with “WITH CHECK” options enabled, it’s important
to keep in mind that the data begin inserted qualifies in the WHERE clause of the view
and is certain to be selected by the view. Simply put the data you insert is picked up
while you select from your view.
o If the view is having joins with more than one table, then most cases chances of
modifying capabilities are negligible unless INSTEAD OF Triggers are in place to
handle the request.
Keeping these in mind, let’s turn to an example and perform INSERTs / Updates / Deletes.
o Insert
Mansha Nawaz SQL Tips Hints : Tips and Hints: Using the View Designer Page 12 of 15
SCHOOL OF COMPUTING, ENGINEERING & DIGITAL TECHNOLOGIES
TIPS AND HINTS: USING THE VIEW DESIGNER
The insert happened because though the columns CustID and Pswd are mandatory
but CustID is IDENTITY and PSWD has a DEFAULT. All the other mandatory data was
supplied in the insert query.
(4 row(s) affected)
o Update
UPDATE CustomerInfo_V
SET [FIRST NAME]='Tyroneoorthy'
WHERE [FIRST NAME]='Tyrone'
SELECT * FROM Customer
(4 row(s) affected)
o Delete
Mansha Nawaz SQL Tips Hints : Tips and Hints: Using the View Designer Page 13 of 15
SCHOOL OF COMPUTING, ENGINEERING & DIGITAL TECHNOLOGIES
TIPS AND HINTS: USING THE VIEW DESIGNER
Alternatively in SSMS.
Tools > Options > Environment >Keyboard > Ctrl-F1 == SP_HELPTEXT
From next time, to see the contents of a VIEW/StoreProcedure, simply select it and hit
Ctrl+F1?
Refreshing Views
Just in case we are working with a non-schema bound view and there is some change in the
underlying table, to prevent the view from producing unexpected results, we have an option to
refresh the view with:
It is not advised to encrypt your views unless you have a very strong reason behind it. That was
some key elements involved. I hope the next time you work with views, this tutorial will help you
make better decisions.
Mansha Nawaz SQL Tips Hints : Tips and Hints: Using the View Designer Page 14 of 15
SCHOOL OF COMPUTING, ENGINEERING & DIGITAL TECHNOLOGIES
TIPS AND HINTS: USING THE VIEW DESIGNER
Mansha Nawaz SQL Tips Hints : Tips and Hints: Using the View Designer Page 15 of 15