DBMS - Module 3 Notes
DBMS - Module 3 Notes
The relational model represents the database as a collection of relations. Informally, each relation resembles
a table of values or, to some extent, a flat file of records. It is called a flat file because each record has a simple
linear or flat structure. When a relation is thought of as a table of values, each row in the table represents a
collection of related data values.
In the formal relational model terminology, a row is called a tuple, 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.
Domain, Attributes, Tuples and Relation
Domain: A common method of specifying a domain is to specify a data type from which the data values
forming the domain are drawn. Some examples of domains are as follow:
• Ind_phone_numbers. The set of eight-digit phone numbers valid in India.
• Adh_number: The set of valid twelve-digit unique identification number of a person.
• Names: The set of character strings that represent names of persons.
• Grade_point_averages. Possible values of computed grade point averages; each must be a real
(floating-point) number between 0 and 10.
• Employee_ages. Possible ages of employees in a company; each must be an integer value between 15
and 60.
• Academic_department_names. The set of academic department names in a university, such as
Computer Science, Economics, and Physics.
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 a role played by some domain D in the relation schema R.
D is called the domain of Ai and is denoted by dom(Ai). R is called the name of this relation. The degree (or
arity) of a relation is the number of attributes n of its relation schema.
A relation of degree seven, which stores information about university students, would contain seven attributes
describing each student as follows:
STUDENT(Name, Ssn, Home_phone, Address, Office_phone, Age, Gpa)
Using the data type of each attribute, the definition is sometimes written as:
STUDENT(Name: string, Ssn: string, Home_phone: string, Address: string, Office_phone: string,
Age: integer, Gpa: real)
For this relation schema, STUDENT is the name of the relation, which has seven attributes. In the preceding
definition, we showed assignment of generic types such as string or integer to the attributes.
Relation State: A relation state is the current set of tuples (rows) in a relation (table) at a specific time.
Example: Consider the following relation schema
STUDENT(SID, Name, Age, Major)
This defines:
• SID: Student ID
• Name: Student Name
• Age: Student Age
• Major: Student's field of study
In this example with give two possible relation states
Relation State 1 (Initial State)
SID Name Age Major
S01 Riya 20 Computer Sci
S02 Arjun 22 Physics
Relation State 2 (After Insert)
SID Name Age Major
S01 Riya 20 Computer Sci
S02 Arjun 22 Physics
S03 Neha 21 Math
The value can also be NULL.
Figure below shows an example of a STUDENT relation, which corresponds to the STUDENT schema just
specified. Each tuple in the relation represents a particular student entity (or object). We display the relation
as a table, where each tuple is shown as a row and each attribute corresponds to a column header indicating a
role or interpretation of the values in that column. NULL values represent attributes whose values are unknown
or do not exist for some individual STUDENT tuple.
Definition of relation using set theory concept:
Example:
RollNo Grade
1 A
2 B
3 A
2. Ordering is Irrelevant
Example:
1 Raj 20
2 Anu 21
Raj 20 1
Anu 21 2
• Each attribute (column) must have atomic values (indivisible, single value from its domain).
• No sets, lists, or composite values are allowed in a single cell.
Example: A student’s "Phone" attribute should not contain {"12345", "67890"} together; instead, store them
in separate rows or another table.
Values of NULLs in Tuples: NULL values are used to represent the values of attributes that may be unknown
or may not apply to a tuple.
In the relation of STUDENT, some STUDENT tuples have NULL for their office phones because they do not
have an office (that is, office phone does not apply to these students). Another student has a NULL for home
phone, presumably because either he does not have a home phone or he has one but we do not know it
Interpretation of a Relation: There are two ways of interpreting a relation. Assertion and Predicate.
Assertion: In assertion interpretation, a relation is viewed as a set of facts — that is, every tuple in the
relation is taken as a true statement (or fact) about the real world. Rather than thinking of the relation as a
test (predicate) that returns true or false, we assert that each tuple is true — and any other tuple (not in the
relation) is not asserted (could be treated as unknown).
Example: Consider the relation with the following tuples
STUDENT(SID, Name, Age)
SID Name Age
Riya 20
S01
S02 Aman 21
{EmpID}, {Email}, {AadharNo}, {EmpID, Name}, {EmpID, Dept}, {Email, Name}, {AadharNo,
Email, Dept}, {EmpID, AadharNo, Email, Name, Dept}
Primary Key: There can be any number of candidate keys in a relation. The chosen candidate key is
called as a Primary Key. The primary key is underlined in the table. That is the method of recognizing
it. In the above example
{EmpID} or {AadharNo} or {Email} can be the Primary key (any one not all).
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.
Example: Figure below shows a relational database schema that can be called
In each relation schema, the underlined attribute represents the primary key.
In Figure above, the Dnumber attribute in both DEPARTMENT and DEPT_LOCATIONS stands for the same
real-world concept—the number given to a department. That same concept is called Dno in EMPLOYEE and
Dnum in PROJECT. Attributes that represent the same real-world concept may or may not have identical
names in different relations. Alternatively, attributes that represent different concepts may have the same name
in different relations. For example, we could have used the attribute name Name for both Pname of PROJECT
and Dname of DEPARTMENT
Figure below shows a relational database state corresponding to the COMPANY schema.
A database state that does not obey all the integrity constraints is called not valid, and a state that satisfies all
the constraints in the defined set of integrity constraints IC is called a valid state.
Each relational DBMS must have a data definition language (DDL) for defining a relational database schema.
Current relational DBMSs are mostly using SQL for this purpose.
ENTITY INTEGRITY, REFERENTIAL INTEGRITY AND FOREIGN KEYS
Entity Integrity Constraint: The entity integrity constraint states that no primary key value can be NULL.
This is because the primary key value is used to identify individual tuples in a relation. Having NULL values
for the primary key implies that we cannot identify some tuples.
Referential Integrity Constraint: Key constraints and entity integrity constraints are specified on individual
relations. The referential integrity constraint is specified between two relations and is used to maintain the
consistency among tuples in the two relations. Informally, the referential integrity constraint states that a tuple
in one relation that refers to another relation must refer to an existing tuple in that relation.
Example: For example, in Figure above, the attribute Dno of EMPLOYEE gives the department number for
which each employee works; hence, its value in every EMPLOYEE tuple must match the Dnumber value of
some tuple in the DEPARTMENT relation.
To define referential integrity more formally, first we define the concept of a foreign key.
Referential integrity constraints typically arise from the relationships among the entities represented by the
relation schemas.
Example: Consider the database shown in Figure above. In the EMPLOYEE relation,
✓ The attribute Dno refers to the department for which an employee works; hence, we designate Dno to
be a foreign key of EMPLOYEE referencing the DEPARTMENT relation. This means that a value of
Dno in any tuple t1 of the EMPLOYEE relation must match a value of the primary key of
DEPARTMENT—the Dnumber attribute—in some tuple t2 of the DEPARTMENT relation
✓ The value of Dno can be NULL if the employee does not belong to a department or will be assigned
to a department later.
✓ Notice that a foreign key can refer to its own relation. For example, the attribute Super_ssn in
EMPLOYEE refers to the supervisor of an employee; this is another employee, represented by a tuple
in the EMPLOYEE relation. Hence, Super_ssn is a foreign key that references the EMPLOYEE
relation itself. In Figure above the tuple for employee ‘John Smith’ references the tuple for employee
‘Franklin Wong,’ indicating that ‘Franklin Wong’ is the supervisor of ‘John Smith’.
✓ We can diagrammatically display referential integrity constraints by drawing a directed arc from each
foreign key to the relation it references. For clarity, the arrowhead may point to the primary key of the
referenced relation.
OTHER TYPES OF CONSTRAINTS
Another class of general constraints, sometimes called semantic integrity constraints, are not part of the DDL
and have to be specified and enforced in a different way.
Examples of such constraints are the salary of an employee should not exceed the salary of the employee’s
supervisor and the maximum number of hours an employee can work on all projects per week is 56.
The types of constraints we discussed so far may be called state constraints because they define the
constraints that a valid state of the database must satisfy. Another type of constraint, called transition
constraints, can be defined to deal with state changes in the database.
An example of a transition constraint is: “An order can only transition from pending to payment received then
to processing and finally to shipped” – It would prevent an order from skipping the payment received stage
and going straight to shipped.
UPDATE OPERATIONS, TRANSACTIONS AND DEALING WITH CONSTRAINT
VIOLATIONS
Here we concentrate on the database modification or update operations. There are three basic operations that
can change the states of relations in the data base: Insert, Delete, and Update (or Modify). They insert new
data, delete old data, or modify existing data records, respectively. Insert is used to insert one or more new
tuples in a relation, Delete is used to delete tuples, and Update (or Modify) is used to change the values of
some attributes in existing tuples. Whenever these operations are applied, the integrity constraints specified
on the relational database schema should not be violated.
These operations are analyzed with the example relation COMPANY shown below
INSERT OPERATION
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 types of constraints.
✓ Domain constraints can be violated if an attribute value is given that does not appear in the
corresponding domain or is not of the appropriate data type.
✓ Key constraints can be violated if a key value in the new tuple t already exists in another tuple in the
relation r(R).
✓ Entity integrity can be violated if any part of the primary key of the new tuple t is NULL.
✓ Referential integrity can be violated if the value of any foreign key in t refers to a tuple that does not
exist in the referenced relation.
Here are some examples to illustrate this discussion.
In operation 3, the DBMS could either ask the user to change the value of Dno to some valid value (or set it
to NULL), or it could ask the user to insert a DEPARTMENT tuple with Dnumber = 7 and could accept the
original insertion only after such an operation was accepted. Notice that in the latter case the insertion violation
can cascade back to the EMPLOYEE relation if the user attempts to insert a tuple for department 7 with a
value for Mgr_ssn that does not exist in the EMPLOYEE relation.
THE DELETE OPERATION
The Delete operation can violate only referential integrity. This occurs if the tuple being deleted is referenced
by foreign keys from other tuples in the database. Here are some examples.
Several options are available if a deletion operation causes a violation.
• The first option, called restrict, is to reject the deletion.
• The second option, called cascade, is to attempt to cascade (or propagate) the deletion by deleting
tuples that reference the tuple that is being deleted.
For example, in operation 2,
the DBMS could automatically delete the offending tuples from WORKS_ON with Essn =
‘999887777’.
• A third option, called set null or set default, is to modify the referencing attribute values that cause the
violation; each such value is either set to NULL or changed to reference another default valid tuple.
Notice that if a referencing attribute that causes a violation is part of the primary key, it cannot be set
to NULL; otherwise, it would violate entity integrity.
For example, to avoid having operation 3 cause a violation,
The DBMS may automatically delete all tuples from WORKS_ON and DEPENDENT with Essn =
‘333445555’. Tuples in EMPLOYEE with Super_ssn = ‘333445555’ and the tuple in DEPARTMENT
with Mgr_ssn = ‘333445555’ can have their Super_ssn and Mgr_ssn values changed to other valid
values or to NULL.
THE UPDATE OPERATION
The Update (or Modify) operation is used to change the values of one or more attributes in a tuple (or tuples)
of some relation R. It is necessary to specify a condition on the attributes of the relation to select the tuple (or
tuples) to be modified. Here are some examples.
Updating an attribute that is neither part of a primary key nor part of a foreign key usually causes no problems;
the DBMS need only check to confirm that the new value is of the correct data type and domain. Modifying
a primary key value is simi lar to deleting one tuple and inserting another in its place because we use the
primary key to identify tuples.
THE TRANSACTION CONCEPT
A transaction is an executing program that includes some database operations, such as reading from the
database, or applying insertions, deletions, or updates to the database. At the end of the transaction, it must
leave the database in a valid or consistent state that satisfies all the constraints specified on the database
schema. A single transaction may involve any number of retrieval operations.
EXERCISE
Suppose that each of the following Update operations is applied directly to the database state shown in Figure
below. Discuss all integrity constraints violated by each operation, if any, and the different ways of enforcing
these constraints.
Example 2
LIBRARY MANAGEMENT SYSTEM
Relational Schema
BOOK
• PK: Book_ID
MEMBER
• PK: Member_ID
LIBRARIAN
• PK: Librarian_ID
ISSUE
• PK: Issue_ID
• FKs:
o Book_ID → BOOK(Book_ID)
o Member_ID → MEMBER(Member_ID)
o Librarian_ID → LIBRARIAN(Librarian_ID)
RETURN
• PK: Return_ID
• FK: Issue_ID → ISSUE(Issue_ID)
BOOK
LIBRARIAN
ISSUE
RETURN
where the symbol σ (sigma) is used to denote the SELECT operator and the selection condition is a Boolean
expression (condition) specified on the attributes of relation R.
Where <attribute name> is the name of an attribute of R, <comparison op> is normally one of the operators
{=, , ≥, ≠}, and <constant value> is a constant value from the attribute domain.
For example,
To select the tuples for all employees who either work in department 4 and make over $25,000 per year, or
work in department 5 and make over $30,000, we can specify the following SELECT operation:
we can always combine a cascade (or sequence) of SELECT operations into a single SELECT operation with
a conjunctive (AND) condition; that is,
Example:
Consider the relation called EMPLOYEE given below
where π (pi) is the symbol used to represent the PROJECT operation, and <attribute list> is the desired sublist
of attributes from the attributes of relation R.
The PROJECT operation removes any duplicate tuples, so the result of the PROJECT operation is a set of
distinct tuples, and hence a valid relation. This is known as duplicate elimination.
For example, consider the following PROJECT operation:
Notice that the tuple <’F’,25000>appears only once in Figure above, even though this combination of values
appears twice in the EMPLOYEE relation.
If duplicates are not eliminated, the result would be a multiset or bag of tuples rather than a set.
The number of tuples in a relation resulting from a PROJECT operation is always less than or equal to the
number of tuples in R. If the projection list is a superkey of R—that is, it includes some key of R—the resulting
relation has the same number of tuples as R. Moreover,
ρ(EmpName,EmpSSN,DeptNo)(EMPLOYEE)
This just changes attribute names inside the same relation.
Alternatively, we can explicitly show the sequence of operations, giving a name to each intermediate relation,
and using the assignment operation, denoted by ← (left arrow), as follows:
We can also rename the attributes in the intermediate and result relations. This can be useful in connection
with more complex operations such as UNION and JOIN,
To rename the attributes in a relation, we simply list the new attribute names in parentheses, as in the following
example:
The relation RESULT1 has the Ssn of all employees who work in department 5, whereas RESULT2 has the
Ssn of all employees who directly supervise an employee who works in department 5. The UNION operation
produces the tuples that are in either RESULT1 or RESULT2 or both while eliminating any duplicates. Thus,
the Ssn value ‘333445555’ appears only once in the result as shown in the figure below.
We can define the three operations UNION, INTERSECTION, and SET DIFFERENCE on two union-
compatible relations R and S as follows:
• UNION: The result of this operation, denoted by R ∪ S, is a relation that includes all tuples that are
either in R or in S or in both R and S. Duplicate tuples are eliminated.
• INTERSECTION: The result of this operation, denoted by R ∩ S, is a relation that includes all tuples
that are in both R and S.
• SET DIFFERENCE (or MINUS): The result of this operation, denoted by R – S, is a relation that
includes all tuples that are in R but not in S.
Figure below illustrates the three operations. The relations STUDENT and INSTRUCTOR in Figure (a) are
union compatible and their tuples represent the names of students and the names of instructors, respectively.
The result of the UNION operation in Figure (b) shows the names of all students and instructors. Note that
duplicate tuples appear only once in the result. The result of the INTERSECTION operation (Figure (c))
includes only those who are both students and instructors.
The resulting relations from this sequence of operations are shown in Figure (e) below. The
EMP_DEPENDENTS relation is the result of applying the CARTESIAN PRODUCT operation to
EMPNAMES from Figure (b) with DEPENDENT from Figure (c). In EMP_DEPENDENTS, every tuple from
EMPNAMES is combined with every tuple from DEPENDENT, giving a result that is not very meaningful
(every dependent is combined with every female employee). We want to combine a female employee tuple
only with her particular dependents—namely, the DEPENDENT tuples whose Essn value match the Ssn value
of the EMPLOYEE tuple. The ACTUAL_DEPENDENTS relation accomplishes this.
(a)
(b)
(c)
(d)
(e)
Example 2
EmpID Name
1 Alice
2 Bob
DeptID DeptName
10 HR
20 Engineering
Result of EMPLOYEE × DEPARTMENT
The first operation is illustrated in Figure BELOW. Note that Mgr_ssn is a foreign key of the DEPARTMENT
relation that references Ssn, the primary key of the EMPLOYEE relation. This referential integrity constraint
plays a role in having matching tuples in the referenced relation EMPLOYEE. The JOIN operation can be
specified as a CARTESIAN PRODUCT operation followed by a SELECT operation.
Theta Join: In JOIN, only combinations of tuples satisfying the join condition appear in the result, whereas
in the CARTESIAN PRODUCT all combinations of tuples are included in the result. The join condition is
specified on attributes from the two relations R and S and is evaluated for each combination of tuples. Each
tuple combination for which the join condition evaluates to TRUE is included in the resulting relation Q as a
single combined tuple.
where each <condition> is of the form Ai θ Bj, Ai is an attribute of R, Bj is an attribute of S, Ai and Bj have
the same domain, and θ (theta) is one of the comparison operators { =, <, ≤, >, ≥, ≠}. A JOIN operation with
such a general join condition is called a THETA JOIN. Tuples whose join attributes are NULL or for which
the join condition is FALSE do not appear in the result.
VARIATIONS OF JOIN: EQUIJOIN AND NATURAL JOIN
EQUIJOIN: The most common use of JOIN involves join conditions with equality comparisons only. Such
a JOIN, where the only comparison operator used is =, is called an EQUIJOIN. Both previous examples were
EQUIJOINs. Notice that in the result of an EQUIJOIN we always have one or more pairs of attributes that
have identical values in every tuple.
NATURAL JOIN: A natural join combines two relations (tables) by automatically matching all columns
with the same name and eliminating duplicate columns in the result.
Example:
Employee Table
Department Table
DeptID DeptName
10 HR
20 Engineering
40 Marketing
Result of Natural Join
1 Alice 10 HR
2 Bob 20 Engineering
Example 2
DeptID DeptName
10 HR
20 Engineering
30 Marketing
Example 3
Consider the Project Table
This joins the PROJECT and DEPT tables on the common attribute Dnum.
If the attributes on which the natural join is specified already have the same names in both relations, renaming
is unnecessary.
Notice that if no combination of tuples satisfies the join condition, the result of a JOIN is an empty relation
with zero tuples.
As another example, a JOIN operation can be specified as a CARTESIAN PRODUCT followed by a SELECT
operation, as we discussed:
Similarly, a NATURAL JOIN can be specified as a CARTESIAN PRODUCT preceded by RENAME and
followed by SELECT and PROJECT operations.
The DIVISION operation, denoted by ÷, is useful for a special kind of query that sometimes occurs in database
applications. T←R÷S
Notice that the tuples (values) b1 and b4 appear in R in combination with all three tuples in S; that is why they
appear in the resulting relation T. All other values of B in R do not appear with all the tuples in S and are not
selected: b2 does not appear with a2, and b3 does not appear with a1
An example is Retrieve the names of employees who work on all the projects that ‘John Smith’ works on. To
express this query using the DIVISION operation, proceed as follows.
4. Perform Division operation of SSN_PNOS and SMITH_PNOS. The division operation gives the Ssn
number of employees who work in all the projects worked by John Smith
5. Finally perform Natural Join of SSNS and EMPLOYEE and Project only the FName, MInit and
LName of these employees.
where <grouping attributes> is a list of attributes of the relation specified in R, and <function list> is a list of (<function>
<attribute> ) pairs.
For example, to retrieve each department number, the number of employees in the department, and their average
salary, while renaming the resulting attributes, we write:
RECURSIVE CLOSURE OPERATIONS
In relational algebra, recursive closure (sometimes called transitive closure) is used when a relationship is
recursive — meaning a tuple in a relation can be related to another tuple in the same relation.
Super_ssn is a foreign key referencing Ssn of another employee (the supervisor). This is a recursive
relationship, because both supervisee and supervisor come from the same table.
Suppose we want all supervisees of James Borg
• Direct supervisees: Franklin T Wong, Jennifer S Wallace
• Indirect supervisees: Ramesh Narayan & Joyce English (supervised by Franklin T Wong),
Ahmad Jabbar (supervised by Jennifer S Wallace)
By using single JOIN we can only get direct supervisees.
Recursive Closure operation is a solution to it
Department (S)
DeptID DeptName
10 Sales
20 HR
EMPLOYEE ⟕ DEPARTMENT Result is
DeptID DeptName
10 Sales
20 HR
30 Marketing
The result of Right Outer Join is
Example:
Suppose we have the following two relations:
EmpID Name
1 Alice
2 Bob
MANAGER
MgrID Dept
10 Sales
20 HR
Now we perform outer union
EMPLOYEE ⨝⨝ MANAGER
Since the schemas are different, the result is:
ID Name
1 Alice
2 Bob
Teacher Relation
ID Subject
3 Math
4 Science
The result of the outer Unioin is
ID Name Subject
1 Alice null
2 Bob null
3 null Math
4 null Science
3. Find the names of employees who work on all the projects controlled by department number 5.
In this query, we first create a table DEPT5_PROJS that contains the project numbers of all projects
controlled by department 5. Then we create a table EMP_PROJ that holds (Ssn, Pno) tuples, and apply
the division operation. Notice that we renamed the attributes so that they will be correctly used in the
division operation. Finally, we join the result of the division, which holds only Ssn val ues, with the
EMPLOYEE table to retrieve the Fname, Lname attributes from EMPLOYEE.
4. Make a list of project numbers for projects that involve an employee whose last name is ‘Smith’,
either as a worker or as a manager of the department that controls the project.