Beginning SQL

Beginning SQL Beginning SQL

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

Figure 3-1 The first task when tackling tricky SQL problems is to work out what information is required and which tables contain that information. The preceding scenario specifies that the results must contain the following information: ❑ Members’ names ❑ All the films that are in the members’ favorite categories ❑ Film names ❑ Films’ year of release ❑ Category to which each film belongs You can obtain the members’ first and last names from the MemberDetails table. Details of the film names come from the Films table. The category each film belongs to is slightly trickier. Although the Films table contains a CategoryId for each record of a film, this is just a number — the results need to display the category’s name. Looking at the table diagram, you can see that the Category table contains the category name, so the Category table provides the category name. But you need to link the Category table to the Films table. The CategoryId field is in both tables, so the CategoryId field provides the link between the two. Remember that when you set up the database, the CategoryId field was a primary key field in the Category table and a foreign key field in the Films table; this field provides the link between the two data sets they hold. Finally, the results ask for each film in a member’s favorite category. The FavCategory table contains this information, but again it just contains MemberId and CategoryId, so in order to get the information in a human-friendly format, you need to link these two tables to the MemberDetails and Category tables. Below is a list summing up the tables you need to use to get the results you want: ❑ MemberDetails ❑ Films Extracting Information 93

Figure 3-1<br />

The first task when tackling tricky <strong>SQL</strong> problems is to work out what information is required and which<br />

tables contain that information. The preceding scenario specifies that the results must contain the following<br />

information:<br />

❑ Members’ names<br />

❑ All the films that are in the members’ favorite categories<br />

❑ Film names<br />

❑ Films’ year of release<br />

❑ Category to which each film belongs<br />

You can obtain the members’ first and last names from the MemberDetails table. Details of the film names<br />

come from the Films table. The category each film belongs to is slightly trickier. Although the Films table<br />

contains a CategoryId for each record of a film, this is just a number — the results need to display the category’s<br />

name. Looking at the table diagram, you can see that the Category table contains the category name,<br />

so the Category table provides the category name. But you need to link the Category table to the Films<br />

table. The CategoryId field is in both tables, so the CategoryId field provides the link between the two.<br />

Remember that when you set up the database, the CategoryId field was a primary key field in the Category<br />

table and a foreign key field in the Films table; this field provides the link between the two data sets they<br />

hold. Finally, the results ask for each film in a member’s favorite category. The FavCategory table contains<br />

this information, but again it just contains MemberId and CategoryId, so in order to get the information in<br />

a human-friendly format, you need to link these two tables to the MemberDetails and Category tables.<br />

Below is a list summing up the tables you need to use to get the results you want:<br />

❑ MemberDetails<br />

❑ Films<br />

Extracting Information<br />

93

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

Saved successfully!

Ooh no, something went wrong!