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

ASIAN INSTITUTE OF COMPUTER STUDIES

Bachelor of Science in Computer Science


Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester

MODULE 8: RELATIONAL ALGEBRA AND RELATIONAL CALCULUS


WEEK 8

Learning Outcomes:
After completing this course you are expected to demonstrate the following:
1. Discuss the relational algebra and its queries and explain the relational calculus.

A. Engage
Comparison And Contrast
Instruction: Using the table below, compare and contrast each type of database model that
has been discussed on the previous module.

Type of Model Comparison Contrast

Hierarchical Database Model

Network Database Model

B. Explore
Video Title: Relational Algebra and Relational Calculus – Week 8
Module Video Filename: Module8 video – Relational Algebra and Relational Calculus

C. Explain
Historically, the relational algebra and calculus were developed before the SQL
language. In fact, in some ways, SQL is based on concepts from both the algebra and the
calculus, as we shall see. Because most relational DBMSs use SQL as their language, we
presented the SQL language first.
The relational algebra is very important for several reasons. First, it provides a formal
foundation for relational model operations. Second, and perhaps more important, it is used
as a basis for implementing and optimizing queries in the query processing and optimization
modules that are integral parts of Relational Database Management Systems (RDBMSs).
Whereas the algebra defines a set of operations for the relational model, the
relational calculus provides a higher-level declarative language for specifying relational
queries. A relational calculus expression creates a new relation. In a relational calculus
expression, there is no order of operations to specify how to retrieve the query result—only
what information the result should contain.

D. Elaborate
Introduction
Relational database systems are expected to be equipped with a query language that can assist
its users to query the database instances. There are two kinds of query languages − rela onal
algebra and relational calculus.
Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 1 of 10
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester

Relational Algebra
Relational algebra is a procedural query language, which takes instances of relations as input
and yields instances of relations as output. It uses operators to perform queries. An operator
can be either unary or binary. They accept relations as their input and yield relations as their
output. Relational algebra is performed recursively on a relation and intermediate results are
also considered relations.
The fundamental operations of relational algebra are as follows −
1. Select Operation (σ)
It selects tuples that satisfy the given predicate from a relation.
Notation − σp(r)
Where σ stands for selection predicate and r stands for relation. p is prepositional logic
formula which may use connectors like and, or, and not. These terms may use relational
operators like − =, ≠, ≥, < , >, ≤.
For example −

σsubject = "database"(Books)

Output − Selects tuples from books where subject is 'database'.

σsubject = "database" and price = "450"(Books)

Output − Selects tuples from books where subject is 'database' and 'price' is 450.

σsubject = "database" and price = "450" or year > "2010"(Books)

Output − Selects tuples from books where subject is 'database' and 'price' is 450 or those
books published after 2010.

2. Project Operation (∏)


It projects column(s) that satisfy a given predicate.
Notation − ∏A1, A2, An (r)
Where A1, A2 , An are attribute names of relation r.
Duplicate rows are automatically eliminated, as relation is a set.
For example −
∏ subject, author (Books)

Selects and projects columns named as subject and author from the relation Books.

3. Union Operation (∪
)
It performs binary union between two given relations and is defined as −
r ∪ s = { t | t ∈ r or t ∈ s}

Notation − r U s
Where r and s are either database relations or relation result set (temporary relation).

Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 2 of 10
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester

For a union operation to be valid, the following conditions must hold −

 r, and s must have the same number of attributes.


 Attribute domains must be compatible.
 Duplicate tuples are automatically eliminated.
∏ author (Books) ∪ ∏ author (Articles)

Output − Projects the names of the authors who have either written a book or an article or
both.

4. Set Difference (−)


The result of set difference operation is tuples, which are present in one relation but are
not in the second relation.
Notation − r − s
Finds all the tuples that are present in r but not in s.
∏ author (Books) − ∏ author (Articles)

Output − Provides the name of authors who have wri en books but not ar cles.

5. Cartesian Product (Χ)


Combines information of two different relations into one.
Notation − r Χ s
Where r and s are relations and their output will be defined as −
r Χ s = { q t | q ∈r and t ∈s}
σ author = 'tutorialspoint'(Books Χ Articles)

Output − Yields a rela on, which shows all the books and ar cles wri en by tutorialspoint.

6. Rename Operation (ρ)


The results of relational algebra are also relations but without any name. The rename
operation allows us to rename the output relation. 'rename' operation is denoted with
small Greek letter rho ρ.
Notation − ρ x (E)
Where the result of expression E is saved with name of x.
Additional operations are −

 Set intersection
 Assignment
 Natural join

Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 3 of 10
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester

Relational Calculus
In contrast to Relational Algebra, Relational Calculus is a non-procedural query language, that
is, it tells what to do but never explains how to do it.
Relational calculus exists in two forms −
1. Tuple Relational Calculus (TRC)
Filtering variable ranges over tuples
Notation − {T | Condi on}
Returns all tuples T that satisfies a condition.
For example −
{ T.name | Author(T) AND T.article = 'database' }

Output − Returns tuples with 'name' from Author who has wri en ar cle on 'database'.
TRC can be quantified. We can use Existential (∃
) and Universal Quan fiers (∀
).
For example −
{ R| ∃T ∈ Authors(T.article='database' AND R.name=T.name)}

Output − The above query will yield the same result as the previous one.

2. Domain Relational Calculus (DRC)


In DRC, the filtering variable uses the domain of attributes instead of entire tuple values
(as done in TRC, mentioned above).
Notation −
{ a1, a2, a3, ..., an | P (a1, a2, a3, ... ,an)}
Where a1, a2 are attributes and P stands for formulae built by inner attributes.
For example −
{< article, page, subject > | ∈TutorialsPoint∧ subject =
'database'}

Output − Yields Ar cle, Page, and Subject from the rela on TutorialsPoint, where subject
is database.
Just like TRC, DRC can also be written using existential and universal quantifiers. DRC also
involves relational operators.
The expression power of Tuple Relation Calculus and Domain Relation Calculus is
equivalent to Relational Algebra.

Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 4 of 10
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester

Summary:

 Relational algebra is a procedural query language, which takes instances of relations as


input and yields instances of relations as output. It uses operators to perform queries.
 Relational Calculus is a non-procedural query language, that is, it tells what to do but never
explains how to do it.
 The fundamental operations of relational algebra are Select Operation, project Operation,
Union Operation, Set Difference, Cartesian Product and Rename Operation.
 Relational calculus exists in two forms; the Tuple Relational Calculus and Domain Relational
Calculus.

E. Evaluate
ASSESSMENT:
Instruction: Answer the questions below. Write your answer in the Answer Sheet (AS)
provided for 2 points each.

Enumeration:

1-6. The fundamental operations of relational algebra.


7-8. Two forms of relational calculus.
9-10. Two kinds of query language.

Review Questions: (5 Points)


Explain the difference between Tuple Relational Calculus and Domain Relational Calculus.

Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 5 of 10
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester

Hands-On Activity 8.1

Login/Logout for Members and an Administrator

 Create the Logindb Database and Table

We will clarify the file structure by using a new name for the database and for the folder
containing the PHP and HTML pages. This is necessary because if students pile modification
upon modification, they can get into a terrible muddle. Therefore, as a general rule, we will
continue to use a separate folder and a new database name for each new chapter in this book.
However, a table can have the same name (for example, users) as the previous tutorial. This is
acceptable as long as the table is located in a different database folder.

Create a new folder and a new database as follows:

1. In the XAMPP htdocs folder, create a new folder called login.


2. Download the PHP files for Chapter 3 from the book’s page at http://www.apress.com,
and put them in the new login folder in htdocs. Alternatively, you can practice creating
the listings by typing each file in your HTML editor as you go through the chapter. Save
them in your login folder in the htdocs folder.
3. Now type http://localhost/phpmyadmin in the address field of your browser.
4. Click the Databases tab.
5. Create a new database called logindb.
6. Select the box next to logindb, and click Check Privileges.
7. Click Add User, and enter the following attributes:

User name: william


Host: localhost
Password: catonlap
Database name: logindb

1. Check all the global privileges, and then accept the default (zeros) for the resources.
2. Click the Go button at the bottom of the page.
3. In the htdocs’ login folder, create the mysqli_connect.php file so that it connects to logindb
as shown in the following listing:

Listing for the Snippet of Code Required for Connecting to the New Database (msqli_connect.
php)
<?php
// Create a connection to the logindb database and to MySQL.
// Set the encoding and the access details as constants:
DEFINE ('DB_USER', 'william');
DEFINE ('DB_PASSWORD', 'catonlap');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'logindb');
// Make the connection:
$dbcon = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
OR die ('Could not connect to MySQL: ' . mysqli_connect_error() );
// Set the encoding…
mysqli_set_charset($dbcon, 'utf8');

Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 6 of 10
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester

4. In the database logindb, create a new table named users with six columns. The details are
given in Table 8.1.
Table 8.1. The attributes for the users table

Next we will do a little housekeeping. We will remove the styling instructions from as many as
possible of the included files and transfer the styles to the main style sheet includes.css
 Tidy the Styling
Best practice decrees that as much as possible of the CSS styling should be incorporated in the
main style sheet. Therefore, the styling has been transferred from most of the included files to
the global style sheet includes.css.
Why is this necessary? The pages worked well without this step, so why do it? The answer is
that pages will not validate successfully unless the styles are removed from the included files.
Validation using the World Wide Web Consortium (W3C) validator is an important part of web
development to ensure that your code conforms to the standard expected by modern browsers
and web-site owners. Also, a page that validates will be more secure. Your clients will be
impressed if you can demonstrate that the pages on his web site conform to the latest W3C
recommendations. To demonstrate this to a client, I always add an HTML5 validation logo and
code to each page. This links to the URL http://validator.w3.org. If styling instructions form part
of an included file, the validator will display a red failure message and display the statement,
“Element style not allowed as child of element div.”
The revised style sheet is listed next. The items transferred from the included files are shown in
bold type in the following code snippet for the style sheet includes.css:
/*the global style sheet includes.css*/
body { text-align:center; background-color:#D7FFEB; color:navy;
font-family: "times new roman"; font-size: 100%; color: navy; margin: auto; }
h2 { font-size:150%; color:navy; text-align:center;}
h3 { font-size:110%; color:navy; text-align:center; }
#container { position:relative; min-width:960px; max-width:1200px;
margin:auto;
text-align:left; }
#header, #header-members { margin:10px auto 0 auto; min-width:960px; max-
width:1200px;
height:175px; background-image: url('images/tile-pale.jpg'); background-
repeat: repeat;
padding:0; color:white;
}
h1 { position:relative; top:40px; font-size:350%; color:white; margin:auto 0
auto 20px;
width: 487px; }
#reg-navigation ul { float:right; font-size:medium; width:160px; margin:-
150px 15px 0 88%; }
ul { position:absolute; top:190px; left:-10px; color:navy; width:135px;
Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 7 of 10
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester

text-align:center; margin:0;
/* set general side button styles */
li { width:115px; list-style-type :none; margin-bottom: 3px; text-align:
center; }
/* set general anchor styles */
li a { display: block; width:115px; color: white; font-weight: bold; text-
decoration: none }
/* specify state styles. */
/* mouseout (default) */
li a { background: #5B78BE; border: 4px outset #aabaff; }
/* mouseover */
li a:hover { display:block; background: #0a4adf; border: 4px outset #8abaff;
width:115px; }
/* onmousedown */
li a:active { background:#aecbff; border: 4px inset #aecbff; }
#midcol { width:90%; margin:auto; }
#mid-left-col { width:48%; float:left; text-align:left; }
#mid-right-col {width:48%; float:right; text-align:left; }
#content { margin-left:150px; margin-right:150px; }
table { width:500px; border:1px navy solid; border-collapse:collapse;
margin:auto; }
td { border:1px navy solid; padding:1px 0 1px 4px; text-align:left; }
form { margin-left:180px; }
#footer { margin:auto; text-align:center; }
p.error { color:red; font-size:105%; font-weight:bold; }
.label { float:left; width:210px; text-align:right; clear:left; margin-
right:5px; }
#submit { margin-left:215px; text-align:center; }
span.left { text-align:left; }
#loginfields input { float:left; margin-bottom:5px; }
#loginfields label { margin-bottom:5px; }
#loginfields span { float:left; }
#loginfields submit { margin:5px auto 5px auto; }
span.left { text-align:left; }

 Add a Login Button to the Home Page Header


We will now add a new button to the header menu on the home page so that members can log
in. We will also remove the View Users button because we want to arrange things so that
onlythe administrator is able to view the table of members. The revised header is shown in
Figure 8.1.

Figure 8.1. The revised header for the home page. The New Password button has been
removed, and the View Users button is replaced with a Login button
The View Users and New Password buttons have been removed, and a Login button is added.
The revised header will automatically be included in the non-private pages (page-2, page-3, and
so on) because the header file name is unchanged.

The new login link in the header is indicated in bold type in Listing 8.1.
Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 8 of 10
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester

Listing 8.1. Revising the Home page Header Menu (header.php)


<div id="header">
<h1>This is the header </h1>
<div id="reg-navigation">
<ul>
<li><a href="login.php">Login</a></li>
<li><a href="register-page.php">Register</a></li>
</ul>
</div>
</div>

With XAMPP running, enter http://localhost/login/index.php into a browser to view the home
page with the new login button as shown in Figure 8-1.
 Remove Redundant Buttons from the Registration and New Password Headers
A new header will be required for the registration page and the new password page because
the previous headers had redundant menu buttons,
On the registration page, the user has no password and so cannot log in; therefore, the Login
and New Password buttons will be removed. The Register button is redundant because the user
has already accessed the registration page. We will now remove the redundant buttons from
the header for the registration page and replace them with something more useful. The new
header is shown in Figure 8.2.

Figure 8.2. The new header for the Registration page and the New Password page
The redundant buttons are replaced by two meaningful links, Erase Entries and Cancel. I chose
the words “Erase Entries” rather than “Clear” or “Erase All” after testing the site with users who
were fairly new computer owners. They were confused by the “Clear” and “Erase All” wording,
but they immediately understood “Erase Entries.” The Erase Entries button reloads the
registration page and then displays empty fields. The Cancel button returns the user to the
home page. Listing 8.2a gives the revised header code for the registration and new password
pages.
Listing 8.2a. Replacing Redundant Buttons with Meaningful Buttons (register-header.php)
<div id="header">
<h1>This is the Header</h1>
<div id="reg-navigation">
<ul>
<li><a href="register-page.php">Erase Entries</a></li>
<li><a href="index.php">Cancel</a></li>
</ul>
</div>
</div>

Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 9 of 10
ASIAN INSTITUTE OF COMPUTER STUDIES
Bachelor of Science in Computer Science
Course Modules
CC225 - Information Management (Database System)
2nd Year – 2nd Semester

The registration page for this chapter and subsequent chapters will be linked to the new header
as shown in bold type in the next snippet of code shown in Listing 8.2b.
Listing 8.2b. Including the New Header in the Registration Page (register-page.php)
<!doctype html>
<html lang=en>
<head>
<title>Register page</title>
<meta charset=utf-8>
<link rel="stylesheet" type="text/css" href="includes.css">
</head>
<body>
<div id="container">
<?php include("register-header.php"); ?>
<?php include("nav.php"); ?>
<?php include("info-col.php"); ?>

References:

1. Lecture Notes on Relational Algebra and Calculus

https://utexas.instructure.com/files/35778323/download?download_frd=1

https://www.tutorialspoint.com/dbms/relational_algebra.htm
2. West, A. W. (2013). Practical PHP and MySQL Web Site Databases: A Simplified Approach,
ISBN-13 (electronic): 978-1-4302-6077-6

Facilitated By:

Name :

MS Teams Account (email) :

Smart Phone Number :

Prepared and Validated By: Ms. Almira Sacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 10 of 10

You might also like