CS145 - Introduction to Databases
Using the RA Relational Algebra Interpreter

Basics

RA
is a relational algebra interpreter that translates relational algebra queries into SQL queries, then executes the SQL on a standard relational database system. RA was developed by Prof. Jun Yang at Duke University. The version of RA we're using runs on the SQLite open-source relational database system. Much of this document is borrowed directly from Prof. Yang's RA home page at Duke.

Once you invoke RA over a database as described in your assignment, you will see the ra> prompt. For help, type "\help;". To exit the interpreter, type "\quit;". Use the "\list;" command to see what relations are available for querying in your database.

Advanced users: Using "\sqlexec_{statement};", you can send a SQL command to the underlying database. This feature allows you to manipulate the database in ways that you cannot with just relational algebra -- data modifications, for example.

The simplest relational algebra expression is one that returns the contents of a relation: type "relName;", where relName is the name of the relation. Note that every command or operator starts with a backslash (\), and every command or query must be terminated by a semicolon (;).

Relation and attribute names are case-insensitive. For example, pizzeria is the same as PIZZERIA. Attributes can take a variety of types -- details are unimportant, just be aware that INTEGER, SMALLINT, FLOAT, REAL, DOUBLE, DECIMAL, and NUMERIC are for numbers, while CHAR and VARCHAR are for strings.

Here is an example of a complex query. It finds all pizzas eaten by at least one person who does not frequent the 'Dominos' pizzeria.

   \project_{pizza} (
      ((\project_{name} Person)          // all people
       \diff
       (\project_{name}                  // people who frequent Dominos
           \select_{pizzeria='Dominos'}
                Frequents)
      \join Eats));                       // join with Eats to find pizzas
The syntax is insensitive to whitespace, and queries may span multiple lines; RA will number the lines (beyond the first one) for the current query. C/C++/Java-style comments (// and /*...*/) are supported, but they must appear before the closing semicolon.

Command-line options

RA supports command-line input history and editing using arrow keys: the Up and Down keys recall the previous and next lines respectively; the Left and Right keys move within the current line.

RA can execute a query specified in a file, as follows:

   /usr/class/cs145/bin/ra -i <filename>
Be sure to terminate the query in the file with a semicolon. A linebreak after the semicolon is required as well. Results can be written to a file:
   /usr/class/cs145/bin/ra -i <filename> -o <outfile> 

You can turn on verbose mode with the "-v" flag.

Operators

RA supports the following relational algebra operators:

Limitations

Currently, RA has the following limitations: