Skip to content

Latest commit

 

History

History
 
 

lab1

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

COSC 304 - Introduction to Database Systems
Lab 1: Querying using Relational Algebra

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.

Relational Algebra Practice

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.

Questions: (Write the answer in relational algebra)

  1. Return all books (ISBN only) with a list price over $50.
  2. Return all books (ISBN only) with a publish date before May. 11, 2020 or whose publisher is 'GenCo'.
  3. Return all addresses (both for authors and bookstores).
  4. Return all authors that have not published a book.
  5. Return the list of books written by Elle Padimi.
  6. Return the list of books written by Elle Padimi that cost less than $70.
  7. Find all authors (firstName, lastName) who have written books that have been published after July 1, 2020.
  8. Find all authors who have written more than one book.
  9. 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).
  10. List all the books (ISBN only) that 'All Books' sells that 'Some Books' also sells.
  11. List all the books (ISBN only) that 'All Books' sells that 'Some Books' sells for less.
  12. 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.

Answers:

  1. π isbnlistPrice > 50 (Book))
  2. π isbnpubDate < date('2020-05-11') OR publisher = 'GenCo' (Book))
  3. π address (Author) ∪   πaddress (Bookstore)
  4. π firstName, lastName (Author) - πfirstName, lastName (BookAuthor)
  5. π isbnfirstName = 'Elle' and lastName = 'Padimi' (BookAuthor))
  6. π isbnfirstName = 'Elle' and lastName = 'Padimi' and listPrice < 70 (BookAuthor ⋈ Book))
  7. π firstName, lastNamepubDate > date('2020-07-01') (BookAuthor ⋈ Book))
  8. π B1.firstName, B1.lastNameB1.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)

  9. π B1.isbn, B2.isbnB1.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)

  10. π isbnstoreName = 'All Books' (Stock)) ∩   πisbnstoreName = 'Some Books' (Stock)) OR

    π S1.isbnS1.storeName = 'All Books' AND S2.storeName = 'Some Books' and S1.isbn=S2.isbn(ρ S1 Stock ⨯ ρ S2 Stock))

    OR

    π S1.isbnS1.storeName = 'All Books' (ρ S1 Stock) ⋈   S1.isbn=S2.isbn σS2.storeName = 'Some Books' (ρ S2 Stock))

  11. π S1.isbnS1.isbn = S2.isbn AND S2.storePrice < S1.storePrice AND S1.storeName = 'All Books' AND S2.storeName = 'Some Books' (ρ S1 Stock ⨯ ρ S2 Stock))

    OR

    π S1.isbnS1.storeName = 'All Books' (ρ S1 Stock) ⋈  S1.isbn = S2.isbn AND S2.storePrice < S1.storePriceS2.storeName = 'Some Books' (ρ S2 Stock)))

Note: Cross-products ('x') should be replaced with joins for efficiency.

Challenge questions:

  1. Find all the books published by Wiley that are in-stock at Amazon.
    Answer:

    π isbnpublisher='Wiley' AND quantity > 0 (Book ⋈ σstoreName='Amazon' (Stock)))

  2. List the addresses of stores that have pairs of books with the same title and different ISBNs.