Servlets and Nested Queries

Overview/Plan

Servlets

Motivation:

Comparison of CGI with Servlets
the CGI model matches each request with an execution of a CGI program   the Servlet model matches each request with a Java thread of a
     dynamically loaded module

Processing Form Data

Let's look again at our servlets from last time. They're at

http://cs.wellesley.edu:8080/webdb/

We'll look at

Extended Example

We'll spend some time discussing the Director Update example:

WebDB Examples

As an exercise, write a servlet that lists all movies whose titles match a form input. Hint: start with the MachineList example!

Stupid Servlet Tricks

Finally, let's look at a cute demo of how servlets differ from CGI scripts, in that they stay in the server. We can have a servlet that is continuously executing in a concurrent thread. For the purposes of this example, the servlet will just be computing prime numbers.

Take a look at it via the WebDB Examples

Nested Queries

The SQL standard defines a syntax for nested queries or subqueries. Indeed, that's where the term "Structured Query Language" comes from: so far, our language has been describable by a regular expression, but now we can put queries inside queries, so we'll need context-free grammars to describe the language.

Of course, we don't really care what class of formal language SQL falls into. We care about expressivity. There are queries we can't easily express without this ability. Try the following:

Use and Syntax

The basic rules for subqueries are:

Nested Queries vs Joins

In many cases, a nested query is equivalent to a join. Consider:

List actors who were directed by Clint Eastwood

How to Choose?

When this is the case, which should you choose? Considerations:

Efficiency of Query Execution (skippable)

In most cases, you should not care about efficiency. High performance databases will choose an execution plan that is efficient for the query, regardless of how you write it.

How do they do that? We'll discuss that on a later day, but essentially they:

Some of those decisions require human assistance, so experienced SQL programmers can intervene and give hints, but we will only be able to introduce the topic in this course.

Temporary Tables

Yet another way to skin the cat is to use temporary tables. For example, consider the dreaded "bi-coastal" query, that was in two homeworks.

The privileges of creating tables and creating temporary tables are separate. Most times, you will want to deny queries the privilege of creating tables in your database. Indeed, some of you who solved the bi-coastal problem using tables, but I wasn't able to run those queries because I didn't have the privilege of creating tables in your database.

However, it's usually safe to allow someone to create temporary tables.

Temporary tables are dropped when the connection is closed!

Natural, Left and Right Joins

We've talked about "joins" and "natural joins":

Look at the code in pub/mysql/joins.sql. You can load that into your own database to create and fill the tables. Then, try the following queries:

Exercise: Write a query to print actor/movie pairs using natural join.

Outer Joins

An outer join is one where if there isn't a match, the row gets listed once. There are three kinds:

(MySQL suggests sticking to left outer joins.) Consider:

Exercise: Write a query to print names and movie id if there is a credit.

You can find out more from MySQL Reference Manual: 13.1.7.1 Join Syntax

Correlated Queries

When the inner query is independent of the outer query, we can solve the problem using temporary tables and such. Consider the following:

List movies by directors over 50

pub/mysql/movies-with-directors-over-50.sql

Or, even worse

List actors in movies by directors over 50

We can build that up step by step. When I build these up, I like to put the names in the first time, and then remove them when I nest the query.

On the other hand, sometimes the subquery depends on a value in the surrounding query. These are called correlated subqueries. Consider the following variant

List people who direct themselves

pub/mysql/people-who-direct-themselves.sql

Notice how the credit.tt and person.nm in the subquery come from the surrounding query.

Of course, that query can also be implemented with a join:

pub/mysql/people-who-direct-themselves2.sql

Resources

A web search on "SQL nested subqueries tutorial" turns up many links. Here are some that look promising:

[an error occurred while processing this directive]