Module 2 DBMS Notes V 2.0 16-1-2024
Module 2 DBMS Notes V 2.0 16-1-2024
Module 2 DBMS Notes V 2.0 16-1-2024
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.
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
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.
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.
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.
.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
(condition) appears as subscript to σ. The argument relation is given in parentheses following the
σ<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>
DEP5_EMPS σ DNO=5(EMPLOYEE)
Note: RUS=SUR
For example, consider.
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
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)
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).
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
symbol
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, 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.
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
Dnum AVERAGESalary(EMPLOYEE).