Beginning SQL

Beginning SQL Beginning SQL

marjan.fesb.hr
from marjan.fesb.hr More from this publisher
20.07.2013 Views

Chapter 10 Types of Views Given that most SQL statements can be coerced into a view, you can build up many different types of views to fit your needs. But first, for the purposes of simplifying your life, build a base view that pulls all of the fields you need from a set of tables. Later in the chapter, you build other views based on that original view. Suppose that you need to analyze meeting attendance. The first, or base, view pulls data from MemberDetails, Attendance, and Location. The user is generally not interested in the table’s primary and foreign keys, so you just pull all the data fields from each table. You are not going to perform any filtering or ordering on the base data; you will just pull the required data. This may not be efficient for large tables or results sets. This strategy is not recommended in general, but it makes your results sets more interesting for your limited data. Table Join Views Table join views are views that join related tables back together in order to pull specific fields out of the related tables and display a view across more than one table. Getting all of the tables joined, with lookup tables, outer joins where needed, and all the other little things required to gather data from a half dozen or more tables, can take a while to get correct. Once correct, there may be little need to do it over and over again. Queries that join multiple tables are called base queries because they form the base of other queries that simply need to use all this data. A single base query may serve to gather data for dozens of other queries. Base View 290 The SQL for a base view might look like the following (in SQL Server): SELECT dbo.MemberDetails.LastName, dbo.MemberDetails.FirstName, dbo.MemberDetails.DateOfBirth, dbo.MemberDetails.Street, dbo.MemberDetails.City, dbo.MemberDetails.State, dbo.MemberDetails.ZipCode, dbo.MemberDetails.Email, dbo.MemberDetails.DateOfJoining, dbo.Attendance.MeetingDate, dbo.Attendance.MemberAttended, dbo.Location.Street AS [Meeting Street], dbo.Location.City AS [Meeting City], dbo.Location.State AS [Meeting State] FROM dbo.MemberDetails INNER JOIN dbo.Attendance ON dbo.MemberDetails.MemberId = dbo.Attendance.MemberId INNER JOIN dbo.Location ON dbo.Attendance.LocationId = dbo.Location.LocationId Notice that SQL Server places a ton of qualifier data in the SQL statement, the dbo.MemberDetails part. In some instances, the table qualifiers are a necessity, specifically when you pull identical field names from different tables, such as City from MemberDetails and from Location. In any event, SQL statements like this can be a little tough to wade through. Once you have the SQL statement working, however, you can now add the CREATE VIEW statement and use this SQL to build a named view. CREATE VIEW dbo.MemberAttendance AS SELECT dbo.MemberDetails.LastName, dbo.MemberDetails.FirstName, dbo.MemberDetails.DateOfBirth, dbo.MemberDetails.Street, dbo.MemberDetails.City, dbo.MemberDetails.State, dbo.MemberDetails.ZipCode, dbo.MemberDetails.Email, dbo.MemberDetails.DateOfJoining, dbo.Attendance.MeetingDate, dbo.Attendance.MemberAttended, dbo.Location.Street AS [Meeting Street], dbo.Location.City AS [Meeting City], dbo.Location.State AS [Meeting State]

Chapter 10<br />

Types of Views<br />

Given that most <strong>SQL</strong> statements can be coerced into a view, you can build up many different types of views<br />

to fit your needs. But first, for the purposes of simplifying your life, build a base view that pulls all of the<br />

fields you need from a set of tables. Later in the chapter, you build other views based on that original view.<br />

Suppose that you need to analyze meeting attendance. The first, or base, view pulls data from<br />

MemberDetails, Attendance, and Location. The user is generally not interested in the table’s primary<br />

and foreign keys, so you just pull all the data fields from each table. You are not going to perform any filtering<br />

or ordering on the base data; you will just pull the required data. This may not be efficient for<br />

large tables or results sets. This strategy is not recommended in general, but it makes your results sets<br />

more interesting for your limited data.<br />

Table Join Views<br />

Table join views are views that join related tables back together in order to pull specific fields out of the<br />

related tables and display a view across more than one table. Getting all of the tables joined, with lookup<br />

tables, outer joins where needed, and all the other little things required to gather data from a half dozen<br />

or more tables, can take a while to get correct. Once correct, there may be little need to do it over and<br />

over again. Queries that join multiple tables are called base queries because they form the base of other<br />

queries that simply need to use all this data. A single base query may serve to gather data for dozens of<br />

other queries.<br />

Base View<br />

290<br />

The <strong>SQL</strong> for a base view might look like the following (in <strong>SQL</strong> Server):<br />

SELECT dbo.MemberDetails.LastName, dbo.MemberDetails.FirstName,<br />

dbo.MemberDetails.DateOfBirth, dbo.MemberDetails.Street, dbo.MemberDetails.City,<br />

dbo.MemberDetails.State, dbo.MemberDetails.ZipCode, dbo.MemberDetails.Email,<br />

dbo.MemberDetails.DateOfJoining, dbo.Attendance.MeetingDate,<br />

dbo.Attendance.MemberAttended, dbo.Location.Street AS [Meeting Street],<br />

dbo.Location.City AS [Meeting City], dbo.Location.State AS [Meeting State]<br />

FROM dbo.MemberDetails<br />

INNER JOIN dbo.Attendance<br />

ON dbo.MemberDetails.MemberId = dbo.Attendance.MemberId<br />

INNER JOIN dbo.Location<br />

ON dbo.Attendance.LocationId = dbo.Location.LocationId<br />

Notice that <strong>SQL</strong> Server places a ton of qualifier data in the <strong>SQL</strong> statement, the dbo.MemberDetails<br />

part. In some instances, the table qualifiers are a necessity, specifically when you pull identical field<br />

names from different tables, such as City from MemberDetails and from Location. In any event, <strong>SQL</strong><br />

statements like this can be a little tough to wade through.<br />

Once you have the <strong>SQL</strong> statement working, however, you can now add the CREATE VIEW statement and<br />

use this <strong>SQL</strong> to build a named view.<br />

CREATE VIEW dbo.MemberAttendance AS<br />

SELECT dbo.MemberDetails.LastName, dbo.MemberDetails.FirstName,<br />

dbo.MemberDetails.DateOfBirth, dbo.MemberDetails.Street, dbo.MemberDetails.City,<br />

dbo.MemberDetails.State, dbo.MemberDetails.ZipCode, dbo.MemberDetails.Email,<br />

dbo.MemberDetails.DateOfJoining, dbo.Attendance.MeetingDate,<br />

dbo.Attendance.MemberAttended, dbo.Location.Street AS [Meeting Street],<br />

dbo.Location.City AS [Meeting City], dbo.Location.State AS [Meeting State]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!