You are on page 1of 5

Database Systems

Assignment 4

Submitted By:
Name: Muhammad Haseeb
Reg No: FA20-BCS-053
Course: Database Systems
Date of Submission: 26th June 2022

Submitted To:
Sir Qasim Malik
COMSATS UNIVERSITY, ISLAMABAD
Department of Computer Science
Theory Assignment-4, Spring-2022

Course: Database Systems - I (CSC371) Class: BCS IV


Instructor: Qasim Malik Total Marks: 10

Problem-1: [CLO1] Indexing [2]


Consider a relation R (A, B, C, D, E) and the following query:
select * from R where A = '123' and B
= '456'.
Suppose relation R has 1000 tuples and have 50 unique values of A with each value appearing in exactly 20 tuples.
Furthermore, there are 100 unique values of B in R with each value appearing in exactly 10 tuples. For each of the
following scenarios, determine the number of tuples that might be accessed to answer the query.

i. No index is present on any attribute of R


1000 tuples

ii. An index is present on attribute A only


20 tuples

iii. An index is present on attribute B only


10 tuples

iv. A multi-attribute index is present on (A,B)


1 tuple

Problem-2: [CLO3] MongoDB Schema Design [3]


Suppose the database for a sports website needs to be built. The following set of requirements have been identified:

 Players group together to form teams. ▪ Teams have coaches.


 Teams can participate in tournaments.
 Tournaments comprise of matches.
 Referees adjudicate matches.
 Matches are played at a certain venue.
 A subset of players from a team participates in a certain match.
 For matches, apart from the basic information e.g., won/lost, its event-wise chronological coverage throughout
the course of the match, that include commentary and other associated information, e.g., goal scored, red card
given etc., also needs to be stored.

There are going to be three variants of the above scenario: one each for Football, Cricket, and Hockey. In order to
determine the variant on which you will be working, use the following formula:

Last_three_digits_of_your_registration_no modulus 3

where 0 refers to the Football, 1 to the Cricket, and 2 to the Hockey variant.
Given the above scenario, think of a single MongoDB collection and provide a sample document of it in JSON format.
Assume attributes as per your real-world knowledge about such a scenario.

HOCKEY
{
"name": "Asia Cup",
"noOfMatches": 10,
"startDate":"4-June-2022",
"endDate":"14-June-2022",
"matches":[{
"matchNO":3,
"matchname": "groupstage",
"venue": {
"name":"Islamabad Hockey Stadium",
"capacity": 40000},
"refrees":[{
"name":"Ali khan",
"experience": "44 Matches conducted"
},
{ "name":"Ahmad Ali",
"experience": "45 Matches conducted"
}],

"teams":[{
"name":"Pakistan",
"coaches":[{
"name":"Ahmad Ali",
"exp":"2 years"
},
{
"name":"Hassan Ali",
"exp":"5 years"
}],

"Players":[{
"name":"Ali khan",
"role":"Goal keeper"
},
{
"name":"Ahmad khan",
"role":"Striker"
},
{
"name":"Hassan khan",
"role":"Defender"
},
{
"name":"Ali Hassan",
"role":"Defender"
}],

"Goals Scored":4,
"redCards": 6,
"corners":13,
"penalities": 2,
"Commentators":[{
"name":"Ahmad Ali",
"country":"Pakistan"
},
{
"name":"Rahul Dravid",
"country":"India"
}]

}]

}]

Problem-3: [CLO4] Database Transactions [5]


Suppose you have two terminals, and create two connections to the hr database. We will use two colors (blue and red) to
indicate the two database sessions. In both the terminals, first set the transaction isolation level to serializable and answer
each of the following questions. Afterwards, set the isolation to read committed and answer them again. You can set the
isolation level using the following sql statement:
set transaction isolation level serializable;

Question 1: In first terminal (blue), we start a transaction and list all of the employees in the hr DB. Now, in the
second (red) terminal, we start a transaction and add an employee Chuck (via an insert command). After adding
the employee Chuck, we decide to generate a list of all accounts in the first (blue) terminal, and in the second (red)
terminal. What output do you expect to get? Are they the same or not? Why?
Answer:
When we insert the new employee (Chuck) in the Employee table in (red) terminal. Due to serializable it will only show
in the (red) terminal only. When we want to check in the (blue) terminal, it will not show there because no parallelism.
Only one will change where the changes are made by the user. So, they are not same at all.
Question 2: Next, we commit the transaction in the second (red) terminal, by issuing the COMMIT statement.
Now we go back to terminal 1 (blue) and regenerate the list of employees. Why is Chuck's row not there?
Answer:
Chuck account will not be there, because we commit (exit transaction) before save the changes and as according to the
ACID properties, the Durability property says All changes in the database must be permanent. So, commit before save in
(red) terminal can result in no show of Chuck account in the (blue) terminal.

Question 3: Now we commit the transaction in the first (blue) terminal and generate a new list of all employees
again. What output do you expect to see? Is it different than the output we received in question 2? Why or why
not?
Answer:
As changes are only occurred in the (red) terminal only. So, (blue) terminal has no changes only it is reviewing the list
made by the (red) terminal. But (red) terminal didn’t save its changes. So, every time when we generate a new list in the
(blue) terminal, it will not show the newly added employee account named Chuck.

.
Question 4: Now, let's try to modify the salary of Chuck. In the first (blue) terminal, we increase the salary by
1000. In the second (red) terminal, we decrease the salary of Chuck by 500. What would you expect to happen to
the execution of the second update? Why?
Answer:
If we made the new employee account in both terminals. Both can run at a same time. So, first when we modify the salary
of Chuck to 1000 in (blue) terminal, and save it. Now, decrease the salary by 500 in (red) terminal. After second update it
is expected that, Salary would decrease by 500. All depends upon the transaction changes save process by the (blue)
terminal. If (blue) save the changes. The (red) terminal can decrease the salary of employee.

Question 5: Let's say now we ROLLBACK the command in the first (blue) terminal, undoing the 1000 increase.
What would you expect would happen now? What happens to the second (red) terminal's transactions? What if
we commit in the second (red) terminal? What would you expect to see as Chuck's salary?
Answer:
If we ABORT the transaction by the (blue) terminal after changes made by the (red) terminal. It all depend upon the
commit of (red) terminal. If red commit after decreasing the salary. Blue can undo its own transaction so changes made
by (red) also undo. Otherwise. No more changes are expected if no commit.

You might also like