Beginning SQL
Beginning SQL Beginning SQL
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
- Page 176: Chapter 3 2. Using the following SQ
- Page 180: Chapter 3 When you execute the quer
- Page 184: Chapter 3 Now that you’re acquain
- Page 188: Chapter 3 It’s as simple as that!
- Page 192: Chapter 3 76 YearReleased 1947 1967
- Page 196: Chapter 3 78 FilmName Rating Gone w
- Page 200: Chapter 3 2. Add a WHERE clause to
- Page 204: Chapter 3 Executing this SQL provid
- Page 208: Chapter 3 84 FullName First name is
- Page 212: Chapter 3 86 FullName First name is
- Page 216: Chapter 3 Even though DateOfBirth i
- Page 220: Chapter 3 Member Name The member is
- Page 224: Chapter 3 92 The preceding SQL prod
- Page 230: That’s all well and good, but so
- Page 234: Category FirstName LastName War Jen
- Page 238: Extracting Information FirstName La
- Page 242: If you take this further and join t
- Page 246: SELECT * FROM FavCategory Figure 3-
- Page 250: Category MemberId Thriller 5 Thrill
- Page 254: Remember to add brackets around the
- Page 258: surprisingly, the MemberDetails tab
- Page 262: LocationId 2 2 2 2 1 1 1 Figure 3-6
- Page 266: includes that record. You should al
- Page 270: FirstName LastName DateOfBirth Jenn
- Page 274: 4 Advanced Database Design This cha
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