RelaX is an online relational algebra tool that we will use for the assignments. It allows entering relational algebra expressions and executing them to get results. The data set can be loaded from a GitHub Gist including the Bookstore data set (GIST: 367f41bb51110ef3c84bb5f906f2fb87) used for sample relational algebra queries in this lab.
This lab has a practice component that is not graded and an assignment component.
The lab practices writing queries in relational algebra using a book database schema (GIST: 367f41bb51110ef3c84bb5f906f2fb87):
Book (isbn, pubDate, listPrice, publisher, title, copyright) Author (firstName, lastName, address) BookAuthor (isbn, firstName, lastName) Bookstore (name, address) Stock (storeName, isbn, storePrice, quantity)
The Author relation gives the author's first and last names and address. Each first name/last name pair is unique. The Book relation gives the isbn, title, copyright date, publication date, and recommended list price. Since a book can have multiple authors, the relation BookAuthor matches up authors (identified by name) with books (identified by ISBN). The Bookstore relation gives the name (key) and address of a book store. The Stock relation gives the bookstore name, the price, and the ISBN number of the book. listPrice and storePrice are real numbers. copyright and pubDate are integers (representing the year). All other attributes are strings.
- Return all books (ISBN only) with a list price over $50.
- Return all books (ISBN only) with a publish date before May. 11, 2020 or whose publisher is 'GenCo'.
- Return all addresses (both for authors and bookstores).
- Return all authors that have not published a book.
- Return the list of books written by Elle Padimi.
- Return the list of books written by Elle Padimi that cost less than $70.
- Find all authors (firstName, lastName) who have written books that have been published after July 1, 2020.
- Find all authors who have written more than one book.
- Find pairs of books with different ISBNs but the same title. A pair should be listed only once; e.g., list (i,j) but not (j,i).
- List all the books (ISBN only) that 'All Books' sells that 'Some Books' also sells.
- List all the books (ISBN only) that 'All Books' sells that 'Some Books' sells for less.
- Open question: You suggest an English question, and let's try answer it using relational algebra. You do not have to have an answer to your own question, but hopefully, you think you can answer it. Also, note that we cannot answer all questions using the subset of relational algebra that we have studied.
- π isbn (σlistPrice > 50 (Book))
- π isbn (σpubDate < date('2020-05-11') OR publisher = 'GenCo' (Book))
- π address (Author) ∪ πaddress (Bookstore)
- π firstName, lastName (Author) - πfirstName, lastName (BookAuthor)
- π isbn (σfirstName = 'Elle' and lastName = 'Padimi' (BookAuthor))
- π isbn (σfirstName = 'Elle' and lastName = 'Padimi' and listPrice < 70 (BookAuthor ⋈ Book))
- π firstName, lastName (σpubDate > date('2020-07-01') (BookAuthor ⋈ Book))
- π B1.firstName, B1.lastName (σB1.firstName = B2.firstName AND B1.lastName = B2.lastName AND B1.isbn != B2.isbn (ρ B1 BookAuthor ⨯ ρ B2 BookAuthor))
With a join:
π B1.firstName, B1.lastName (ρ B1 BookAuthor ⋈ B1.firstName = B2.firstName AND B1.lastName = B2.lastName AND B1.isbn != B2.isbn ρ B2 BookAuthor)
- π B1.isbn, B2.isbn (σB1.title = B2.title AND B1.isbn < B2.isbn (ρ B1 Book ⨯ ρ B2 Book))
With a join:
π B1.isbn, B2.isbn (ρ B1 Book ⋈ B1.title = B2.title AND B1.isbn < B2.isbn ρ B2 Book)
- π isbn (σstoreName = 'All Books' (Stock)) ∩ πisbn(σstoreName = 'Some Books' (Stock))
OR
π S1.isbn (σS1.storeName = 'All Books' AND S2.storeName = 'Some Books' and S1.isbn=S2.isbn(ρ S1 Stock ⨯ ρ S2 Stock))
OR
π S1.isbn (σS1.storeName = 'All Books' (ρ S1 Stock) ⋈ S1.isbn=S2.isbn σS2.storeName = 'Some Books' (ρ S2 Stock))
- π S1.isbn (σS1.isbn = S2.isbn AND S2.storePrice < S1.storePrice AND S1.storeName = 'All Books' AND S2.storeName = 'Some Books' (ρ S1 Stock ⨯ ρ S2 Stock))
OR
π S1.isbn (σS1.storeName = 'All Books' (ρ S1 Stock) ⋈ S1.isbn = S2.isbn AND S2.storePrice < S1.storePrice (σS2.storeName = 'Some Books' (ρ S2 Stock)))
Note: Cross-products ('x') should be replaced with joins for efficiency.
Challenge questions: