0% found this document useful (0 votes)
21 views18 pages

Itpc 101 - Chapter II

Uploaded by

Aizen Dz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views18 pages

Itpc 101 - Chapter II

Uploaded by

Aizen Dz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

ITPC 101 - Database Management System

Chapter II: Relational Database


The relational model in DBMS is an abstract model used to organize and manage the
data stored in a database. It stores data in two-dimensional inter-related tables, also known as
relations in which each row represents an entity and each column represents the properties of
the entity.

II.1 What is the Relational Model?


The relational model for database management is an approach to logically represent
and manage the data stored in a database. In this model, the data is organized into a collection
of two-dimensional inter-related tables, also known as relations. Each relation is a collection
of columns and rows, where the column represents the attributes of an entity and the rows (or
tuples) represents the records.

Let's look at a scenario to understand the relational model:

Consider a case where you wish to store the name, the CGPA attained, and the roll number of
all the students of a particular class.

NOTE: A database implemented and organized in terms of the relational model is known as
a relational database management system (RDBMS). Hence, the relational model describes
how data is stored in relational databases.

II.1.1 Relational Model Concepts


As discussed earlier, a relational database is based on the relational model. This database
consists of various components based on the relational model. These include:

Chapter II: Relational Database Model


Lesson by: Rodlen A. Basilio
DO NOT REPRODUCE TO OTHERS
ITPC 101 - Database Management System
 Relation : Two-dimensional table used to store a collection of data elements.

 Tuple : Row of the relation, depicting a real-world entity.

 Attribute/Field : Column of the relation, depicting properties that define the relation.

 Attribute Domain : Set of pre-defined atomic values that an attribute can take i.e., it
describes the legal values that an attribute can take.

 Degree : It is the total number of attributes present in the relation.

 Cardinality : It specifies the number of entities involved in the relation i.e., it is the
total number of rows present in the relation.

 Relational Schema : It is the logical blueprint of the relation i.e., it describes the
design and the structure of the relation. It contains the table name, its attributes, and
their types:

TABLE_NAME(ATTRIBUTE_1 TYPE_1, ATTRIBUTE_2 TYPE_2, ...)

For our Student relation example, the relational schema will be:

STUDENT(ROLL_NUMBER INTEGER, NAME VARCHAR(20), CGPA FLOAT)

 Relational Instance : It is the collection of records present in the relation at a given


time.

 Relation Key : It is an attribute or a group of attributes that can be used to uniquely


identify an entity in a table or to determine the relationship between two tables.
Relation keys can be of 6 different types:

1. Candidate Key

2. Super Key

3. Composite Key

4. Primary Key

5. Alternate Key

6. Foreign Key

II.2 Relational Algebra in DBMS


Relational algebra in DBMS is a procedural query language. Queries in relational
algebra are performed using operators. Relational Algebra is the fundamental block for
modern language SQL and modern Database Management Systems such as Oracle Database,
Mircosoft SQL Server, IBM Db2, etc.

Chapter II: Relational Database Model


Lesson by: Rodlen A. Basilio
DO NOT REPRODUCE TO OTHERS
ITPC 101 - Database Management System
Relational Algebra came in 1970 and was given by Edgar F. Codd (Father of DBMS).
It is also known as Procedural Query Language(PQL) as in PQL, a programmer/user has to
mention two things, "What to Do" and "How to Do".

II.2.1 Types of Relational Operations


In Relation Algebra, we have two types of Operations.

 Basic Operations

 Derived Operations

Applying these operations over relations/tables will give us new relation as output.

II.2.2 Basic Operations

Six fundamental operations are mentioned below. The majority of data retrieval operations
are carried out by these. Let's know them one by one.

But, before moving in detail, let's have two tables or we can say
relations STUDENT(ROLL, NAME, AGE) and EMPLOYEE(EMPLOYEE_NO,
NAME, AGE) which will be used in the below examples.

Chapter II: Relational Database Model


Lesson by: Rodlen A. Basilio
DO NOT REPRODUCE TO OTHERS
ITPC 101 - Database Management System
 Select (σ)

Select operation is done by Selection Operator which is represented by "sigma"(σ). It is used


to retrieve tuples(rows) from the table where the given condition is satisfied. It is a unary
operator means it require only one operand.

Notation : σ p(R)
Where σ is used to represent SELECTION
R is used to represent RELATION
p is the logic formula

Let's understand this with an example:


Suppose we want the row(s) from STUDENT Relation where "AGE" is 20

σ AGE=20 (STUDENT)

 Project (∏)

Project operation is done by Projection Operator which is represented by "pi"(∏). It is used to


retrieve certain attributes(columns) from the table. It is also known as vertical partitioning as
it separates the table vertically. It is also a unary operator.

Notation : ∏ a(r)
Where ∏ is used to represent PROJECTION
r is used to represent RELATION
a is the attribute list

Let's understand this with an example:


Suppose we want the names of all students from STUDENT Relation.

∏ NAME(STUDENT)

Chapter II: Relational Database Model


Lesson by: Rodlen A. Basilio
DO NOT REPRODUCE TO OTHERS
ITPC 101 - Database Management System

As you can see from the above output it eliminates duplicates.


For multiple attributes, we can separate them using a ",".

∏ ROLL,NAME(STUDENT)

 Union (∪)

Union operation is done by Union Operator which is represented by "union"(∪). It is the


same as the union operator from set theory, i.e., it selects all tuples from both relations but
with the exception that for the union of two relations/tables both relations must have the
same set of Attributes. It is a binary operator as it requires two operands.

Notation: R ∪ S
Where R is the first relation
S is the second relation

If relations don't have the same set of attributes, then the union of such relations will result
in NULL. Let's have an example to clear the concept:
Suppose we want all the names from STUDENT and EMPLOYEE relation.

∏ NAME(STUDENT) ∪ ∏ NAME(EMPLOYEE)

Chapter II: Relational Database Model


Lesson by: Rodlen A. Basilio
DO NOT REPRODUCE TO OTHERS
ITPC 101 - Database Management System

 Set Difference (-)

Set Difference as its name indicates is the difference of two relations (R-S). It is denoted by a
"Hyphen"(-) and it returns all the tuples(rows) which are in relation R but not in relation S. It
is also a binary operator.

Notation : R - S
Where R is the first relation
S is the second relation

Just like union, the set difference also comes with the exception of the same set of attributes
in both relations.

Let's take an example where we would like to know the names of students who are in
STUDENT Relation but not in EMPLOYEE Relation.

∏ NAME(STUDENT) - ∏ NAME(EMPLOYEE)

Chapter II: Relational Database Model


Lesson by: Rodlen A. Basilio
DO NOT REPRODUCE TO OTHERS
ITPC 101 - Database Management System
 Cartesian product (X)

Cartesian product is denoted by the "X" symbol. Let's say we have two relations R and S.
Cartesian product will combine every tuple(row) from R with all the tuples from S. I know it
sounds complicated, but once we look at an example, you'll see what I mean.
Notation: R X S
Where R is the first relation
S is the second relation
As we can see from the notation it is also a binary operator. Let's combine the two relations
STUDENT and EMPLOYEE.

STUDENT X EMPLOYEE

. . . And so on.

 Rename (ρ)

Rename operation is denoted by "Rho"(ρ). As its name suggests it is used to rename the
output relation. Rename operator too is a binary operator.

Notation: ρ(R,S) Where R is the new relation name


S is the old relation name

Let's have an example to clear this


Suppose we are fetching the names of students from STUDENT relation. We would like to
rename this relation as STUDENT_NAME.

ρ(STUDENT_NAME,∏ NAME(STUDENT))

Chapter II: Relational Database Model


Lesson by: Rodlen A. Basilio
DO NOT REPRODUCE TO OTHERS
ITPC 101 - Database Management System
II.2.3 Derived Operations
Also known as extended operations, these operations can be derived from basic operations
hence named Derived Operations. These include three operations: Join Operations,
Intersection operation, and Division operation.

 Join Operations

Join Operations are binary operations that allow us to combine two or more relations.
They are further classified into two types: Inner Join, and Outer Join.

First, let's have two relations EMPLOYEE consisting


of E_NO, E_NAME, CITY and EXPERIENCE. EMPLOYEE table contains employee's
information such as id, name, city, and experience of employee(In Years).

The other relation is DEPARTMENT consisting


of D_NO, D_NAME, E_NO and MIN_EXPERIENCE. DEPARTMENT table defines the
mapping of an employee to its department. It contains Department Number, Department
Name, Employee Id of the employee working in that department and, minimum experience
required (In Years) to be in that department.

Also, let's have the Cartesian Product of the above two relations. It will be much easier to
understand Join Operations when we have the Cartesian Product.

Chapter II: Relational Database Model


Lesson by: Rodlen A. Basilio
DO NOT REPRODUCE TO OTHERS
ITPC 101 - Database Management System
 Inner Join

When we perform Inner Join, only those tuples are returned which satisfies the certain
condition. It is also classified into three types: Theta Join, Equi Join and Natural Join.

 Theta Join (θ)

Theta Join combines two relations using a condition. This condition is represented by the
symbol "theta"(θ). Here conditions can be inequality conditions such as >,<,>=,<=, etc.

Notation : R ⋈θ S
Where R is the first relation
S is the second relation
Let's have a simple example to understand this.

Suppose we want a relation where EXPERIENCE from EMPLOYEE >=


MIN_EXPERIENCE from DEPARTMENT.

EMPLOYEE⋈θ
EMPLOYEE.EXPERIENCE>=DEPARTMENT.MIN_EXPERIENCE
DEPARTMENT

 Equi Join

Equi Join is a special case of theta join where the condition can only
contain **equality(=)** comparisons.
A non-equijoin is the inverse of an equi join, which occurs when you join on a condition
other than "=".
Let's have an example where we would like to join EMPLOYEE and DEPARTMENT
relation where E_NO from EMPLOYEE = E_NO from DEPARTMENT.

EMPLOYEE ⋈EMPLOYEE.E_NO = DEPARTMENT.E_NO DEPARTMENT

Chapter II: Relational Database Model


Lesson by: Rodlen A. Basilio
DO NOT REPRODUCE TO OTHERS
ITPC 101 - Database Management System

 Natural Join (⋈)

A comparison operator is not used in a natural join. It does not concatenate like a
Cartesian product. A Natural Join can be performed only if two relations share at least one
common attribute. Furthermore, the attributes must share the same name and domain.
Natural join operates on matching attributes where the values of the attributes in both
relations are the same and remove the duplicate ones.
Preferably Natural Join is performed on the foreign key.

Notation : R ⋈ S
Where R is the first relation
S is the second relation

Let's say we want to join EMPLOYEE and DEPARTMENT relation with E_NO as common
attribute.
Notice, here E_NO has same name in both the relations and also consists of same domain,
i.e., in both relations E_NO is a string.

EMPLOYEE ⋈ DEPARTMENT

Outer Join

Unlike Inner Join which includes the tuple that satisfies the given condition, Outer Join also
includes some/all the tuples which doesn't satisfies the given condition. It is also of three
types: Left Outer Join, Right Outer Join, and Full Outer Join.
Let's say we have two relations R and S, then
Below is the representation of Left, Right, and Full Outer Joins.

Chapter II: Relational Database Model


Lesson by: Rodlen A. Basilio
DO NOT REPRODUCE TO OTHERS
ITPC 101 - Database Management System

 Left Outer Join

As we can see from the diagram, Left Outer Join returns the matching tuples(tuples present in
both relations) and the tuples which are only present in Left Relation, here R.
However, if the matching tuples are NULL, then attributes/columns of Right Relation, here S
are made NULL in the output relation.

Let's understand this a bit more using an example:

EMPLOYEE ⟕EMPLOYEE.E_NO = DEPARTMENT.E_NO DEPARTMENT

 Right Outer Join

Right Outer Join returns the matching tuples and the tuples which are only present in
Right Relation here S.
The same happens with the Right Outer Join, if the matching tuples are NULL, then the
attributes of Left Relation, here R are made NULL in output relation.

We will combine EMPLOYEE and DEPARTMENT relation with same constraint as above.

EMPLOYEE ⟖EMPLOYEE.E_NO = DEPARTMENT.E_NO DEPARTMENT

Chapter II: Relational Database Model


Lesson by: Rodlen A. Basilio
DO NOT REPRODUCE TO OTHERS
ITPC 101 - Database Management System

 Full Outer Join

Full Outer Join returns all the tuples from both relations. However if there are no
matching tuples then, their respective attributes are made NULL in output relation.

Again, combine the EMPLOYEE and DEPARTMENT relation with same constraint.

EMPLOYEE ⟗EMPLOYEE.E_NO = DEPARTMENT.E_NO DEPARTMENT

 Intersection (∩)

Intersection operation is done by Intersection Operator which is represented


by "intersection"(∩).It is the same as the intersection operator from set theory, i.e., it selects
all the tuples which are present in both relations. It is a binary operator as it requires two
operands. Also, it eliminate duplicates.

Notation : R ∩ S
Where R is the first relation
S is the second relation

Let's have an example to clear the concept:


Suppose we want the names which are present in STUDENT as well as in EMPLOYEE
relation, Relations we used in Basic Operations.

∏ NAME(STUDENT) ∩ ∏ NAME(EMPLOYEE)

Chapter II: Relational Database Model


Lesson by: Rodlen A. Basilio
DO NOT REPRODUCE TO OTHERS
ITPC 101 - Database Management System

 Division (÷)

Division Operation is represented by "division"(÷ or /) operator and is used in queries which


involve keyword "every", "all", etc.

Notation : R(X,Y)/S(Y)
Here,
R is the first relation from which data is to retrieved.
S is second relation which will help to retrieve the data.
X and Y are the attributes/columns present in relation. We can have multiple
attributes in relation, but keep in mind that attributes of S must be proper subset of
attributes of R.
For each corresponding value of Y, above notation will return us the value of X from
tuple<X,Y> which exist everywhere.

It's a bit difficult to understand this in theoretical way, but you will understand this with an
example.

Let's have two relations, ENROLLED and COURSE. ENROLLED consist of two attributes
STUDENT_ID and COURSE_ID. It denotes the map of students who are enrolled in given
courses.
COURSE contains the list of courses available.

Chapter II: Relational Database Model


Lesson by: Rodlen A. Basilio
DO NOT REPRODUCE TO OTHERS
ITPC 101 - Database Management System

Now the query is to return the STUDENT_ID of students who are enrolled in every course.

ENROLLED(STUDENT_ID, COURSE_ID)/COURSE(COURSE_ID)

II.3 ER Model to Relational Model


II.3.1 What is ER Model?
An ER model stands for the Entity-Relationship model that Peter Chen developed in
1976. This model consists of a collection of entities (Real word objects) and their
relationships. It describes the database's conceptual view. We must ensure that no two entities
are identical in this context.

ER Model describes the system's logical view from a data perspective formed by the
entity set, relationship set, and attributes. In this model, all entities come under the entity set,
all relations between the entities come under the relationship set, and attributes describe the
properties of entities.

An entity is a real-world object with some attributes.

Chapter II: Relational Database Model


Lesson by: Rodlen A. Basilio
DO NOT REPRODUCE TO OTHERS
ITPC 101 - Database Management System

II.4 Relationship within the relational database


Database relationships are associations between tables that are created using join statements
to retrieve data.

One-to-one

 Both tables can have only one record on each side of the relationship.
 Each primary key value relates to none or only one record in the related table.
 Most one-to-one relationships are forced by business rules and do not flow naturally
from the data. Without such a rule, you can typically combine both tables without
breaking any normalization rules.

Examples:

One-to-many

 The primary key table contains only one record that relates to none, one, or many
records in the related table.

Example:

Chapter II: Relational Database Model


Lesson by: Rodlen A. Basilio
DO NOT REPRODUCE TO OTHERS
ITPC 101 - Database Management System

Many-to-many

 Each record in both tables can relate to none or any number of records in the other
table. These relationships require a third table, called an associate or linking table,
because relational systems cannot directly accommodate the relationship.

Example:

II.5 – Codd’s Relationship Database rule


Edgar F. Codd, the creator of the relational model proposed 13 rules known as Codd
Rules that states:

For a database to be considered as a perfect relational database, it must follow the following
rules:

1. Foundation Rule - The database must be able to manage data in relational form.

Chapter II: Relational Database Model


Lesson by: Rodlen A. Basilio
DO NOT REPRODUCE TO OTHERS
ITPC 101 - Database Management System
2. Information Rule - All data stored in the database must exist as a value of some table
cell.

3. Guaranteed Access Rule - Every unique data element should be accessible by only a
combination of the table name, primary key value, and the column name.

4. Systematic Treatment of NULL values - Database must support NULL values.

5. Active Online Catalog - The organization of the database must exist in an online
catalog that can be queried by authorized users.

6. Comprehensive Data Sub-Language Rule - Database must support at least one


language that supports: data definition, view definition, data manipulation, integrity
constraints, authorization, and transaction boundaries.

7. View Updating Rule - All views should be theoretically and practically updatable by
the system.

8. Relational Level Operation Rule - The database must support high-level insertion,
updation, and deletion operations.

9. Physical Data Independence Rule - Data stored in the database must be independent
of the applications that can access it i.e., the data stored in the database must not
depend on any other data or an application.

10. Logical Data Independence Rule - Any change in the logical representation of the
data (structure of the tables) must not affect the user's view.

11. Integrity independence - Changing the integrity constraints at the database level
should not reflect any change at the application level.

12. Distribution independence - The database must work properly even if the data is
stored in multiple locations or is being used by multiple end-users.

13. Non-subversion Rule - Accessing the data by low-level relational language should
not be able to bypass the integrity rules and constraints expressed in the high-level
relational language.

Chapter II: Relational Database Model


Lesson by: Rodlen A. Basilio
DO NOT REPRODUCE TO OTHERS
ITPC 101 - Database Management System

Reference:
https://www.scaler.com/topics/dbms/relational-model-in-dbms/

https://www.ibm.com/docs/en/control-desk/7.6.0?topic=design-relational-database-structure

https://www.smartsheet.com/database-
relationships#:~:text=Relationships%20are%20the%20cornerstone%20of,titles%2C%20and%20anot
her%20for%20artists.

Chapter II: Relational Database Model


Lesson by: Rodlen A. Basilio
DO NOT REPRODUCE TO OTHERS

You might also like