Views Flashcards
How do you reference an existing view from another query?
Use the name of the view. View name is sales.USAcusts
~~~
SELECT custid, companyname FROM sales.USAcusts
~~~
SQL Server 70-461 04-02b to 04-03
Is the result set from the view or the view definition stored in the database?
The definition of the view.
SQL Server 70-461 04-02b to 04-03
What is the general structure used to create a view?
<statement to drop view if it exists>; GO CREATE VIEW view_name AS <select statement that defines the view>; GO
Just making a sentence long enough so the code will left justify for this flash card.
SQL Server 70-461 04-02b to 04-03
Example of a view statement for reference
If OBJECT_ID('Sales.USACusts') IS NOT NULL DROP VIEW Sales.USACusts; GO CREATE VIEW Sales.USACusts AS SELECT custid, companyname, contactname, contacttitle, address, city, region, postal code, country, phone, fax FROM Sales.Customers WHERE country=N'USA'; GO
SQL Server 70-461 04-02b to 04-03
General form of statement to create a view
USE TSQL2012
GO
CREATE VIEW Sales.OrderTotalsByYear
WITH SCHEMABINDING
AS
SELECT YEAR(O.orderdate) AS orderyear,
SUM(OD.qty) AS qty
FROM Sales.Orders AS O
JOIN Sales.OrderDetails AS OD
ON OD.orderid=O.orderid
GROUP BY YEAR(orderdate);
GO
SQL Server 70-461 09-01
Can you select a view just like you select a table in a SELECT statement?
Yes
Example:
SELECT orderyear.qty
FROM Sales.OrderTotalsByYear
Just writing enough words to get code to left justify
SQL Server 70-461 09-01
How should a view be named with respect to a schema? What is best practice?
You should always include the schema name the view will belong to when naming it.
Example:
Sales.OrderTotalsByYear
Sales is the schema
OrderTotalsByYear is the view
SQL Server 70-461 09-01
What does including the SCHEMABINDING statement when creating a view do?
SCHEMABINDING guarantees that the underlying table structures that the view includes cannot be altered without dropping the view
SQL Server 70-461 09-01
General form of create view statement that includes SCHEMABINDING
CREATE VIEW Sales.OrderTotalsByYear WITH SCHEMABINDING
SQL Server 70-461 09-01
Since the body of a view is just a SELECT statement, what rules for SELECT statements does it follow?
All of the usual rules when putting together a SELECT statement
SQL Server 70-461 09-01
Where must the CREATE VIEW statement be in a batch?
- It must be the first statement
- It cannot be inside an IF statement
SQL Server 70-461 09-01
Are there any considrations that must be made when naming a view? With respect to other objects in the database.
Yes
Because it is an object in the database it must have a name that is unique from all other objects
SQL Server 70-461 09-01
You can name the columns right after the new view name or you can name them in the SELECT statement by aliasing the column. Which is better?
- CREATE VIEW Sales.OrderTotalsByYear(orderyear, qty)
- SELECT YEAR(O.orderdate) AS orderyear, SUM(OD.qty) AS qty
As you can see, it would be better to put them in the SELECT statement so they are right next to the column they are naming instead of having to look back and forth between CREATE statement and SELECT statement
SQL Server 70-461 09-01
What are the three view options and which of them can be used together?
WITH ENCRYPTION
WITH SCHEMABINDING
WITH VIEW_METADATA
You can use any combination of the three. Meaning you could also use just one or two.
SQL Server 70-461 09-01
What does WITH ENCRYPTION do?
It makes it difficult for users to discover the SELECT text of the view. Note it is not strong encryption.
SQL Server 70-461 09-01