SQL 2008 Essentials

Page 205

190

CHAPTER 6

Query Development and Derived Structures

When you use a view for queries, you use it just as you would use the underlying table(s). Views can be used to create other views or queries. Views are typically a way of building queries on top of other queries. 6.3.1.1

Creating Views

A view can be regarded as a result set that itself can be queried. The SELECT statement used to create a view can employ one or more underlying tables and/or other views in the current or other databases. The general SQL syntax used to create a view is: CREATE VIEW view name AS SELECT . . . The following example creates a view called namemaj, which is a view of students’ names and majors from the Student table. To create the view, namemaj, enter the following in the SQL query editor pane: CREATE VIEW namemaj AS SELECT sname, major FROM Student and then execute this query in the regular way. To view namemaj, click on Views in the Object Explorer, as shown in Figure 6.3:

Figure 6.3

Viewing the namemaj view

A view is a stored SELECT statement. Each time a view is accessed, the SELECT statement in the view is run. 6.3.1.2

Using Views

The new view can be used just like a table in the FROM clause of any SELECT statement, as shown here: SELECT * FROM namemaj


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.