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 9<br />

Now that you’ve added the necessary data to the Film Club database, you can start writing and executing<br />

complex, difficult queries.<br />

Tackling Difficult Queries<br />

In this section, you learn some tips and techniques for writing difficult queries. Unfortunately, there’s no<br />

quick fix, but hopefully by the time you’ve finished this section you’ll be ready and raring to go solve<br />

some mind-bending queries.<br />

The following list details the basic principles you should follow when creating a complex query:<br />

❑ Decide what data you want. You might find that it helps to actually write a small portion of data<br />

on paper. That way, you know what results you’re expecting and roughly how to get them.<br />

❑ Create the SELECT clause and populate its column list. When you know what columns you’re<br />

after, writing queries becomes much easier.<br />

❑ Work out which tables to get your data from and create the FROM clause of the SELECT statement.<br />

❑ Work through the FROM clause a bit at a time. Don’t try to create all the table selections and joins<br />

all at once. Start simple and test the code at each stage to see if it works and gives the results<br />

that you expect. Then work out the FROM clause using a database diagram, and determine where<br />

you need to get your next column’s data from and where the next join links to.<br />

❑ Query elements that don’t affect the final results (such as the ORDER BY clause) can wait until<br />

the end.<br />

Considering the preceding list, the first tip might seem strange, but it’s this: Ask yourself whether you<br />

really need to write a complex query? While it’s quite satisfying to spend a day writing a query so complex<br />

that it confounds even the most gifted database guru, doing so might not always suit your needs. If you’re<br />

in a project under tight deadlines, it might be a whole lot easier to write a few simple queries and do the<br />

data manipulation from a high-level language such as Visual Basic, C++, Java, or whatever language you<br />

use for application development. Writing multiple simple queries is sometimes quicker and the code is easier<br />

to understand.<br />

Work Out What You Want, What You Really, Really Want<br />

270<br />

Leaping right into the middle of writing a query can be tempting, but in fact the first step is working out<br />

what the query’s requirements are. You need to sit down and analyze what’s been asked for and what<br />

the end results should be. A simple example might be if you’re asked to delete a member from the<br />

MemberDetails table. On the face of it, this is just a simple DELETE FROM MemberDetails and a WHERE<br />

clause ensuring that only the member you want deleted is deleted. However, it has a ripple effect, as references<br />

to the member are also contained in the FavCategory table, in the Attendance table, and the Orders<br />

table. If the database is set up correctly, as the Film Club one is, then trying to delete a member but not<br />

deleting references to them in other tables will throw up an error. That’s because constraints were set up<br />

to stop this from happening. However, if restraints don’t exist, then it’s all too easy to end up with<br />

orphaned data — for example, references to a member’s favorite category for a member who doesn’t exist!

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

Saved successfully!

Ooh no, something went wrong!