Creating Views

Creating Views

SQL views were discussed in Chapter 7. One view we created there was CustomerInterestsView. In SQL Server 2008 R2, views can be created in the Microsoft SQL Server Management Studio by using either an SQL statement (as we have done to create and populate the VRG tables) or by using the GUI Display (by right-clicking the Views folder to display a shortcut menu and then clicking the New View command.) CustomerInterestsView can be created with the SQL statement:

/* SQL View SQL-CREATE-VIEW-CH07-05 - CustomerInterestsView */ CREATE VIEW CustomerInterestsView AS

SELECT C.LastName AS CustomerLastName, C.FirstName AS CustomerFirstName, A.LastName AS ArtistName

FROM

CUSTOMER AS C JOIN CUSTOMER_ARTIST_INT AS CAI ON C.CustomerID = CAI.CustomerID JOIN ARTIST AS A

ON

CAI.ArtistID = A.ArtistID;

Chapter 10 Managing Databases with SQL Server 2008 R2

Figure 10-25

Continued (continued)

Note that the comment labeling this SQL CREATE VIEW statement refers to the view as SQL-CREATE-VIEW-CH07-05:

/* SQL View SQL-CREATE-VIEW-CH07-05 - CustomerInterestsView */

This numbering corresponds to the numbering we used in Chapter 7, and is used here for continuity and easy reference between this chapter and Chapter 7. Figure 10-26 on page 402 shows this SQL CREATE VIEW statement in an SQL script named DBP-e12-VRG- Create-Views.sql in the Microsoft SQL Server Management Studio. We will step through creating and testing this view.

Creating a New View

1. In the Microsoft SQL Server Management Studio Object Explorer, click the New Query

button to open a new tabbed SQL document window.

2. Click the Intellisense Enabled button to disable the Intellisense feature.

3. Type the SQL statements, but not the comments, shown in Figure 10-25.

4. Click the Parse button. If there are any SQL coding errors detected, fix them.

5. Click the Execute button.

6. Expand the Views folder in the VRG database object. Note that the dbo.CustomerInter- estsView object has been added to the VRG database, as shown in Figure 10-26.

7. To save this CREATE VIEW statement as part of an SQL script, add the comments shown in Figure 10-26 and then save the script as DBP-e12-VRG-Create- Views.sql.

8. Click the document window Close button to close the window. We can now look at the view in a GUI display.

Part 4 Multiuser Database Processing

Figure 10-25

Continued

Chapter 10 Managing Databases with SQL Server 2008 R2

Figure 10-25

Continued (continued)

Part 4 Multiuser Database Processing

Figure 10-25

Continued

Chapter 10 Managing Databases with SQL Server 2008 R2

Figure 10-25

Continued

Part 4 Multiuser Database Processing

The Execute button The Parse button

The Intellisense Enabled button

The Views folder object

The

dbo.CustomerInterestsView

view object

Figure 10-26

Creating an SQL View

Viewing an Existing View in the SQL Server GUI Display

1. Right-click the dbo.CustomerInterestsView object in the Object Browser to display

a shortcut menu.

2. Click the Design command on the shortcut menu. The dbo.CustomerInterestsView is displayed in a new tabbed document window in the GUI display format.

3. Rearrange the components of the view in the GUI display so that it appears as shown

in Figure 10-27.

4. Click the Save button to save the reconfigured view GUI display.

5. Click the document window Close button to close the GUI display window.

6. Collapse the Views folder in the Object Browser.

Figure 10-27

Viewing an SQL View in the GUI Display

The

dbo.CustomerInterestsView

view in the GUI display as a tabbed document page

The

dbo.CustomerInterestsView

view object

Chapter 10 Managing Databases with SQL Server 2008 R2

As explained in Chapter 7, SQL views are used like tables in other SQL statements. For example, to see all the data in the view, we use the SQL SELECT statement:

/* SQL View SQL-Query-View-CH07-05 - CustomerInterestsView */ SELECT

FROM

CustomerInterestsView

ORDER BY

CustomerLastName, CustomerFirstName;

Running a Single SQL Command in an SQL Script

1. Add the SQL code for SQL-Query-View-07-05 into your DBP-e12-VRG-Create-Views.sql

SQL script file.

2. Click the Save button to save the edited script file.

3. As shown in Figure 10-28, highlight just the SQL-Query-View-07-05 SQL query

statement.

4. Click the Execute button.

5. Note that only the SQL-Query-Views-10-01 SQL query statement is executed, not the entire script, and the results are displayed in the tabbed Results window as shown in Figure 10-28.

6. Close the DBP-e12-VRG-Create-Views.sql SQL script file. This is a handy technique to know—it allows you to run store multiple SQL commands in a

single SQL script, and then run only the command or commands that you want to run. The result is shown in Figure 10-29.

At this point, you should create and add all the SQL views and SQL view queries discussed in Chapter 7 to the DBP-e12-VRG-Create-Views.sql SQL script file. With these views created in the VRG database, they will be available for use in later sections of this chapter.

Figure 10-28

Running a Selected SQL Command in an SQL Script

The Save button The Execute button Highlight just the

SELECT command code as shown, and then click the Execute button

The results of the highlighted SQL command are displayed in a tabbed Results window

Part 4 Multiuser Database Processing

Figure 10-29

Result of Using the View CustomerInterestsView

SQL Server Application Logic

An SQL Server database can be processed from an application in a number of different ways. The first is one that we are already familiar with—SQL scripts. For security, however, such files should only be used during application development and testing and never on an operational database.

Another way is to create application code using a Microsoft .NET language such as C#.NET, C++.NET, VB.NET, or some other programming language and then invoke SQL Server DBMS commands from those programs. The modern way to do this is to use a library of object classes, create objects that accomplish database work, and then process those objects by setting object properties and invoking object methods. We will look at another alternative— embedding SQL statement in Web page code using the PHP scripting language in Chapter 11.

Finally, based on the SQL standard, application logic can be embedded in SQL/Persistent Stored Modules (SQL/PSM) modules—functions, triggers, and stored procedures. As you learned in Chapter 7, triggers can be used to set default values, to enforce data constraints, to update views, and to enforce referential integrity constraints. In this chapter, we will describe four triggers, one for each of the four trigger uses. These triggers will be invoked by SQL Server when the specified actions occur.

Stored procedures, as described in Chapter 7, can then be invoked from application programs or from Web pages using languages such as VBScript or PHP. Stored procedures can also be executed from the SQL Server Management Studio, but this should be done only when

Chapter 10 Managing Databases with SQL Server 2008 R2

the procedures are being developed and tested. As described in Chapter 9, for security reasons, no one other than authorized members of the DBA staff should be allowed to interactively process an operational database.

In this chapter, we will describe and illustrate two stored procedures. Here, we will test those procedures by invoking them from the SQL Server Management Studio, and some of our output will be designed specifically for this environment. Again, this should be done only during development and testing. You will learn how to invoke these stored procedures from application code in Chapter 11.