You are on page 1of 8

Green University of Bangladesh

Dept. of Computer Science and Engineering

Course Code: CSE-209


Course Title: Database System

Assignment No: 01
Assignment Title:

Assignment of Final Examination

Submitted by:
Student ID: 182002037 Section: DB
Student Name: Safiqul Islam
Semester: Spring-20
Date of Submission: 07/06/2020

Submitted to:
MD ANSARUL ISLAM

Lecturer
Dept. of Computer Science and Engineering
Green University of Bangladesh

Department of Computer Science and Engineering


Page 2 of 10

1. To maintain the consistency of database during transactions


which properties are used? Briefly explain each property.

Answer:

➢ To maintain the consistency of database during transactions some


properties are used known as ACID properties.
ACID refers to a database system’s four transaction properties:
Atomicity,
Consistency,
Isolation
Durability.
A transaction is a sequence of operations that satisfies these properties.

❖ Atomicity:
the database considers all transaction operations as one whole unit or atom.
when a database processes a transaction, it is either fully completed or not
executed at all. If a single portion of the transaction fails, the whole transaction
will fail.

Example:
For example, user A wants to withdraw 50 tk from his account and then
transfer it to the account of user B. Each transaction (withdrawing 50 tk from
account A and transferring 50 tk to account B) is counted as separate. If the
first transaction (withdrawing 50 tk) fails because (say) the server crashes
during the transaction, user A cannot transfer the money to user B.
Atomicity is particularly important to mitigate damage in case of database
server crashes. If a non-volatile database crashes in the middle of a
transaction, all changes done will be discarded or rolled back to avoid sending
partial results to the production database.
❖ Consistency:
Consistency is key to maintain data integrity. All inconsistent data is discarded,
and all transactions that might cause an inconsistency are aborted. Consistency
is a property ensuring that only valid data following all rules and constraints is
written in the database. When a transaction results in invalid data, the
database reverts to its previous state.
Page 3 of 10

Example:
if user A wants to withdraw 1,000 tk from his account, but only has a balance
of 500 tk, consistency will prevent him from withdrawing money and the
transaction will be aborted.

❖ Isolation:
Isolation is a property that guarantees the individuality of each transaction,
and prevents them from being affected from other transactions. It ensures that
transactions are securely and independently processed at the same time
without interference, but it does not ensure the order of transactions.

Example:
User A withdraws 100 tk and user B withdraws 250 tk from user Z’s account,
which has a balance of 1,000 tk. Since both A and B draw from Z’s account,
one of the users is required to wait until the other user transaction is
completed, avoiding inconsistent Data.
If B is required to wait, then B must wait until A’s transaction is completed, and
Z’s account balance changes to 900 tk. Now, B can withdraw 250 tk from this
900 tk balance.
❖ Durability:
Durability is a property that enforces completed transactions, guaranteeing that
once each one of them has been committed, it will remain in the system even in
case of subsequent failures. If a transaction is successful, all changes generated
by it are stored permanently.

Example:
User B may withdraw 100 tk only after user A’s transaction is completed and is
updated in the database. If the system fails before A’s transaction is logged in
the database, A cannot withdraw any money, and Z’s account returns to its
previous consistent state.
Page 4 of 10

2. Define conflict serializability. How to check if two schedule S and


S’ are conflict serializable or not- explain with example.?

Answer:
➢ Conflict Serializability: If a given non-serial schedule can be converted
into a serial schedule by swapping its non-conflicting operations, then it
is called as a conflict serializable schedule.

Two operations are said to be conflicting if all conditions satisfy:


Both the operations belong to different transactions.
Both the operations are on the same data item.
At least one of the two operations is a write operation.

Consider the following schedules S and S’ involving two transactions T1 and


T2.
S: R1(X) R1(Y) R2(X) R2(Y) W2(Y) W1(X)
S’: R1(X) R2(X) R2(Y) W2(Y) R1(Y) W1(X)

Two transactions of given schedules are:


T1: R1(X) R1(Y) W1(X)
T2: R2(X) R2(Y) W2(Y)
Let us first check serializability of S:
S: R1(X) R1(Y) R2(X) R2(Y) W2(Y) W1(X)
To convert it to a serial schedule, we have to swap non-conflicting operations
so that S becomes equivalent to serial schedule T1->T2 or T2->T1. In this case,
to convert it to a serial schedule, we must have to swap R2(X) and W1(X) but
they are conflicting. So S can’t be converted to a serial schedule.
Now, let us check serializability of S’:
S’: R1(X) R2(X) R2(Y) W2(Y) R1(Y) W1(X)
Swapping non conflicting operations R1(X) and R2(X) of S’, we get
S’1: R2(X) R1(X) R2(Y) W2(Y) R1(Y) W1(X)
Again, swapping non conflicting operations R1(X) and R2(Y) of S’1, we get
S’2: R2(X) R2(Y) R1(X) W2(Y) R1(Y) W1(X)
Again, swapping non conflicting operations R1(X) and W2(Y) of S’2, we get
S’3: R2(X) R2(Y) W2(Y) R1(X) R1(Y) W1(X)
which is equivalent to a serial schedule T2->T1.
Only S’ is conflict serializable.
Page 5 of 10

3. Facebook database contains millions of records. There are many


people whose name is Mehedi Hasan in Facebook.Now write a
SQL query to find those records whose first name starts with
mehedi. Is there any problem with the SQL query you have
written. Facebook may take long time to respond. What can be the
efficient way- explain with necessary SQL statements?

Answer:
➢ SQL Query: select*from Facebook where first_name='Mehedi';
This given SQL Query can search the required data which is stored in first_name
attribute as Mehedi. But it will take long time to give the output. Because the
size of the database is huge.

For this query, these are 2 different ways that SQL could find the results:

1) Do a "full table scan": look at every single row in the table, return the
matching rows.
2) Create an "index": Make a copy of the table sorted by first_name, then
do a binary search to find the row where the first_name is "Mehedi",
find the matching IDs, then do a binary search on the original table that
returns the rows that match the ID.
It would be faster to do a binary search on a sorted table if we already had
that table created.

3) Also Hashing could be a very efficient way for this kind of problem. It
would be more faster then “Indexing”. Hashing technique is used to
calculate the direct location of a data record on the disk without using
index structure. In this technique, data is stored at the data blocks
whose address is generated by using the hashing function. The memory
location where these records are stored is known as data bucket or data
blocks. In this, a hash function can choose the first_name column value
to generate the address.
Page 6 of 10

4. What is Functional Dependency?

Stu_Id Stu_Name Course_Id Course_Name Grade


Is there any partial functional dependency in the above table- justify
your answer.

Answer:
A functional dependency in databases is a dependency which satisfies
the properties of a function.

A dependency exists in a database table when an attribute value is dependent


on the value of a single or a group of attributes. The dependency is functional
when it satisfies the properties of a function and the main property of a
function is that it gives a single value for some input. In other words, if the
value of dependent attribute/s is same for some value of the determinant
attribute/s in all the tuples then it is a functional dependency.

Symbol:

A functional dependency is denoted by ‘→’

A→B

Where A is the determinant attribute and B is the dependent attribute. A → B


can be read as A determines B.

Stu_Id Stu_Name Course_Id Course_Name Grade

Here in this given table we have partial functional dependency.


Stu_Id → Stu_name and Course_Id → Course_Name and
Stu_Id, Course_Id→Grade (Stu_name , Course_Name).
Page 7 of 10

Here we can see Stu_Name is partially dependent on Stu_Id and


Course_Name is partially dependent on Course_Id. Then Grade is partially
dependent on Stu_Id and Course_Id.
Here Stu_name can’t be a primary key because name can be same in different
persons but we can uniquely identify name using Stu_Id, so here Stu_name is
partially dependent on Stu_Id and this same reason happens for Cours_Name
dependent on Course_Id.
Here Grade is Partially dependent on Stu_Id and Course_Id .Because without
the relation we can’t find the exact person who’s Grade and in what Course.

5. See the below transaction.

T1 T2
R(A)
W(A)
R(A)
R(B)
R(B)
W(B)

Is this schedule conflict serializable- explain on your answer.

Answer:
➢ Conflict Serializability: If a given non-serial schedule can be converted
into a serial schedule by swapping its non-conflicting operations, then it
is called as a conflict serializable schedule.

Two operations are said to be conflicting if all conditions satisfy:


Both the operations belong to different transactions.
Both the operations are on the same data item.
At least one of the two operations is a write operation.
Page 8 of 10

To check Conflict serializability 1st we have to swap between R1(B) and R2(B)
and it’s possible they are not conflicting.

T1 T2
R(A)
W(A)
R(A)
R(B)
R(B)
W(B)

Then we swap between R1(B) and R2(A) and it’s possible they are not
conflicting.
T1 T2
R(A)
W(A)
R(B)
R(A)
R(B)
W(B)

Then we swap between W1(B) and R2(B) and it’s not possible because they are
conflicting.
So as the schedule is not serial schedule after swapping all non-conflicting
operations so we can say that this given schedule is not conflict Serializable.

You might also like