You are on page 1of 7

Assignment: MBA – SEM III

Subject Code: MI0025

Database Management System

Set II
Question 1: Explain relationships with an example. Also show degree of
relationship for that example.

Answer: Relationships: A relationship is an association between entities. The


association or relationship that exists between the entities relates data items to
each other in a meaningful way. A collection of relationships of the same type is
called a relationship set.

A relationship type R is a set of associations between E1, E2 ...En entity types;


mathematically, R is a set of relationship instances ri.

e.g. consider a relationship type WORKS_FOR between two entity types –


employee and department, which associates each employee with the
department the employee works for. Each relationship instance in WORKS_FOR
associates one employee entity and one department entity, where each
relationship instance is ri which connects employee and department entities that
participate in ri.

Employee e1, e3 and e6 work for department d1, e2 and e4 work for d2 and e5 and
e7 work for d3. Relationship type R is a set of all relationship instances.

WORKS_FOR
E1
DEPARTMENT

E2
EMPLOYEE
R1
D1
E3
R2
D2
E4
R3
D3
R4

E6 R5

Eete EE5

Degree of Relationship: Degree of relationship is calculated by the number of


entity sets that participate in a relationship set. Hence, the degree of
relationship type for the aforementioned example will be binary as two
entities are associated.

In the WORKS_FOR relationship type, the employee plays the role of employee or
worker and department plays the role of department or employer. However in
some cases the same entity type participates more than once in a relationship
type in different roles. Such relationship types are called recursive.

e.g. employee entity type participates twice in supervision once in the role of a
supervisor and once in the role of a supervisee.
Question 2: Explain the different operations of relational algebra with an
example each.

Answer: Relational algebra operations are divided into two groups:

 Set operations like union, intersection, difference and Cartesian


product.
 Developed specifically for the relational databases, they are
select, project and join.

Select Operation: This operation is used to select the subset of the tuples from
a relation that satisfies a selection condition or search criteria. Mathematical
symbol σ [sigma] is used to denote the SELECT operator. The general syntax for
selection operation is shown below:

Sigma<selection condition>(<relation name>)

e.g. Select the employees who are working in department 10, and whose salary
is greater than Rs. 5000.

σ(DNO = 0 AND SALARY>5000)(EMPLOYEE)


Project Operation: Projection operation is used to select only few columns from
a table. The mathematical symbol for project operator is π and the syntax is as
below:

π<ATTRIBUTE LIST>(<relation>)

e.g. Select the name and salary of all employees:

πNAME.SALARY (EMPLOYEE)

Relations R and S

R
ENo Name
1 Jyothi
2 Ganga
3 Girija
4 Ankitha

S
ENo Name
3 Girija
4 Ankitha
5 Tanvi
6 Manvi

Intersection (Π): The intersection operator selects the common tuples from
the two relations. The result of the operation R Π S is:
ENo Name
3 Girija
4 Ankitha
Union (U): The result of this operation is denoted by R U S, is a relation that
includes all tuples that are either in R or in S or in both. Duplicate tuples will not
appear in the output.

ENo Name
1 Jyothi
2 Ganga
3 Girija
4 Ankitha
5 Tanvi
6 Manvi

Difference: The result of difference (R-S) consists of all tuples in R but not in S.

ENo Name
1 Jyothi
2 Ganga

Cartesian product (X): The Cartesian product is a binary operation used to


combine two relations. Assuming R and S as relations with n and m attributes
respectively, the Cartesian product R X S can be written as:

R(A1, A2.....An) X S(B1, B2........Bn)

The result of above operation set is:

Q(A1, A2.....An, B1, B2..........Bn)

Total number of columns in Q(degree Q) = n+m


Total number of tuples in Q (count Q) = Number of tuples in R * Number of tuples
in S

R
DNo Name
1 E&C
2 Computer Science
3 HRD

S
PNo PName
10 Networking
11 Payroll
Cartesian product of R and S can be written as:

DNo Name PNo PName


1 E&C 10 Networking
1 E&C 11 Payroll
2 Computer Science 10 Networking
2 Computer Science 11 Payroll
3 HRD 10 Networking
3 HRD 11 Payroll

Join Operation (x): The capability of retrieving multiple tables using a single
SQL statement can be accomplished using join operation. The join operation
denoted by x is used to combine two or more relations to retrieve useful
information.

The general form of join operation is:

Rx<join condition>S

For example, by joining employee and department relations, we can get the
name of the department in which the employee is working.

Select emp_no, ename, dept.dname from emp.dept


Where emp.deptno = dept_dept_no and emp_no = & emp_no

Question 3: a. Write SQL query for the following:


i. To find the student who scored more than ‘Rashmi’ in computer
Science subject.
ii. List all the students who belong to class II.
b. Define locks. Write a binary lock function.

Solution: (a)

i. Let’s consider the following table (Std_Marks) for the first


scenario:

Roll_No Student_Name Marks_CS


A101 Anand 60
A102 Rashmi 75
A103 Ram 80
A104 Pankaj 90
A105 Nita 60

SQL Query – Select * from Std_Marks where Marks_CS > (Select Marks_CS from
Std_Marks where Student_Name = ‘Rashmi’)

ii. Let’s consider the following table (Std_Class) for the second
scenario:
Roll_No Student_Name Class
A101 Anand I
A102 Rashmi II
A103 Ram II
A104 Pankaj III
A105 Nita II

SQL Query – Select * from Std_Class where Class = ‘II’

(b) Locks: A lock is a restriction on access to data in a multi-user environment.


It prevents multiple users from changing the same data simultaneously. If
locking is not used, data within the database may become logically incorrect and
may produce unexpected results.

Binary Lock Function: Two operations, lock_item and unlock_item, are used
with binary locking. A transaction requests access to an item X by first issuing a
lock_item(X) operation. If LOCK(X) = 1, the transaction is forced to wait. If
LOCK(X) = 0, it is set to 1 and the transaction is allowed to access item X. When
the transaction is through using the item, it issues an unlock_item(X) operation,
which sets LOCK(X) to 0, so that X may be accessed by another transaction.
Hence, a binary lock enforces mutual- exclusion on the data item.

Lock_Item(D):
B: if LOCK(X) = 0 (“item is unlocked”)
Then LOCK(X) 1 (“lock the item”)
else begin
wait (until lock(X) = 0 and the lock manager wakes up the
transaction);
goto B
end;
unlock_item(X):
LOCK(X) 0; (“unlock the item”)

You might also like