Assignment 2 Marking
Assignment 2 Marking
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)
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)
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)}