Project 5: Other Database Constructs


This project will involve adding more features of the SQL language to your DBMS. As this project will not use any features developed in Project 4, you may build off of either your Project 3 or Project 4 solution. There will not be a Project 4 Instructor Solution released.

Files in Project 5

  • You need to supply this file. You should probably copy the file from Project3 (or Project 4) or from Project3 solution released by the instructor.

What you need to do

All of the code you write for this project must be in "".

There is no need to write any database output to any files. Each test will be done on a clean, empty database.

Your program is allowed to use any built in modules of python, with the exception of sqlite3. sqlite3 is the reference implementation for this project, your output will be compared against it for correctness. Importing the sqlite3 module in will be considered academic dishonesty for these projects.

You may (and should) write additional functions and classes in "" to perform the needed actions.

Test Categories


These tests should pass if you completed Project 3. They should pass automatically at the start of the project. If these fail, you've made a regression (you have broken previously functional code).

DESC (Descending Ordering)

The DESC keyword indicates that the collation should be reversed, so that values are ordered in a descending manner instead of the default (ascending).

SELECT * FROM students ORDER BY name DESC, grade DESC;

Default Values

It is often useful to supply non-NULL default values for specific columns. For instance, if no grade is supplied for an insert statement, I may want to assume a grade of 0.0. The way you specify the default value for a column is in the CREATE TABLE statement:

CREATE TABLE students (name TEXT, grade REAL DEFAULT 0.0, id TEXT);

Additionally, if you want to insert a row with entirely default values, you can do so like:



Views are read-only named SELECT statements. They act like a table, but if any of the tables the underlying SELECT statements it draws from changes, the results returned are changed (on subsequent queries). Example:

CREATE TABLE students (name TEXT, grade REAL);
CREATE VIEW stu_view AS SELECT * FROM students WHERE grade > 3.0 ORDER BY name;
SELECT name FROM stu_view ORDER BY grade;

For simplicity, none of the columns of the view's underlying tables will share names.

Parameterized Queries

Parameterized queries make it easy to reuse queries with wildcard (?) values pulled from the variables in the programming language interface. You need to implement a executemany method on your Connection class that accepts two arguments: a SQL statement with wildcard placeholders, and list of tuples with the values that should be slotted in. Example:

conn.execute("CREATE TABLE students (name TEXT, grade REAL, class INTEGER);")
conn.executemany("INSERT INTO students VALUES (?, ?, 480);", [('Josh', 3.5), ('Tyler', 2.5), ('Grant', 3.0)])

Custom Collations

Custom collation functions allow the user to supply a function that specifies the way a column should be ordered. Defines a function that takes two arguments (left and right): and the function returns -1 if left is less than right, 0 if they are even and 1 otherwise. Then the create_collation method of the connection is called with two arguments (the name of the collation within SQL and the function itself). You need to write the create_collation and update your SELECT-related code to accept such collations.

Example test case:

conn.execute("CREATE TABLE students (name TEXT, grade REAL, class INTEGER);")
conn.executemany("INSERT INTO students VALUES (?, ?, 480);", [('Josh', 3.5, 480), ('Tyler', 2.5, 480), ('Tosh', 4.5, 450), ('Losh', 3.2, 450), ('Grant', 3.3, 480), ('Emily', 2.25, 450), ('James', 2.25, 450)])
conn.execute("SELECT * FROM students ORDER BY class, name;")
def collate_ignore_first_letter(string1, string2):
    string1 = string1[1:]
    string2 = string2[1:]
    if string1 == string2:
        return 0
    if string1 < string2:
        return -1
        return 1
conn.create_collation("skip", collate_ignore_first_letter)
conn.execute("SELECT * FROM students ORDER BY name COLLATE skip, grade;")

Be aware that DESC can be used with custom collations like so:

SELECT * FROM students ORDER BY name COLLATE skip DESC, grade DESC;

Aggregate Functions

You need to support the aggregate functions: min and max. Because we aren't implementing the GROUP BY clause, any SELECT statement using an aggregate function will only return a single row.


SELECT max(grade) FROM students ORDER BY grade;