OQL uses a syntax that is similar to SQL, but it operates on objects, not tables. One form for queries is
Although the syntax is similar to SQL, there are some important differences. The expression list may contain the names of attributes of objects, identified by using dot notation, as in
The use of dot notation invokes the default get() method for the attributes named in the SELECT line. The result will be the values of pId and credits for each of the Student instances in the Students extent.
In addition to attributes, we can use methods in the expression list, which retrieves the result of applying the method. We could write, for example
which will return the name and age of the person whose pId is 1001, where the findAge() method is written to calculate a person’s age.
We can also use a relationship in the expression list. This retrieves the object or set of objects related to the “calling” object through the relationship. For example
retrieves the cNo and sectionCode of classes that student 999 takes, as well as the pId.
The list of variables in the FROM line is similar to defining an alias in SQL. Usually, we list the name of an extent, such as Students or People, and an identifier for the name of the variable, such as s or p, as we saw earlier. The variable is an iterator variable that ranges over the extent. There are alternate forms for declaring an iterator variable, such as
In the WHERE clause, we are restricted to a Boolean expression having constants and variables defined in the FROM clause. We can use <, <=, >, >=, !=, AND, OR, and NOT in the expression, as well as IN, and quantifiers EXISTS and FORALL. As in SQL, OQL does not eliminate duplicates, so the return is a bag (multiset) rather than a set. If we wish to eliminate duplicates, we can add the keyword DISTINCT as in SQL, producing a set. We can optionally add an ORDER BY clause, as in SQL.
Example 1. Simple Retrieval
Query Specification: Find the ID, name, and major of all undergraduates who have between 60 and 90 credits, in order by name.
OQL Query:
Example 2. Using a Relationship to Find Records through References
Query Specification: Find the names of all faculty in the Biology department.
OQL Query:
The first part of the FROM clause says that d is an iterator variable that ranges over the objects in the Departments extent, and the second part says that for each of the Department objects, the set of faculty identified by the relationship hasFaculty for that department is to be identified with the iterator variable f. This type of expression, in which one follows a relationship or a method to get to another object, is called a path expression. We are essentially setting up a nested loop that has d taking on each department in turn and f taking on each faculty member in that department. The WHERE line restricts the results to the Biology department, and the SELECT line displays just the faculty name.
Example 3. Subqueries in OQL
Query Specification: The previous query can be expressed in a form similar to nested queries in SQL. Find the names of all faculty in the Biology department, using a subquery. We can first find the Biology department object.
OQL Query:
We can use this as a subquery to provide the value for an iterator variable, which we will call b. Then we declare an iterator variable for b, which we will call f, for the relationship set hasFaculty. The entire query is expressed as follows.
OQL Query:
Example 4. Defining Structures for Returned Results within a Query
Query Specification: Find the name, rank, and salary of all faculty in the English department and put the results in a structure, in descending order by salary.
OQL Query:
Example 5. Using OQL Aggregation Operators
Like SQL, OQL includes the operators COUNT, SUM, AVG, MAX, and MIN, but they are used differently. In OQL, the operator is applied to a collection rather than to a column. COUNT returns an integer, while the return from the other operators has the same type as the collection. The operators can be used on the results of a query.
Query Specification: Find the average salary of faculty members in the History department.
OQL Query:
Query Specification: Find the number of undergraduates majoring in computer science.
OQL Query:
Query Specification: Find the course number and section of all the class sections that have more than 30 students in them.
OQL Query:
Example 6. Using the Set Operations of UNION, INTERSECTION, and EXCEPT (Difference)
Query Specification: Find the IDs and names of all students whose undergraduate major or graduate program is computer science.
OQL Query:
Example 7. Using Defined Queries
Defined queries allow us to create views that can be operated on. We give the query a name and then perform the desired operations on the results of the query.
Query Specification: Create a defined query to find all juniors and use the defined query to find the ID and name of all juniors.
OQL Query:
This definition can then be used in the query
We have chosen examples that are similar to SQL. OQL has many additional forms and capabilities that are not illustrated here.
3.142.43.10