Module 2 DBMS Notes V 2.0 16-1-2024

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Module-2

RELATIONAL MODEL
Q1) Define the following terms as they apply to the relational model of data: domain,
attribute, n-tuple, relation schema, relation state, degree of a relation, relational database
schema, and relational database state.

Domains: A domain D is set of atomic values, atomic means each value in the domain is
individual. A common method for specifying a domain is to specify a data type from which the
data values forming the domain are drawn and also we should specify a name for the domain.
Some examples of domains are.
* Branch_names: The set of names of bank branches of character type.
* Account_Number: The set of 3 digit account numbers.
* Customer_Name: The set of names of persons having account in the bank.
* Balance: The set of values of amount in the account.
These are the logical definitions of domains.
The relational model represents the database as a collection of relations (tables) each of
which is assigned a unique name. Informally each relation resembles a table, where the table
consists of rows and columns. Each row in the table represents a collection of related data values.
These values can be interpreted as facts describing the real world entity. In relational model
terminology, a row is called a tuple and a column header is called an attribute and the table is
called a relation. The data type describing the types of values that can appear in each column is
represented by a domain of possible values.
Relation Schema: A relation schema R, denoted by R(A1,A2,.An), is made up of a relation name R
and a list of attributes A1, A2,….An. Each attribute Ai is the name of role played by some domain
D in the relation schema R.D is called the domain of Ai and is denoted by dom (Ai). A relation
schema is used to describe a relation; R is called name of this relation.

Degree of a Relation: The degree (or arity) of a relation is the number of attributes present in the
relation schema. An example for relation schema for a relation of degree 7 is STUDENT (Name,
Ssn, HomePhone, Address, OfficePhone, Age, GPA), here STUDENT is the name of the relation
which has 7 attributes. The below figure shows an example of STUDENT relation.

Database Management Systems/21CS53, ISE, DBIT 1


SSEM
A relation (or relation state) r of the relation schema R (A1, A2, ... , An), also denoted by r(R), is
a set of n-tuples. r = {t1, t2, ..., tm}. Each n-tuple t is an ordered list of n values t =<v1, v2,
...,vn>, where each value vi, 1 ≤ i≤ n, is an element of dom (Ai) or is a special NULL value. The
ith value in tuple t, which corresponds to the attribute Ai, is referred to as t[Ai] or t.Ai.

A relational database schema S is a set of relation schemas S = {R1, R2, ... , Rm} and a set of integrity
constraints IC. A relational database state DB of S is a set of relation states DB= {r1, r2, ... , rm} such
that each ri is a state of Ri and such that the ri relation states satisfy the integrity constraints specified in IC.
Figure below shows a relational database schema.
COMPANY={EMPLOYEE,DEPARTMENT,DEPT_LOCATIONS, PROJECT,WORKS_ON, DEPENDENT}.
The underlined attributes represent primary keys.
Relational database schema S is a set of relation schemas S={R1, R2,....., Rm}and a set of
integrity constraints IC.
EMPLOYEE
Name SSN Bdate Address Sex Salary Dno
DEPARTMENT
Dname Dnumber
PROJECT
Pname Pnumber Dnum
WORKS_ON
ESSN PNo Hours

Figure: The COMPANY relational database schema

Database Management Systems/21CS53, ISE, DBIT 2


SSEM
Q2) Explain the Characteristics of Relation.

The characteristics of a relation make it different from a file or a table.


1) Ordering of Tuples in a Relation. A relation is defined as a set of tuples. Mathematically,
elements of a set have no order among them; hence, tuples in a relation do not have any particular
order.
2) Ordering of values within a Tuple and an Alternative Definition of a Relation. In a relation,
an n-tuple is an ordered list of n values, so the ordering of values in a tuple is important.
An alternative definition of a relation can be given, making the ordering of values in a tuple
unnecessary. In this definition, a relation schema R={ A1,A2,. . . . . An} is a set of attributes, and a
relation state r(R) is a finite set of mapping r = {t1, t2,. . . . . tm }, where each tuple ti is a mapping
from R to D is the union of the attribute domains; that is, D=dom(A1) Udom(A2) U. . . . . Udom(An).
In this definition, t[Ai] must be in dom(Ai) for 1<=i<=n for each mapping t in r. Each mapping t i is
called a tuple.
According to this definition of a tuple as a mapping, a tuple can be considered as a set of
(<attribute>,<value>) pairs, where each pair gives the value of the mapping from an attribute A i to a
value vi from dom (Ai).
Example:
t = < (Name, Dick Davidson), (Ssn, 422-11-2320), (Home_phone, NULL), (Address, 3452 Elgin
Road), (Office_Phone, 749-1253), (Age, 25), (Gps, 3.53) >
3) Values and NULLs in the Tuples. Each value in a tuple is an atomic value; that is, it is not
divisible into components within the framework of the basic relational model.
An important concept is that of NULL values, which are used to represent the values of attributes
that may be unknown or may not apply to a tuple. A special value, called NULL, is used in these
cases.
Example: SUDENT tuples have NULL for their officephones.
4) Interpretation (Meaning) of a Relation. The relation schema can be interpreted as a declaration
or a type of assertion. For example, the scheme of the Student relation asserts that, in general, a
student entity has a Name, Ssn, Home_phone, Address, Office_phone,

Database Management Systems/21CS53, ISE, DBIT 3


SSEM
Age, and Gpa. Each tuple in the relation can then be interpreted as a fact or a particular instance of
the assertion. For example, the tuple in the relation asserts the fact that there is a STUDENT whose
Name is Dick Davidson, Ssn is 422-11-2320, Age is 25, and so on.
Q3) List and Explain the Relational Model Notations.
a. A relation schema R of degree n is denoted by R (A1, A2, . ................, An).
b. The letters Q, R, S denotes relation names.
c. The letters q, r, s denote relation states.
d. The letters t, u, v denote tuples.
e. In general, the name of a relation schema such as STUDENT also indicate the current set of
tuples in that relation – the current relation state – whereas STUDENT (Name, Ssn, …) refers
only to the relation schema.
f. An attribute A can be qualified with the relation name R to which it belongs by using the dot
notation R.A – for example, STUDENT.Name or STUDENT.Age. This is because the same
name may be used for two attributes in different relations. However, all attribute names in a
particular relation must be distinct.
g. An n-tuple t in a relation r( R) is denoted by t = < v1,v2,. . . . . vn>, where vi is the value
corresponding to attribute Ai. The following notation refers to component values of tuples.
i. Both t[Ai] and t.Ai (and sometimes t[i]) refer to the value vi int for attribute Ai.
ii.Both t[Au, Aw,…,Az] and t.( Au, Aw,…,Az), where Au, Aw,…,Azis a list of attributes from R, refer
to the subtuple of value <vu, vw, …, vz> from t corresponding to the attributes specified in the list.

Q4) Explain the Relational Model Constraints.

Constraints on databases can generally be divided into three main categories:


1) Constraints that are inherent in the data model are called as inherent model-based or implicit
constraints.
2) Constraints that can be directly expressed in schemas of the data model, typically by specifying
the min DDL and are called as schema-based or explicit constraints.
3) Constraints that cannot be directly expressed in schemas of the data model, and hence must be
expressed and enforced by the application program and are called as application-based or
semantic constraints or business rules.

Database Management Systems/21CS53, ISE, DBIT 4


SSEM
Domain Constraints: Domain constraints specify that the value of each attribute is A must be an
atomic value form the domain dom (A) for that attribute.
The data types associated with domains typically include standard numeric data types for integers
and real numbers. Characters, fixed length strings and variable length strings etc.,
Key Constraints: A relation is defined as set of tuples, and all elements of set are distinct. Hence
all the tuples in a relation must also be distinct i,e no two tuples have the same combination of
values for all their attributes.

Q5) Define the following: Superkey, Minimal Superkey, Candidate Key, Primary Key

Superkey: The subset of attributes of a relation schema R with the property that no two tuples in
any relation instance r of R will have the same combination of values for these attributes. Such set
of attributes is called superkey of the relation schema R.
A set of attributes SK of R such that no two tuples in any valid relation in stance r(R) will have the
same value for SK. That is, for any distinct tuples t1 and t2 in r(R), t1[SK]≠t2[SK]. Any such set of
attributes SK is called a superkey of the relation schema R. A superkey SK specifies a uniqueness
constraint that no two distinct tuples in any state r of R can have the same value for SK.

A key k of a relation schema R is a superkey of R with the additional property that removing any
attribute A from K leaves a set of attribute Kꞌ that is not a super key of R any more. Hence, a key
satisfies two constraints.

1. Two distinct tuples in any state of the relation cannot have identical values for (all) the
attributes in the key.
2. It is a minimal superkey – that is, a superkey from which we cannot remove any attributes
and still have the uniqueness constraint in condition 1 hold.
Minimal superkey: A superkey from which we cannot remove any attributes and still have the
uniqueness constraint hold.
For example, consider the relation with
STUDENT (Name, SSN, HomePhone, Address, Age, GPA), here the attribute set {SSN} is key
of STUDENT because no two student tuples can have same value for SSN. Any set of attributes
that include SSN – for example, {SSN, Name, Age } is a superkey however the superkey {SSN,
Database Management Systems/21CS53, ISE, DBIT 5
SSEM
Name, Age } is not a keyof STUDENT because removing Name or Age or both from the set still
leaves us with superkey. Hence a minimal superkey is a key of the relation. Here {SSN} is a
minimal superkey or key of relation STUDENT.

Candidate Key: In general a relation schema may have more than one key, i,e relation may have
more one attribute or set of attribute which can distinguish each tuple. In such cases each key is
called candidate key.

Primary Key: The candidate key whose vales are used to identify tuples in the relation is called
Primary Key. In the relation schema the attributes that form the primary key of a relation are
underlined.

Q6) Define Entity Integrity, Referential Integrity, Foreign Keys and Other Constraints.
Entity integrity constraints: The entity integrity constraints state that no primary key value can
be null. This is because the primary key value is used to identify individual tuples in the relation.
Note: Key constraints and entity integrity constraints are specified on individual relations.

Referential integrity constraint: is specified between two relations and is used to maintain
consistency among tuples of two relations.
Referential integrity constraints states that a tuple in one relation that refers to another relation
must refer to an existing tuple in that relation. For example consider,

Here the attribute DNO of EMPLOYEE gives the department number for which employee works;
hence DNO value in every EMPLOYEE tuple must match the Dnumber value of some tuples in the
DEPARTMENT relation.

Database Management Systems/21CS53, ISE, DBIT 6


SSEM
Foreign Key: A set of attributes FK in relation schema R1 is a foreign key of R1 if it satisfies the
following two rules.
i. The attributes in FK have the same domain as primary key attributes PK of another relation
schema R2; i,e the attributes FK are said to reference or refer to the relation R2. In other words
a value of FK in tuple t1of R1 = the value of PK for some tuples t2 in R2 (t1[FK]= t2[PK],
here the tuple t1 references or refers to the tuple t2)
ii. A value of FK in a tuple t1 of R1 either occurs as a value of PK for some tuples t2 in R2 or is
null.
In the above example DNO is the foreign key which refers to Dnumber in DEPARTMENT relation.

Other Types of Constraints


Constraints that cannot be directly expressed in schemas of the data model, and hence must be
expressed and enforced by the application program and are called as application-based or
semantic constraints or business rules. Examples of such constraints are the salary of an
employee should not exceed the salary of the empoyee‘s supervisor and the maximum number of
hours an employee can work on all projects per week is 56. Such constraints can be specified and
enforced within the application programs that update the database, or by using a general-purpose
constraint specification language. Mechanisms called triggers and assertions can be used. In SQL-
99, a CREATE ASSERTION and triggers statement is used for this purpose.

Q7) Discuss the various reasons that lead to the occurrence of NULL values in relations.
OR
Write a note on NULL and three valued logic.
SQL has various rules for dealing with NULL values. NULL is used to represent a missing value,
but that it usually has one of three different interpretations. Consider the following examples to
illustrate each of the meanings of NULL.
1. Unknown value. Aperson‘s date of birth is not known, so it is represented by NULL in the
database.
2. Unavailable or withheld value. A person has a home phone but does not want it to be listed,
so it is withheld and represented as NULL in the database.
3. Not applicable attribute. An attribute LastCollegeDegree would be NULL for a person who
has no college degrees because it does not apply to that person.

Database Management Systems/21CS53, ISE, DBIT 7


SSEM
It is often not possible to determine which of the meanings is intended; for example, a NULL
for the home phone of a person can have any of the three meanings. Hence, SQL does not
distinguish between the different meanings of NULL.
SQL uses a three-valued logic with values TRUE, FALSE and UNKNOWN instead of the
standard two-valued (Boolean) logic with values TRUE or FALSE.
SQL allows queries that check whether an attribute value is NULL. Rather than using = or <>
to compare an attribute value to NULL, SQL uses the comparison operators IS or IS NOT. This is
because SQL considers each NULL value as being distinct from every other NULL value, so
equality comparison is not appropriate.
Example: Retrieve the names of all employees who do not have supervisors.
SELECT Fname FROM EMPLOYEE
WHERE Super_ssn IS NULL;

Q8) Discuss the different types of update operations on relational database. Give example for
each.
The operations of relational model can be categorized in Retrieval and Updates. There are
three basic modification or update operations on relations.
1.Insert 2. Delete 3.Modify
The Insert Operation: it is used to insert new tuples in to a relation. The insert operation
provides a list of attribute values for a new tuple t that is to be inserted into a relation R. Insert can
violate any of the four constraints, Domain constraints, key constraint, referential integrity, entity
integrity. In such cases two options are available. The first option is to reject the insertion by
specifying why the insertion was rejected. The second option is to attempt to correct the reason
for rejecting the insertion.
Examples:
.Operation:
Insert<‘Madhu‖,‘G‘,‘Kumar‘,NULL,‘10-9-1998‘,‗Bangalore‘,25000,NULL,4>into EMPLOYEE.
Result: This insertion violates the entity integrity constraint (NULL for the primary key Ssn), So it
is rejected.
.Operation:
Insert<‘Anil‘,‘S‘,‘Kumar‘,1234,‘12-6-1998‘,‗Bangalore‘,20000,NULL,4>intoEMPLOYEE.

Database Management Systems/21CS53, ISE, DBIT 8


SSEM
Result: This insertion violates the key constraint because another tuple with the same Ssn value
already exists in the EMPLOYEE relation, and so it is rejected.
.Operation:
Insert<‘Kiran‘,‘G‘,‘Shetty‘,2345,‘13-7-1998‘,‗Mysore‘,22000,NULL,5>into EMPLOYEE.
Result: This insertion violates the referential integrity constraint specified on Dno in EMPLOYEE
because no corresponding tuple exists in DEPARTMENT with Dnumber=5.
.Operation:
Insert<‘Santosh‘,‘J‘,‘Kumar‘,3456,‘6-10-1998‘,‗Bangalore‘,25000,NULL,4>into EMPLOYEE.
Result: This insertion satisfies all constraints, so it is acceptable.
The Delete Operation: It is used to delete tuples from relation. The delete operation can violate
only referential integrity, if the tuple being deleted is referenced by the foreign keys from other
tuples in the database. If the deletion operation causes violation, there are three options. The first
option is to reject the deletion. The second option is to attempt to cascade the deletion by deleting
tuples that references the tuples that is being deleted. The third option is to modify the referencing
attribute values that cause the violation.
.Operation:
Delete the WORKS_ON tuple with Essn=1234 and Pno=10.

Result: This deletion is acceptable and deletes exactly one tupel.


.Operation:
Delete the EMPLOYEE tuple with Ssn=1234.
Result: This deletion is not acceptable, because there are tuples in WORKS_ON that refer to this
tuple. Hence, if the tuple in EMPLOYEE is deleted, referential integrity violations will result.

.Operation:
Delete the EMPLOYEE tuple with Ssn=2345.
Result: This deletion will result in even worse referential integrity violations, because the tuple
involved in referenced by tuples from the EMPLOYEE, DEPARTMENT, WORKS_ON, and
DEPENDENT relations.

The Modify Operation: The Update (or Modify) operation is used to change the values of one or

Database Management Systems/21CS53, ISE, DBIT 9


SSEM
more attributes in a tuple (or tuples) of some relation R. It is necessary to specify a condition on the
attributes of relation R to select the tuple (tuples) to be modified.
.Operation:
Update the salary of the EMPLOYEE tuple with Ssn=1234 to 29000.
Result: Acceptable.
.Operation:
Update the Dno of the EMPLOYEE tuple with Ssn=1234 to 1.
Result: Acceptable.
.Operation:
Update the Dno of the EMPLOYEE tuple with Ssn=1234 to 7.
Result: Unacceptable, because it violates referential integrity.
.Operation:
Update the Ssn of the EMPLOYEE tuple with Ssn=1234 to 2345.
Result: Unacceptable, because it violates primary key constraint by repeating a value that already
exists as a primary key in another tuple; it violates referential integrity constraints because there
are other relations that refer to the existing value of Ssn.
Modifying an attribute that is neither a primary key nor foreign key usually causes no problem; the
DBMS need only check to confirm the new value is of the correct data type and domain.

Q9)Write a note on SELECT and PROJECT relational algebra operations.

The SELECT Operation


The SELECT operation is used to select a subset of the tuples in a relation that satisfy selection
condition. We can use lower case Greek letter sigma (σ) to denote selection. The predicate

(condition) appears as subscript to σ. The argument relation is given in parentheses following the

σ. In general, the syntax for selection operation is

σ<selection condition> (<relation name> )here the <selection condition> is made up of number of
clauses of the form
<attribute name><relational operator><constant value> or
<attribute name><relational operator><attribute name>

Database Management Systems/21CS53, ISE, DBIT 10


SSEM
The relational operator may be any one of the operators {=, <, >, <=, >=, !=}
Clauses can be connected by the Boolean Operators AND, OR, NOT.
For example, select tuples for all employees who either works in department 4 and take salary
more than 25,000 or works in department 5 and take over salary more than 30,000
σ(DNO=4 AND Salary>25000) OR (DNO=5 AND Salary>30000) (EMPLOYEE)
The PROJECT Operation
The Project operation is a unary operation that returns its argument relation, with certain columns
left out. i.e it selects certain columns and discards the other columns. Projection is denoted by the
Greek letter Pi (π). The general form of the PROJECT Operation is

π<attribute list>(<relation name>)


For exampleπName, Salary(EMPLOYEE) selects only Name and Salary column from the
relation EMPLOYEE.
Sequence of Operations and the RENAME Operations
To apply several relational algebra operations, we can either write the operation as single
relational algebra expression by nesting the operation or we can apply one operation at a time
and create intermediate result relation. We must name the relations that hold the intermediate
results. For example, to retrieve Name, Age, and Salary of all employees who works in department
number 5 we must apply SELECT and a PROJECT operation.

Database Management Systems/21CS53, ISE, DBIT 11


SSEM
πName, Age, Salary (σ DNO = 5 (EMPLOYEE)). Or we can explicitly show the sequence of
operations, giving a name to each intermediate relation as

DEP5_EMPS σ DNO=5(EMPLOYEE)

RESULT πName, Age, Salary(DEP5_EMPS)


It is simpler to break down the complex sequence of operations by specifying intermediate result
relations than writing a single relational algebra expression. This technique is also useful to
rename the attributes in the intermediate and result relations. To rename the attributes in a
relation that results from applying a relation algebra operation, we simply list the new attribute
names in parentheses. For example

TEMP σ DNO=5 (EMPLOYEE)

R(EMPNAME, EMPAGE, EMPSALARY) π Name, Age, Salary(TEMP)

Q10) Explain UNION, INTERSECTION, MINUS (DIFFERENCE), CARTESIAN


PRODUCT (CROSS PRODUCT) Operations.
The set operations are the standard mathematical operations on sets, they apply to relational model
because a relation is defined to be set of tuples and can be used to process the tuples in two
relations as sets.
Union Compatible :Two relations R(A1, A2, . . . . .An ) and S(B1, B2, . . . , Bn) are said to be
union compatible if they have the same degree n and if dom(Ai) = dom(Bi) for 1 <= i<= n. This
means that the two relations have the same number of attributes and that each pair of
corresponding attributes have the same domain.
We can define the three operations UNION, INTERSECTION, and SET DIFFERENCE (also
called MINUS).
UNION: The result of this operation, denoted by R US, is a relation that includes all tuples that are
either in R or in S or in both R and S. Duplicate tuples is eliminated.

Note: RUS=SUR
For example, consider.

Database Management Systems/21CS53, ISE, DBIT 12


SSEM
STUDENT
FN LN
Susan Yao
Ramesh Shah
Johnny Kohler
Barbara Jones
Amy Ford
Jimmy Wang
Ernest Gilbert
INSTRUCTOR
FNAME LNAME
John Smith
Ricardo Browne
Susan Yao
Francis Johnson
Ramesh Shah

Here the relations STUDENT and INSTRUCTOR are union compatible and their tuple represent
the names of students and instructors respectively.
The result of union operation shows the names of all students and instructors.
STUDENT U INSTRUCTOR
FN LN
Susan Yao
Ramesh Shah
Johnny Kohler
Barbara Jones
Amy Ford
Jimmy Wang
Ernest Gilbert
John Smith
Ricardo Browne
Francis Johnson

INTERSECTION: The result of this operation, denoted byRNS is relation that includes all tuples
that are in common in both R and S.
Note: R ∩ S = S ∩ R

Database Management Systems/21CS53, ISE, DBIT 13


SSEM
The result of STUDENT ∩ INSTRUCTOR is
FN LN
Susan Yao
Ramesh Shah

Both UNION and INTERSECTION can be treated as an-ary operations applicable to any number
of relations because both are associative operations; that is
RU(SUT) = (RUS)UT and (R N S) N T = R N (S N T)
DEFFERENCE: The result of this operation, denoted by R–S, is a relation that includes all tuples
that are in R but not in S.
Note: R– S≠S–R
The result of INSTRUCTOR–STUDENT is
FN LN
John Smith
Ricardo Browne
Francis Johnson
The INTERSECTION can be expressed in terms of union and set difference as follows:
R∩S=R U S-(R-S)-(S-R)

The CARTESIAN PRODUCT (CROSS PRODUCT) Operation


It is binary set operation denoted by x. This is also known as CROSS PRODUCT or CROSS
JOIN. This operation is used to combine tuples from two relations so that related tuples can be
identified. In general, the result of R(A1,A2, ....,An) x S(B1,B2,Bm) is a relation Q with n + m
attributes Q(A1,A2,...An, B1,B2,Bm) in that order. The resulting relation Q has one tuple for each
combination of tuples – one from R and one from S. Hence if R as n R tuples and S has nS tuples, R
x S will have nR * nS tuples.
For example, consider
Relation ‘R’
Name Place
Ranga Tumkur
Soma Bangalore

Database Management Systems/21CS53, ISE, DBIT 14


SSEM
Relation ‘S’
College Department
SIT CSE
RVCE TCE

The result of RxS is the relation Q as shown below


Realation Q
Name Place College Department
Ranga Tumkur SIT CSE
Ranga Tumkur RVCE TCE
Soma Bangalore SIT CSE
Soma Bangalore RVCE TCE

Q11) Explain the JOIN and DIVISION Binary relational operations with an example.
The JOIN Operation

The JOIN operation denoted by ⋈ is used to combine related tuples from two relations into single
tuples. This operation is more important for any relational database with a more than a single
relation. Because allows to process relationships among relations. The general form JOIN operation
on two relations R(A1,A2, . . . . An) and S(B1,B2,…Bm) is

R⋈<Join condition>S
The result of join is a relation Q with n+m attributes Q(A1,A2,...An, B1,B2,…Bm) in that order; Q
has one tuple for each combination of tuples – one from R and one from S, whenever the
combination satisfies the join condition.
The join condition is of the form Ai$Bi, where Ai is an attribute of relation R and Bi is an attribute of
relation S and $ is one of the comparison operator {=, <, >, >=, <=, !=}.
Example:
Consider the relations EMPLOYEE(Ename, address, Dnum) and
DEPARTMENT(DeptNum, Dname, Location, Head_name).

DEPT_JOIN ← (EMPLOYEE⋈ Dnum=DeptNumANDHead_name=EnameDEPARTMENT

Database Management Systems/21CS53, ISE, DBIT 30


SSEM
Difference between JOIN and CARTESIAN PRODUCT: In join only the combinations of tuples
satisfying join condition appears in the result; whereas in CARTESIAN PRODUCT all
combinations of tuples are included in the result.

EQUIJOIN: The JOIN, which involves join condition with equality comparison only i,e the only
comparison operator used is = , is called an EQUIJOIN.
Example: Consider the relations
EMPLOYEE(Ename, Address, Dnum) and
DEPARTMENT (DeptNum, Dname, Location).
Then DEPT_JOIN (EMPLOYEE Dnum=DeptNumDEPARTMENT)

Then the result of this EQUIJOIN is the relation DEPT_JOIN (Ename, Address, Dnum, Deptnum,
Dname, Location)
This relation will have all the combination tuples which are having Dnum=Deptnum.
NATURAL JOIN: The result of Equijoin will have one or more pair of attributes that have
identical values in every tuple. In NATURAL JOIN this superfluous attributes are removed. Hence
NATURAL JOIN is basically an EQUIJOIN followed by removal of the superfluous (redundant)
attributes. NATURAL JOIN is denoted by *.The NATURAL JOIN requires the two join attributes
to have the same name in both the relations.
The general definition for NATURAL JOIN is Q R*(<list1>,<list2>)S
Example: DEPT_NAT EMPLOYEE * DEPARTMENT.

OUTER JOIN: It is the extension of JOIN operation. The OUTER JOIN operation is used to take the
UNION of tuples from two relations that are not UNION compatible. In normal, join operations
(NATURAL or EQUI JOIN ), only the matching tuples appear in the result, i.e the tuples without related
tuples and the tuples with null in the join attributes are eliminated. In OUTER JOIN all the tuples in relation
R and in relation S or both in the result, whether or not they have matching tuples in the other relation.
There are Three types of OUTER JOIN.
1. LEFTOUTER JOIN: The LEFTOUTER JOIN operation keeps every tuple in first or
left relation R in the result of RS; if no matching tuple is found in S, then attributes of S in

Database Management Systems/21CS53, ISE, DBIT 30


SSEM
the result are filled or padded with null values. The LEFT OUTER JOIN is denoted by the

symbol

2. RIGHTOUTERJOIN: The RIGHT OUTER JOIN is denoted by . The RIGHT


OUTER JOIN keeps every tuple in second or right relation S in the result of RS.

3. FULL OUTER JOIN: This operation is denoted by . FULL OUTER JOIN keeps
all the tuples in both left and right relation when no matching tuples are found, padding
them with null values as needed.

The DIVISION Operation

The DIVISION operation, denoted by , is useful for a special kind of query that sometimes
occurs in database applications.

In general, the DIVISION operation is applied to two relations R(Z) S(X), where X Z. Let
Y=Z- X (and hence Z=XY); that is, let Y be the set of attributes of R that are not attributes of S.
The result of DIVISION is a relation T(Y) that includes a tuple t if tuples t R appear in R with
tR[Y]=t, and with tR[X]=tS for every tuple tS in S. This means that, for a tuple t to appear in the
result T of the DIVISION, the values in t must appear in R in combination with every tuple in S.

Interpretation of the division operation A/B:

i. Divide the attributes of A into 2 sets: A1andA2.


ii. Divide the attributes of B into 2sets: B2 and B3.
iii. Where the sets A2 and B2 have the same attributes.
iv. For each set of values in B2:
v. Search in A2 for the sets of rows (having the same A1values) whose A2 values (taken
together) form a set which is the same as the set of B2‘s.
vi. For all the set of rows in A which satisfy the above search, pick out their A1values and put
them in the answer.

Database Management Systems/21CS53, ISE, DBIT 30


SSEM
Q12) Write a note on aggregate functions.
Some common database requests involving Aggregate functions (SUM, AVERAGE, MINIMUM,
MAXIMUM), Recursive Closer Operations etc., cannot be performed with standard relational
algebra. But most query languages for RDBMS include these capabilities. These additional
operations are.

Aggregate Functions: The Aggregate functions that are applied on numeric values include
SUM, AVERAGE, MAXIMUM, MINIMUM and COUNT used for counting number of tuples.
Example: For query involving aggregate functions is retrieving the average or total salary of all

employees. We can define a FUNCTION operation using the symbol (pronounced as―Script
F) to specify these types of requests as

<grouping attributes> <function list>(<relation name>)


where<grouping list>is a list of attributes of the relation specified in <relation name>, and
<function list>is a list of (<functions><attribute> ) pairs. In each pair <function > is one of the
allowed functions such as SUM, AVERAGE, MAXIMUM, MINIMUM, COUNT, and <attribute>
is an attribute in the relation <relation name>.
Example:Consider a relation EMPLOYEE (Ename, Enum, Dnum, Salary)

Dnum AVERAGESalary(EMPLOYEE).

Q13) Describe the steps of an algorithm for ER to relational mapping.


Step 1: Mapping of Regular Entity Types. For each regular (strong) entity type E in the ER
schema, create a relation R that includes all the simple attributes of E. Include only the simple
component attributes of a composite attribute. Choose one of the key attributes of E as the
primary key for R. If the chosen key of E is a composite, then the set of simple attributes that
form it will together form the primary key of R.
If multiple keys were identified for E during the conceptual design, the information describing the
attributes that form each additional key is kept in order to specify secondary (unique) keys of
relation R.
Step 2: Mapping of Weak Entity Types. For each weak entity type W in the ER schema with

Database Management Systems/21CS53, ISE, DBIT 30


SSEM
owner entity type E, create a relation Rand include all simple attributes (or simple components of
composite attributes) of W as attributes of R. In addition, include as foreign key attributes of R,
the primary key attribute(s) of the relation(s) that correspond to the owner entity type(s); this
takes care of mapping the identifying relationship type of W. The primary key of R is the
combination of the primary key(s) of the owner(s) and the partial key of the weak entity type W,
if any.
Step 3: Mapping of Binary 1:1 Relationship Types. For each binary 1:1 relationship type R in
the ER schema, identify the relations S and T that correspond to the entity types participating in
R. There are three possible approaches: (1) the foreign key approach, (2) the merged relationship approach, and (3)
the cross-reference or relationship relation approach. The first approach is the most useful and should be followed
unless special conditions exist, as we discuss below.
1. Foreign key approach: Choose one of the relations—S, say—and include as a foreign key in S
the primary key of T. It is better to choose an entity type with total participation in R in the role of
S. Include all the simple attributes (or simple components of composite attributes) of the 1:1
relationship type R as attributes of S.
2. Merged relation approach: An alternative mapping of a 1:1 relationship type is to merge the
two entity types and the relationship into a single relation. This is possible when both
participations are total, as this would indicate that the two tables will have the exact same number
of tuples at all times.
3. Cross-reference or relationship relation approach: The third option is to setup a third relation
R for the purpose of cross-referencing the primary keys of the two relations S and T representing
the entity types. As we will see, this approach is required for binary M:N relationships.
The relation R is called a relationship relation (or sometimes a lookup table), because each tuple
in R represents a relationship instance that relates one tuple from S with one tuple from T. The
relation R will include the primary key attributes of S and T as foreign keys to S and T. The
primary key of R will be one of the two foreign keys, and the other foreign key will be a unique
key of R.
Step 4: Mapping of Binary 1:N Relationship Types. For each regular binary 1:N relationship
type R, identify the relation S that represents the participating entity type at the N-side of the
relationship type. Include as foreign key in S the primary key of the relation T that represents the
other entity type participating in R. Include any simple attributes (or simple components of

Database Management Systems/21CS53, ISE, DBIT 30


SSEM
composite attributes) of the 1:N relationship type as attributes of S.
Step 5: Mapping of Binary M:N Relationship Types. For each binary M:N relationship type R,
create a new relation S to represent R. Include as foreign key attributes in S the primary keys of
the relations that represent the participating entity types; their combination will form the primary
key of S. Also include any simple attributes of the M:N relationship type (or simple components
of composite attributes) as attributes of S. Notice that we cannot represent an M:N relationship
type by a single foreign key attribute in one of the participating relations (as we did for 1:1 or 1:N
relationship types) because of the M:N cardinality ratio; we must create a separate relationship
relation S.
In the example, we map the M:N relationship type WORKS_ON from Figure by creating the
relation WORKS_ON in Figure. We include the primary keys of the PROJECT and EMPLOYEE
relations as foreign keys in WORKS_ON and rename them Pno and Essn, respectively. We also
include an attribute Hours in WORKS_ON to represent the Hours attribute of the relationship
type. The primary key of the WORKS_ON relation is the combination of the foreign key
attributes {Essn, Pno}. This relationship relation is shown in Figure 9.3(c).
Step 6: Mapping of Multivalued Attributes. For each multi valued attribute A, create a new
relation R. This relation R will include an attribute corresponding to A, plus the primary key
attribute K—as a foreign key in R—of the relation that represents the entity type or relationship
type that has A as a multivalued attribute. The primary key of R is the combination of A and K. If
the multivalued attribute is composite, we include its simple components.
Step 7: Mapping of N-ary Relationship Types. For each n-ary relation ship type R, where n > 2,
create a new relation S to represent R. Include as foreign key attributes in S the primary keys of
the relations that represent the participating entity types. Also include any simple attributes of the
n-ary relationship type (or simple components of composite attributes) as attributes of S. The
primary key of S is usually a combination of all the foreign keys that reference the relations
representing the participating entity types. However, if the cardinality constraints on any of the
entitytypes E participating in R is 1, then the primary key of S should not include the foreign key
attribute that references the relation E corresponding to E.

Database Management Systems/21CS53, ISE, DBIT 30


SSEM

You might also like