0% found this document useful (0 votes)
36 views7 pages

DB2 20210122 Text and Solutions

The document discusses triggers to maintain a materialized view that calculates student scores for courses based on exercise results. It presents two approaches for writing a trigger to handle inserting a new exercise result: with two separate triggers based on whether a student/course score already exists or not, or with a single trigger using an if/else statement to check for existence and either update or insert accordingly. It also discusses analyzing a concurrency control schedule with respect to 2PL, timestamp serializability (TS) mono and multi, and provides solutions and justifications.

Uploaded by

fjuopregheru5734
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)
36 views7 pages

DB2 20210122 Text and Solutions

The document discusses triggers to maintain a materialized view that calculates student scores for courses based on exercise results. It presents two approaches for writing a trigger to handle inserting a new exercise result: with two separate triggers based on whether a student/course score already exists or not, or with a single trigger using an if/else statement to check for existence and either update or insert accordingly. It also discusses analyzing a concurrency control schedule with respect to 2PL, timestamp serializability (TS) mono and multi, and provides solutions and justifications.

Uploaded by

fjuopregheru5734
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/ 7

DATA BASES 2 – JANUARY 22ND, 2021

PROFF. SARA COMAI, PIERO FRATERNALI

TRIGGER

Consider the following schema of a database storing data about exercises done by students for different
courses.

COURSE (COURSEID, CourseTitle)


EXERCISE (EXERCISEID, CourseId, Topic, Title, Text, Type, MaximumScore)
EXERCISERESULT (EXERCISEID, STUDENTID, Date, Time, PercentageOfCompletion, Bonus)
PercentageOfCompletion is an integer number between 0 and 100. The score for an exercise is
computed as the percentage of completion applied to the maximum score of that exercise, plus the
value in the bonus attribute.

All the scores are kept in a view scores( studid, courseid, score ) that, for each student and for each
course, computes the overall score of that student for that course, as the sum of the points obtained by
the student from all the solved exercises.

create materialized view Scores( studid, courseid, score ) as

select StudentId, CourseId, sum( PercentageOfCompletion / 100 * MaximumScore + Bonus )

from ExerciseResult JOIN Exercise on ExerciseID

group by StudentId, CourseId

Write a (set of) trigger(s) that incrementally maintain the materialized view, in response to the insertion
of a new ExerciseResult.

CONCURRENCY CONTROL

The following schedule is CSR. Classify it w.r.t. 2PL (without update locks), TS-mono and TS-multi,
GIVING TERSE BUT PRECISE JUSTIFICATIONS.

r1(X) r3(Z) r2(Y) r3(X) w4(Z) r2(Z) w1(X) r4(Z) r4(Y) w2(Y)

HOW TO ANSWER:

we expect a textual answer. For example, given the schedule fragment r2(X), r1(X), w1(X), r3(Y), ... you
can arrange it into columns and for the 2PL analysis write
readlock_2(X)

1. r2(X)

readlock_1(X)

2. r1(X)

unlock_2(X)

readlock_1(X) --> writelock_1(X)

3. w1(X)

readlock_3(Y)

4. r3(Y)

5. …

This is your schedule, you can copy&paste it:

1. r1(X)

2. r3(Z)

3. r2(Y)

4. r3(X)

5. w4(Z)

6. r2(Z)

7. w1(X)

8. r4(Z)

9. r4(Y)

10. w2(Y)
PHYSICAL DB

Table A(PKA, x, y, z) stores 100K tuples in 10K blocks in a primary sequentially-ordered sequential
structure sorted by X; x is unique and not null.

A table B(PKB, x, w) stores 125K tuples in 5K blocks in a hash primary storage with the hash function
defined on attribute x. The average cost for a lookup is 1.3 i/o ops.

On table B there is a secondary B +-tree index built on attribute x, with three levels and 2.5k leaf nodes.

Consider the two following queries and describe the most efficient execution plans you can think of for
them (estimating their costs) in the above scenario, knowing that 13 tuples in table B satisfy the
condition w<0.

QUERY 1)

select *

from A

where x not in (select x

from B)

QUERY 2)

select *

from A

where x not in (select x

from B

where w<0)
JPA

An application lets an employee edit the projects he is responsible for. A project has a name, a start and
end date, and a budget. The employee can create projects and assign other employees to them.
Employees have a name, a code. Employees also have one or more skills (e.g., software engineering,
database administration, development operations, etc.). After logging in, the employee accesses a
HOME PAGE that displays: 1) the list of his projects, ordered by end date descending and with the list of
employees allocated to each one; 2) the list of other employees, with the last name and the list of skills
possessed; 3) a form for creating a project; 4) a form for choosing a project and assigning it to another
employee. After creating a project or an assignment, the HOME PAGE is displayed again, with the
information updated.

Given the specifications

1. Design the Entity-Relationship diagram of the data model

2. Write the SQL DDL code or draw the graphical model of the logical schema corresponding to the
ER diagram

3. Write the entity classes of the ORM mapping, including annotations for the attributes and for
the relationships, fetch type of attributes and of relationships, and operation cascading policies for
relationships (when not by default). Motivate the design choices.

4. Specify the named queries (other than “checkCredentials”) used by the methods of the business
objects.

5. List the components of the application.

6. For the data access services in the business tier, specify the type of the EJB component and
write the complete signature of all the business methods. Motivate the design choices

7. Write the Java code of the entity method necessary for updating the relationship between the
projects and the employees who work in them

8. Write the Java code of the business methods that assigns an employee to a project
SOLUTIONS
TRIGGER

After inserting a new result we need to check if this is the FIRST result for the pair (student, course) and
therefore in the view there is no tuple for that pair, or if there is already a tuple to update in the view.

We can write two triggers, dedicated to these two cases, or distinguish the two cases within one single
trigger (with an if-then-else)

Solution 1 - LONGER VERSION with two triggers having opposite when conditions

create trigger AnotherResult_existing_tuple


after insert on ExerciseResult
when exists ( select * from Scores join Exercise on Cid = CourseId
where ExerciseId = new.ExerciseId and Sid = new.StudentId )
begin
update Scores set score = score +
( select new.PercentageOfCompletion / 100 * MaximumScore + new.Bonus
from Exercise where ExrciseId = new.ExerciseId )
where Sid = new.StudentId and Cid = ( select CourseId
from Exercise
where ExerciseId = new.ExerciseId ) ;
end;

create trigger AnotherResult_nonexisting_tuple


after insert on ExerciseResult
when not exists ( select * from Scores join Exercise on Cid = CourseId
where ExerciseId = new.ExerciseId and Sid = new.StudentId )
begin
insert into Scores values( new.StudentId,
( select CourseId from Exercise where ExerciseId = new.ExerciseId ),
( select new.PercentageOfCompletion / 100 * MaximumScore + new.Bonus
from Exercise
where ExrciseId = new.ExerciseId ) );
end;

Alternative syntax

insert into Scores ( select new.StudentId, CourseId,


new.PercentageOfCompletion / 100 * MaximumScore + new.Bonus
from Exercise
where ExrciseId = new.ExerciseId );
SIMPLER VERSION with only one trigger and a small trick to write it all down very compactly:

create trigger AnotherResult


after insert on ExerciseResult
declare integer c, p;
begin
select CourseId into c, new.PercentageOfCompletion / 100 * MaximumScore + new.Bonus into p
from Exercise
where ExrciseId = new.ExerciseId;

if( ( new.StudentId, c ) in ( select Sid, Cid from Scores ) )


then update Scores
set score = score + p
where ( Sid, Cid ) = ( new.StudentId, c );
else insert into Scores values(new.StudentId, c, p );
end;

CONCURRENCY CONTROL

(graphically)
upgrade
3 1
X r1 r3 w1 upgrade
4 2
4
Y r2 r4 w2
3
4 2
Z r3 w4 r2 r4
downgrade

It is 2PL

Anticipated readlocks: T4 requests the readlock on Y before doing the downgrade on Z

Delayed unlocks: Transaction 3 must release Z before w4  also X must be released before w4

Transaction 4 must release Y before w2  also Z must be released before w2

Upgrades : on X before w1, on Y before w2

Downgrades: on X T4 downgrades the writelock on w4

It isn’t TS mono: because of r3(x), w1(x) and r4(y),w2(y) and w4(z), r2(z)

It isn’t TS multi: because of r3(x), w1(x) and r4(y),w2(y)


PHYSICAL DATABASES

Query 1: Scan of A and, in parallel, scan of B+ to check if x exists or not in table B

Cost = 10K + 2.5K = 12.5K

Query 2: Full scan of B + cache the 13 values of x that satisfy the condition + scan of A

Cost = 5K + (13) * 10K  5K + 10K = 15K

Alternative solution:

A is sequentially ordered, so we can apply a binary search algorithm to find x

log_2(10K)=14 accesses to find a value in the worst case

Cost = 5K + 13*14 = 5,2K

JPA

See ppt

You might also like