0% found this document useful (0 votes)
27 views4 pages

Assignment 2 Marking

The document provides sample SQL queries and relational algebra expressions to answer questions about a bank database schema. It includes 14 questions with suggested solutions for each. The questions involve retrieving data from multiple tables using joins, selections, projections, set operations, and renaming attributes. The solutions demonstrate the use of relational algebra operators, natural joins, theta joins, Cartesian products, projections, selections, set differences, unions, and renaming attributes. Two additional questions at the end are provided as examples translated to tuple relational calculus.

Uploaded by

Kerem Sarışen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views4 pages

Assignment 2 Marking

The document provides sample SQL queries and relational algebra expressions to answer questions about a bank database schema. It includes 14 questions with suggested solutions for each. The questions involve retrieving data from multiple tables using joins, selections, projections, set operations, and renaming attributes. The solutions demonstrate the use of relational algebra operators, natural joins, theta joins, Cartesian products, projections, selections, set differences, unions, and renaming attributes. Two additional questions at the end are provided as examples translated to tuple relational calculus.

Uploaded by

Kerem Sarışen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Question 1

Suggested Solution
[2] CREATE TABLE Band( [1] CREATE TABLE GreatestHits(
bandName CHAR(20), albumName CHAR(20),
startYear DATETIME, earliestDate DATETIME,
genre CHAR(20), latestDate DATETIME,
PRIMARY KEY(bandname) ) PRIMARY KEY(albumName),
FOREIGN KEY (albumName) REFERENCES Album)
[3] CREATE TABLE Musician(
msin CHAR(9), [2] CREATE TABLE Live(
musFName CHAR(20), albumName CHAR(20),
musLNname CHAR(20) NOT NULL, location CHAR(20) NOT NULL,
instrument CHAR(20), concertDate DATETIME,
musEmail CHAR(30) UNIQUE, PRIMARY KEY(albumName) NOT NULL,
PRIMARY KEY(msin) ) FOREIGN KEY (albumName) REFERENCES Album)

[2] CREATE TABLE Label( [3] CREATE TABLE Plays(


labelName CHAR(20), bandname CHAR(20),
country CHAR(20), msin CHAR(9),
labelEmail CHAR(30) UNIQUE, share REAL,
PRIMARY KEY(labelName) ) PRIMARY KEY(bandName, msin),
FOREIGN KEY(bandname) References Band,
[3] CREATE TABLE Song( FOREIGN KEY(msin) References Musician)
isrc CHAR(12),
title CHAR(20), [3] CREATE TABLE Performs(
songYear DATETIME, isrc CHAR(12),
duration REAL, msin CHAR(9),
bandName CHAR(20) NOT NULL, fee REAL,
PRIMARY KEY(isrc), PRIMARY KEY(isrc, msin),
FOREIGN KEY(bandName) References Band) FOREIGN KEY(isrc) References Song,
FOREIGN KEY(msin) References Musician)
[2] CREATE TABLE Album(
albumName CHAR(20), [3] CREATE TABLE Collects(
artist CHAR(20), isrc CHAR(12),
albumYear DATETIME, albumName CHAR(20),
PRIMARY KEY(albumName) ) PRIMARY KEY(isrc, albumName),
FOREIGN KEY(isrc) References Song,
[5] CREATE TABLE Sales( FOREIGN KEY(albumName) References Album)
isrc CHAR(12),
vendor CHAR(20), [3] CREATE TABLE Contract(
country CHAR(20), labelName CHAR(20),
salesDate DATETIME, msin CHAR(9),
amount REAL, PRIMARY KEY(msin),
quantity INTEGER, FOREIGN KEY(labelName) References Label,
PRIMARY KEY(isrc, vendor, country, FOREIGN KEY(msin) References Musician)
salesDate),
FOREIGN KEY(isrc) References Song)
Deduct marks as follows - with a maximum deduction for each table as shown in []s:
▪ -1/2 for each missing attribute
▪ -1 for including each of numSongs and member attributes
▪ -1 for each missing constraint (primary key, foreign key, not null or unique)
o Note the compound primary keys in sales, plays and collects - and the single attribute primary
key in contract. If not as shown above they are incorrect.
▪ -2 for creating unnecessary tables instead of using foreign keys (sells or records)
▪ -2 for not creating table for contract relationship
▪ -2 for incorrectly combining tables
▪ -4 for missing tables where data could not be stored not in database
▪ do not deduct marks for using different names for attributes – unless they are particularly bad!

Bank Schema
Customer = {customerID, firstName, lastName, birthDate, income}
Account = {accNumber, type, balance, branchNumberBranch}
Owns = {customerIDCustomer, accNumberAccount}
Transaction = {transNumber, accNumberAccount, amount, date, description}
Employee = {sin, firstName, lastName, salary, startDate, branchNumber Branch}
PersonalBanker = {customerIDCustomer, sinEmployee}
Branch = {branchNumber, branchName, city, street, numberEmployees, managerSIN Employee, budget}

Question 2
Each question is out of 3 (14*3 = 42), on this basis:
3 – correct
2 – minor error(s) (e.g. using < instead of >, missing bracket at end of query, incorrect format)
1 – partially correct with substantial errors in the query logic
0 – incorrect
Notes
▪ There are no marks given for the efficiency or elegance of the solutions!
▪ Do not deduct marks for very minor format errors (e.g. quote style or date formats)

The solutions given below are just sample solutions, there are multiple possible correct solutions. If you have
any doubts about whether a solution is correct do feel free to email me.

1. The branch names and cities of branches with budgets less than $1,000,000 or greater than $5,000,000
ΠbranchName,city(σbudget < 1000000  budget > 5000000 (Branch))

2. The account numbers, transaction numbers and amounts of transactions of transactions held at
branch number 23
ΠaccNumber,transNumber,amount(σbranchNumber = 23(Transaction ⋈ Acount))
lots of alternatives here, particularly Cartesian product and selection
3. The SIN, first and last names and salary of the manager of the branch that holds account number 178
πsin,firstName,lastName,salary(σaccNumber = 178(Account ⋈ (Employee ⋈sin = managerSinBranch)))
the theta join, could be X and a selection, or use a union-compatible intersection

4. The account numbers and customer IDs of joint accounts (accounts that are owned by more than one
customer)
πcustomerID,accNumber(σaccNumber = acc ∧ customerID != cid(Owns × owns2(cid, acc)(Owns)))
attributes of one copy of Owns needs to be renamed, though the table itself (owns2) does not

5. The SINs and salaries of employees who earn more than the manager of their branch
Πsin,salary(σbranchNumber = bn ∧ salary > bmsal(Employee ×
ρmanager(bn,bmsin,bmsal)(πbranchNumber,sin,salary(Employee ⋈sin = managerSin Branch)) ))

6. The branch names of branches that employ an employee whose last name is Wilson, but that do not
employ any employees whose last name is Maple; note that branch name is not a candidate key
πbranchName(πbranchNumber,branchName(σlastName = "Wilson"(Employee ⋈ Branch)) −
πbranchNumber,branchName(σlastName = "Maple"(Employee ⋈ Branch)) )
the set difference must include branchNumber since branchName is not a candidate key, note that
queries with <> (!=, etc.) are likely to be incorrect

7. The first names, last names and incomes of customers who own an account in a branch named
Lonsdale and the first names, last names and salaries of employees who work in a branch named
Kingsway (i.e. one query that returns one list of first and last names and either salaries or incomes)
πfirstName,lastName,income(σbranchName = "Lonsdale"(Customer ⋈ Owns ⋈ Account ⋈ Branch)) ∪
πfirstName,lastName,salary(σbranchName = "Kingsway"(Employee ⋈ Branch))
the result must have three columns, hence the use of union

8. The account numbers of all accounts owned (or co-owned) by customers who have an account in
Burnaby (a city)
ΠaccNumber(Owns ⋈ πcustomerID(σcity = "Burnaby"(Owns ⋈ Account ⋈ Branch)))
as usual, could be other versions but they need to find customer IDs with an account in Burnaby and
then include all accounts of that customer regardless of where the accounts are held

9. The customer IDs and birth dates of customers who own at least three accounts
πcustomerID,birthDate(Customer ⋈
σacc ≠ acc2  acc2 ≠ acc3((customerID, acc2)(Owns) ⋈ ((customerID, acc3)(Owns) ⋈ Owns))
not renaming customerID allows natural joins to be used – otherwise use Cartesian product and equate the
three customer IDs

10. The customer IDs of customers whose accounts have no transactions with amounts of which the
absolute value is less than $17,000 (i.e. all their transactions are greater than or equal to $17,000 or less
than or equal to -$17,000).
πcustomerID(Owns) – πcustomerID(σamount  17000  amount  -17000(Owns ⋈ Transaction))
11. The customer IDs and last names of customers who have at least one account in each city where
there is a branch
πcustomerID,lastName,city(Customer ⋈ Owns ⋈ Account ⋈ Branch) ÷ πcity(Branch)

12. The two highest distinct account balances


πbalance(Account) − πbal1(σbal1 < balance((acc1, t1, bal1, br1)(Account)  Account)) 
πbal1(σbal1 < balance((acc1,t1, bal1, br1)(Account)  Account)) −
πbal2(σbal2 < bal3(
πbal2(σbal2 < balance((acc2, t2, bal2, br2)(Account)  Account)) 
πbal3(σbal3 < balance((acc3, t3, bal3, br3)(Account)  Account)) ))
the highest balance union the second highest balance, note that bal2 and bal3 are the sets of balances
that do not contain the highest balance

Tuple Relational Calculus

The branch names and cities of branches with budgets less than $1,000,000 or greater than $5,000,000
13. {t | b  Branch(b.budget < 1000000  b.budget > 5000000 
t.branchName = b.branchName  t.city = b.city)}

The SIN, first and last names and salary of the manager of the branch that holds account number 178
14. {t | a Account, e  Employee, b  Branch(a.accNumber = 178 
a.branchNumber = b.branchNumber  e.sin = b.managerSin 
t.sin = e.sin  t.firstName = e.firstName  t.lastName = e.lastName  t.salary = e.salary)}

You might also like