Banner

 

9 - Relational Algebra

Chuck, who recently began working for the Nature’s Vitamin store, wants to create a database to track his customers’ purchases. He starts to reflect on how other stores track purchases. He recalls that the hardware store always asks him for his zip code, and the dry cleaners always asks him for his phone number. He also knows that the video rental store gives him a card with a scan bar on the back and a membership number on the front. Last week when he went to the public library and had forgotten his library card, he used his social security number to pull up his record and check out some books. He wants to be able to track customer information (including what products each customer purchases on which days) and also be able to notify them of specials via e-mail or post.

  1. What attributes should be included in his customer table?
  2. What are the candidate keys? What is the primary key?
  3. On what grounds did you select your primary key?

Given the data model below:

Fill in the remaining attributes. For product, include product name and price. For order line, include the number of each product purchased on the specific order. For order, include the date of the order. Decide whether you should include total cost of the order.

Product

Orderline

Order

Customer

Order total can be derived by summing orderlineqty*prodprice.

Write the relational algebra and SQL statement for the following queries:

List information about all customers

customer
SELECT * FROM customer

List the customer's name and email

customer [custname, custemail]
SELECT custname, custemail FROM customer

List the products that sell for more than $10

prodname WHERE price > 10
SELECT prodname FROM product WHERE prodprice > 10

List the product name from the product table where the price is greater than $10

(product WHERE price > 10) [prodname]
SELECT prodname FROM product
WHERE prodprice > 10

Find all customers who live in Tampa with a zip code of 33456

(customer WHERE custcity = 'Tampa')
INTERSECT
(customer WHERE custzip = '33456')
SELECT * FROM customer
WHERE custcity = 'Tampa'
AND custzip = '33456'

Find all customers that do not live in zip codes 33456 or 33454

customer MINUS
((customer WHERE custzip code = '33456')
INTERSECT
(customer WHERE custzip code = '33454'))
SELECT * FROM customer
WHERE custzip NOT IN ('33456','33454')

List the name and phone number

(customer JOIN order WHERE
 customer.custid=order.custid)
	[custfname, custlname, custphone)
SELECT custfname, custlname, custphone
FROM customer, order
WHERE customer.custnum = order.custnum

Find the products that are on all orders

((orderline[prodid, orderid]
DIVIDEBY
ORDER[orderid])
JOIN
product) [prodid, prodname]
SELECT prodid, prodname FROM product
WHERE NOT EXISTS
 (SELECT * FROM order
  WHERE NOT EXISTS
  (SELECT * FROM orderline
   WHERE orderline.protid = product.protid
    AND orderline.ordeidm = order.ordeidm))

 

This page is part of the promotional and support material for Data Management (open edition) by Richard T. Watson
For questions and comments please contact the author
Date revised: 10-Dec-2021