Beginning SQL
Beginning SQL Beginning SQL
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]
- Page 568: Chapter 9 264 Figure 9-1 The SQL ne
- Page 572: Chapter 9 266 INSERT INTO OrderItem
- Page 576: Chapter 9 268 VALUES (5,7, 2,’200
- Page 580: Chapter 9 Now that you’ve added t
- Page 584: Chapter 9 272 FirstName LastName Mo
- Page 588: Chapter 9 274 DVDPrice column is in
- Page 592: Chapter 9 SalesPersonId FirstName L
- Page 596: Chapter 9 278 SalesPersonId MemberI
- Page 600: Chapter 9 280 SalesPersonId MemberI
- Page 604: Chapter 9 282 MemberDetails.FirstNa
- Page 608: Chapter 9 284 The query would work
- Page 612: Chapter 9 Exercises 286 Assume that
- Page 616: Chapter 10 Now that you know what v
- Page 622: FROM dbo.MemberDetails INNER JOIN d
- Page 626: For example, you might want a list
- Page 630: However, if you take the case of Me
- Page 634: However, someone writes a query tha
- Page 638: Summary As you learned, views are l
- Page 644: Chapter 11 The information that fol
- Page 648: Chapter 11 This SQL adds formats (d
- Page 652: Chapter 11 TRANSACTION statement is
- Page 656: Chapter 11 Additionally, if a DBMS
- Page 660: Chapter 11 ROLLBACK TRANSACTION 310
- Page 664: Chapter 11 Transaction Logs 312 How
- Page 668: Chapter 11 Database There are perfe
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]