Many database applications, including Microsoft SQL Server, allow you to create a query and be able to save it for later use, or even to use it as if it were its own pseudo-table. This is the idea behind a view. A view is a series of records retrieved from one or more existing tables, or as a combination of one or more views and one or more tables.
Before creating a view, you must first decide where its columns and records would come from. Obviously the easiest view is one whose columns and records come from one table. To create a view, you can use the Object Explorer, a Query Editor, the Command Prompt, or Windows PowerShell. Before starting the view, you would have to specify the table(s) that would be involved. To create a view from the Object Explorer, you can expand the database, right-click Views and click New View... This would open the Add Table dialog box:
The basic functionality is exactly the same as we reviewed for the Query Designer:
After selecting the source object, you can either double-click it or you can click it once and click Add. In in the previous lesson, we saw that you could add more than one existing table.
After selecting the objects, they would display in the window. The View Designer toolbar would be added:
If the View Designer toolbar is not visible:
The window from which you create a view, the View
Designer, displays four sections. These are the same sections for the Query
Designer. Each section is represented on the View Designer toolbar by
a button: the Show Diagram Pane button
If you don't want a particular section or you want to hide some sections, you can right-click anywhere in the table, position the mouse on Pane and click the name of the section. To hide any section, you can right-click anywhere in the window, position the mouse on Pane and click one of the selections:
When a section is displaying, its menu option is surrounded. To select a column:
The structure of a view can be considered complete when the SELECT statement is as complete as possible. At any time, to test the results of a view, you can run it. To see the result of a query:
Any of these actions would cause the bottom section of the view to display the results of the query.
As stated already, one of the reasons for creating a view is to be able to use it over and over again. To achieve this, the view must be saved. Like most objects in Microsoft SQL Server, a view must be saved and given a name. To save a view from the view window, you can click the
Save button on the toolbar
When saving a view, you must give it a name that follows the rules and suggestions of SQL. In our lessons, here are the rules we will use to name our views:
After saving a view, it becomes part of the Views node of its database: a node would be created for it and its name would appear in the Views node of its database.
To programmatically create a view, you use the following formula: CREATE VIEW [SchemaName.]ViewName AS SELECT Statement Microsoft SQL Server can generate skeleton code of a view for you. To use it, first create an empty Query Editor. Display the Template Explorer and expand the View node. From the View node, drag Create View and drop it in the Query Editor. The creation of a view starts with the CREATE VIEW expression followed by a name. After the name of the view, use the AS keyword to indicate that you are ready to define the view. Because a view is primarily a query, it is defined using a SELECT statement, using the same rules we studied for data analysis. Here is an example of a view: USE rosh;
GO
CREATE VIEW FemaleStudents
AS
SELECT FirstName, LastName, Gender, ParentsNames
FROM Registration.Students;
GO
After creating the SQL statement that defines the view, you must execute the statement.
When creating a view, if you want, you can encrypt its entry in the database engine. The formula to follow to do this is: CREATE VIEW [SchemaName.]ViewName WITH ENCRYPTION AS SELECT Statement Hee is an example: CREATE VIEW Personnel.EmployeesIdentifications
WITH ENCRYPTION
AS
SELECT EmplNbr,
FirstName,
LastName
FROM Personnel.Employees;
GO
As stated already, a view is a technique of selecting records to view or use over an over again. After a view has been created, you can open it. You have two main options:
Executing a view consists of seeing its results. You have various options. To view the results of a view:
In Transact-SQL, a view is considered an object. As such, it can be viewed, changed, or deleted. Like any regular object, a view has its own characteristics. To see them, you can right-click the view and click Properties. A View Properties dialog box would come up. It can give you information such as the name of the database the view belongs to, the date the view was created, etc.
After creating and executing a view, you can change its name with little worries with regards to its functionality. To visually rename a view, in the Object Explorer, right-click the name of the view and click Rename. the name of the view will be highlighted without its schema. You can then type the new name. To programmatically rename a view, open a Query Editor and use the following formula: sp_rename CurrentViewName, NewName; If the view uses a schema other than dbo, you must include it in the CurrentViewName. Here is an example: sp_rename N'Personnel.StaffMembers', N'EmployeesNames'; GO When renaming a view, you must take care of the permissions associated with it because those permissions will be lost.
After a view has been created, either by you or someone else, you may find out that it has an unnecessary column, it needs a missing column, it includes unnecessary records, or some records are missing. Fortunately, you can change the structure or the code of a view. This is referred to as altering a view. Most of the time, normal users are never asked to modify a view. There are many options to modify a view:
The basic formula to programmatically modify a view is: ALTER VIEW ViewName AS SELECT Statement You start the alteration with the ALTER VIEW expression followed by the name of the view. After the name of the view, use the AS keyword to specify that you are ready to show the change. After the AS keyword, you can then define the view as you see fit. For example, you can create a SELECT statement that includes a modification of the existing code or a completely new statement. Here is an example: ALTER VIEW FemaleStudents
AS
SELECT FirstName,
LastName,
DateOfBirth,
Gender,
ParentsNames
FROM Registration.Students;
GO
Instead of modifying a view, if you find it altogether useless, you can remove it from its database. You must first consider who will have that responsibility. Obviously, users are never or hardly asked to remove a view. A developer who has to delete a view must have the DELETE permission. To delete a view:
The formula to programmatically delete a view is: DROP VIEW ViewName On the right side of the DROP VIEW expression, enter the name of the undesired view. Here is an example: DROP VIEW FemaleStudents; GO Then execute the statement. You will not be warned before the interpreter deletes the view. |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||