20.07.2013 Views

Beginning SQL

Beginning SQL

Beginning SQL

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 10<br />

296<br />

This statement gives you the following results shown in Figure 10-9.<br />

Figure 10-9<br />

Suppose you then edit a member’s birth date such that the date is before January 1, 1970:<br />

UPDATE MemberBirthdayFiltered<br />

SET DateOfBirth = 1 / 1 / 1969<br />

WHERE MemberId = 1<br />

The UPDATE query modifies the birthday to a date in 1969, but MemberBirthdayFiltered pulls only records<br />

for members with birthdays after January 1, 1970. Therefore, the record no longer matches the criteria and<br />

disappears from the records pulled in the view, and the record would disappear from the view.<br />

SELECT *<br />

FROM MemberBirthdayFiltered<br />

Figure 10-10<br />

While it would still be in the table, the result would be disconcerting to the user, to say the least.<br />

Another problem associated with views is that you could potentially insert or delete records that you are<br />

not supposed to edit. Assume that the membership is divided by state with different people assigned to<br />

maintenance of member records according to state. Build a view for birthdays in Golden State:<br />

CREATE VIEW MemberBirthdaysGoldenState<br />

AS<br />

SELECT MemberId, FirstName, LastName, DateOfBirth, Street, City, State,<br />

ZipCode, Email, DateOfJoining<br />

FROM MemberDetails<br />

WHERE (State = ‘Golden State’)<br />

Having created the view, you can now execute the following SELECT * statement:<br />

SELECT *<br />

FROM MemberBirthdaysGoldenState<br />

Executing the statement provides the results shown in Figure 10-11.<br />

Figure 10-11

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

Saved successfully!

Ooh no, something went wrong!