Uploaded by 23110078

Database Exam: SQL, Relational Algebra, Tuple Calculus

advertisement
Important Information
This information may be useful during the exam. Feel free to use it or not as you wish.
(a) Reference for SQL Syntax
select dept_name, sum(salary) as total_salary
from instructor
group by dept_name;
(b) Reference for Relational Algebra
Name
Selection
Projection
Natural Join
Renaming
Theta Join
Symbol
σ
Π
▷◁
ρ
▷◁θ
Name
There exists
Belongs to
Symbol
∃
∈
(c) Reference for Tuple Calculus
(d) The primary keys are underlined in the schema.
1
Question 1: Multiple choice questions
[16 pts] Each question carries 2 marks for correct answer and a penalty of -1 for wrong answer. Partial
answers are not acceptable.
(a) If an attribute in a table is a foreign key, then the table cannot contain two tuples with the same value
of that attribute.
(a) True
(b) False
(b) Consider the following relational schema. Primary key attributes are underlined.
Person(SSN, name, address)
Car(license, year, model)
Accident(license, accident_date, driver, damage_amount)
Owns(SSN, license)
Which of the following queries are equivalent to this query in English?
Find the SSN of every person who owns one or more cars, none of which has ever been
involved in a car accident.
(a) SELECT O.SSN
FROM Owns O
WHERE O.license NOT IN (SELECT A.license FROM Accident A)
(b) ΠSSN (Owns) − ΠSSN (Owns ▷◁ Accident)
(c) {O|∃O1 ∈ Owns(O.SSN = O1.SSN ∧ ¬∃A ∈ Accident(O1.license = A.license))}
(d) None of the Above
(c) If the relation R(A,B) has m tuples, and the relation S(B,C) has n tuples and m < n, what is the
smallest and largest possible size of the output of the natural join of R and S? Choose one of the
following:
(a) m, m*n
(b) n, m+n
(c) m+n,n
(d) None of the above
(d) Every natural join can be rewritten using cross product and selection:
(a) True
(b) False
(e) [Multiple options are possible] Consider the following relational schema.
Students(rollno: integer, sname: string)
Courses(courseno: integer, cname: string)
Registration(rollno: integer, courseno: integer, percent: float)
Which of the following queries are equivalent to this query in English?
Find the distinct names of all students who score more than 90% in the course number
107.
2
(a) SELECT DISTINCT S.sname
FROM Students as S, Registration as R
WHERE R.rollno=S.rollno AND R.courseno=107 AND R.percent >90
(b) Πsname (σcourseno=107∧percent>90 (Registration ▷◁ Students))
(c) {T |∃S ∈ Students, ∃R ∈ Registration(S.rollno = R.rollno ∧ R.courseno = 107 ∧ R.percent >
90 ∧ T.sname = S.sname)}
(d) All of the above
(f) Given relations r(w, x) and s(y, z), the result of
select distinct w, x
from r, s
is guaranteed to be same as r, provided
(a) r has no duplicates and s is non-empty
(b) r and s have no duplicates
(c) s has no duplicates and r is non-empty
(d) r and s have the same number of tuples
(g) The following table has two attributes A and C where A is the primary key and C is the foreign key
referencing A with on-delete cascade.
A
2
3
4
5
7
9
6
C
4
4
3
2
2
5
4
The set of all tuples that must be additionally deleted to preserve referential integrity when the tuple
(2,4) is deleted is:
(a) (3,4) and (6,4)
(b) (5,2) and (7,2)
(c) (5,2), (7,2) and (9,5)
(d) (3,4), (4,3) and (6,4)
(h) Consider the following two tables and four queries in SQL. Book (isbn, bname) and Stock (isbn, copies)
Query 1: SELECT B.isbn, S.copies
FROM Book B INNER JOIN Stock S
ON B.isbn = S.isbn;
Query 2: SELECT B.isbn, S.copies
FROM Book B LEFT OUTER JOIN Stock S
ON B.isbn = S.isbn;
Query 3: SELECT B.isbn, S.copies
FROM Book B RIGHT OUTER JOIN Stock S
ON B.isbn = S.isbn;
3
Query 4: SELECT B.isbn, S.copies
FROM B B FULL OUTER JOIN Stock S
ON B.isbn = S.isbn;
Which one of the queries above is certain to have an output that is a superset of the outputs of the
other three queries?
(a) Query 1
(b) Query 2
(c) Query 3
(d) Query 4
Question 2: Formal Relational Query Languages
[8 pts] Consider two relations R(A,B), S(C,D). Answer True/false and explain in 1-2 sentences. For
each subpart, 1 point for marking correct True/False and 1 point for the correct explanation.
(a) Is the size of R▷◁A=C S always larger than or equal to the size of R▷◁A=C and B=D S?
(b) Is the size of R▷◁A=C and B̸=D S always larger than or equal to the size of R▷◁A=C and B=D S ?
(c) Are these two expressions equivalent σA=5 (R ▷◁B=C S) = (σA=5 (R)) ▷◁B=C S?
(d) Are these two expressions equivalent (RxS) - (R ▷◁B=C S) = R ▷◁B̸=C S?
Question 3: Matrix Multiplication
[6 pts] A matrix can be stored in a relation with three attributes: A (i,j,v) where i, j are the row and
column and v the value of the element in that row and column. For example the matrix:


30 5
0
 7 76 86
54 5 62
can be represented as:
i
1
1
1
2
2
2
3
3
3
j
1
2
3
1
2
3
1
2
3
v
30
5
0
7
76
86
54
5
62
You are given two matrices A, B with schemas: A(i,j,v) and B(j,k,v) Write an SQL query that
computes the product matrix A.B. Your query should return a set of triples (i, k, v) representing the
product matrix.
4
Question 4: SQL and Relational Algebra
[10 pts] The following database contains information about actors, plays, and roles performed.
Actor(actor_id, name, year_born)
Play(play_id, title, author, year_written)
Role(actor_id, character_name, play_id)
(a) [3 pts] Write a SQL query that returns the number of actors who have performed in three or more
different plays written by the author “August Wilson”.
(b) [4 pts] Write a SQL query that returns the names of all actors who have performed some play by
the author “Chekhov” and have never performed in any play written by author “Shakespeare”.
The list should not contain duplicates but does not need to be ordered.
(c) [3 pts] The following query returns information about all persons who have acted in a play that
they have written:
SELECT a.name, p.title, r.character_name
FROM Actor a, Play p, Role r
Where a.name = p.author AND a.actor_id = r.actor_id AND p.play_id = r.play_id
Give a equivalent relational algebra query for the above SQL query.
5
Download