![]()
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Introducing . . . SQL!In Lesson 3, Using Cloudview, you got a rude introduction to SQL and learned about the INSERT command. This section is a proper, more polite introduction to the language used to interact with the database management software.
|
Question: Would the following statements cause an error? |
Answer: No, because command words are case-insensitive. Database object identifiers are usually not case-sensitive either. |
You can also specify which rows to retrieve by constructing a search argument. A search argument consists of a clause to the SQL statement beginning with the keyword WHERE and is called a WHERE clause. A WHERE clause specifies conditions that the values stored in rows must meet in order to be retrieved.
Look for the saying whose NUMBER value is 2.
SELECT * FROM Sayings WHERE number = 2
The results appear in the bottom window. Only the row for which the NUMBER value is 2 appears in the window.
A join is a query in which you select data from related tables. One way to specify how the tables are related to one another is to use the JOIN . . . USING commands. JOIN means to join the two tables specified; USING specifies the column to use in joining the tables.
The two tables in the HelloWorldDB are related to each other by the NUMBER column (as you know, because you already created a foreign-key constraint on that column in the Responses table).
Look for all sayings and their corresponding responses.
SELECT SAYING, RESPONSE FROM Sayings JOIN Responses
USING (NUMBER)
This statement joins the two tables based on the NUMBER column, which has the same name in both tables. The value of the NUMBER column is what links a saying to its corresponding responses.
The results appear in the bottom window. Each saying appears with its related responses. A saying appears once for each response it has. Since in this database each saying has two responses, each saying appears twice in the result set, even though a particular saying appears only once in the Sayings table. Only those sayings with responses are shown.
You can combine search arguments with a join specification. Edit the current SQL-J statement so that it returns only those rows for which the value in the NUMBER column is 3.
When specifying a column in a WHERE clause for which the name is ambiguous, you must specify the table name along with the column name. Specifying the NUMBER column is ambiguous, because there is a NUMBER column in both Sayings and Responses.
You can also execute a join in a statement without the JOIN keyword. Instead, you use the WHERE clause to specify which columns must be related.
SELECT SAYING, RESPONSE FROM Sayings, Responses
WHERE SAYINGS.NUMBER = RESPONSES.NUMBER
You should get the same results as in the first query in this section.
NOTE: In the current release, Cloudscape can often execute such a statement faster than one using the JOIN keyword.
In Executing Methods from the Object Inspector, you executed methods on objects stored in the database using the Object Inspector window. You can also execute methods within a SQL-J statement.
Even though there are no Java data types in the Sayings or Responses tables, we can execute Java methods on them because VARCHAR values are automatically mapped to the java.lang.String class. SMALLINT values are automatically mapped to the java.lang.Short class. You can execute the methods of those classes on the values stored in the Sayings and Responses tables.
To execute a method on an object stored in a column, you add the method call with any parameters to the column name. A dot separates the column name from the method call. Cloudscape executes the methods once for each value returned in the result set for the particular column.
In this task, you will get all the sayings and convert them to uppercase.
NOTE: Remember that you delimit strings within SQL-J statements with single quotation marks.
SELECT SAYING.toUpperCase() FROM Sayings
Since toUpperCase is a Java method, its name is case-sensitive and must be typed exactly as it appears in the class definition.
SELECT NUMBER.toString().concat(' hello') FROM Sayings
Results appear in the bottom window.
Question: In the query in number 2 above, the toString() method is defined in what class? |
Answer: It is defined in the java.lang.Short class. SMALLINT values automatically map to java.lang.Short. |