0% found this document useful (0 votes)
53 views

Unit 4: Relational Model:: " Database Management"

Uploaded by

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

Unit 4: Relational Model:: " Database Management"

Uploaded by

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

“Database Management”

Unit 4: Relational Model:


Relational Model Concept:

Students: Course:

Name Std. ID Class Major Course Course Credit Departments


Subject Name Number Hours
05 11 Science Computer CS 310 3 CS
Laxman Science
Hari 07 12 Management Data base DB 315 3 CS
Maths MAT 305 3 Math
Sam 12 11 English Nepali NEP 318 3 Nepali

Section: Grade Report:

Section Course Semester Year Instructor


ID Number
75 MAT Fall 99 J. Gupta
305
77 DB 315 Spring 98 P. Gurung
Symbol Std ID Section Grade
80 NEP Spring 97 B. Twari No. ID
318 30115 5 75 B+
82 CS 310 Fall 99 K. Bista
30116 7 77 A

30117 12 80 C

Fig: A Database that stores Students and Course Information.

The relational model represents the database as a collection of relations. Informally, each
relation resembles a table of values or to some extent, a file of records. For example the
database of files that is shown above is similar to the relational model representation.

When a relation is thought of as a table of values, each row in the table represent a
collection of related data values. We introduced entity types and relationship types as
concept for modeling real world data. In the relational model, each row in the table
represents a fact that typically corresponds to a real world entity or relationship. The table
names and column names are used to help in interpreting the meaning of the values in

1
“Database Management”

each row. For example, the first table of above figure is called STUDENT because each
row represents facts about a particular student entity. The column names:-Names, Std ID,
Class and Major Subject specify how to interpret the data values in each row based on the
column each value is in. All values in a column are the same data type.
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.

Fundamental Concepts on Relational Data Model

Relations:-

The relational data model organizes and represents data in the form table or relations.
Relation is terms that comes from Mathematics and represent a simple 2- dimensional
table, consisting of rows and columns of data.

WORKER:
Worker ID Name Hourly- Rate Skill-Type SUPV- ID
123 M.Bista 12 Electric 131
141 K. Khanal 11 Plumbing 152
292 S.Prajapati 10 Roofing -
323 S.poudel 11 Driving -
152 P.Pandey 15 Teaching -

Fig: A portion of the relation worker.

This above figure shows a relation with sample data values, which represents the
WORKER object set, and its attributes. Each column in the relation is an attribute of the
relation. The name of the column is called the attribute name. We use the terms attribute
and attribute name rather than column name.

The number of attributes in a relation is called the degree of the relation. The degree of
WORKER is Five.

The rows of relation are also called tuples. It is assumed that there is no prescribed order
to the rows or tuples if a relation and that no tuples have identical set of values. The set of
all possible values that an attribute may have is the domain of the attribute.

Null Values:-

A null value means the value given an attribute in a tuple if the attribute is inapplicable or
its value is unknown. For example, some employees in the WORKER relation do not
have supervisors. Consequently no values exist for SUPV-ID for Three employees. In
addition, when we are entering data for a row in relation, we might not know the values
2
“Database Management”

of one or more attributes for that row. In either case, we enter nothing, and that row is
recorded in the database with Null values for those attributes. A null value is not blank or
zero. It is simply unknown or inapplicable and may be supplied at a later time.

Key:-

In fact, Key is a minimal set of attributes that uniquely identifies each row in a relation.
In the above figure, let us assume that the WORKER –ID attribute uniquely identifies a
row in WORKER, and we say that WORKER-ID is a key in the WORKER relation.

Any set of attributes that uniquely identifies each tuple in relation is called a super key. A
key of a relation is a minimal set of such attributes. That is, a key is a minimal super key.
By minimal, we mean that no subset of the set of key attributes will uniquely identify
tuples in a relation.

WORKER-ID BLDG-ID START DATE NO.-DAYS


123 312 10/10 5
141 312 05/10 10
123 322 12/08 5
141 315 12/12 12
Fig:- ASSIGNMENT Relation

In the ASSIGNMENT relation, the key consist of the WORKER-ID and the BLDG-ID
attributes. Neither WORKER-ID alone nor BL DG-ID alone uniquely identifies every
row, but the two attributes together do provide that unique identification required for a
key. A key consisting of more than one attribute is called a Composite Key.

In any given relation, there may be more than one set of attributes that could be chosen as
a key, these are called Candidate Keys. Means, Candidate Key is defined as “any set of
attributes that could be chosen as a key of a relation. For example WORKER-ID is a
candidate key in WORKER relation if it will always be unique. When one of the
candidate key is selected as the relation key, it may be called the Primary Key. The
candidate key that is the easiest to use in day-to-day data entry is normally selected as the
primary key.

A Foreign Key is a set of attributes in one relation that constitutes a key in some other or
possibly the same relation that are used to indicate logical links between relations.
WORKER-ID in the WORKER relation is the example of foreign keys since WORKER-
ID is the key of the ASSIGNMENT relation.

RELATIONAL ALGEBRA

3
“Database Management”

A Query language is a language in which a user request information from the database.
The relation algebra is a procedural query language (in procedural language, the user
instructs the system, to perform a sequence of operations on the database to compute the
desired result). It consists of a set of operations that take one or two relations as input and
produce a new relation as their results. The fundamental operations in the relation algebra
are select, project, union, set difference, Cartesian product and rename. In addition,
to the fundamental operations, there are several others operations-namely, set-
intersection, natural join, division, and assignment.

Fundamental Operations:-

In case of fundamental operations, the select, Project and rename operations are called
unary operations because they operate on one relation and the other three operations, i.e.
union, set difference and Cartesian product operate on pairs of relations and are
therefore called binary operations. Let us describe all the fundamental operations in
brief.

1. The Select operation:-


The select operation selects tuples that satisfy a given predicate. We use the letter
sigma () to denote selection. The predicate appears as a subscript to . The
argument relation is in parentheses after the . The general format of the select
operation is: -  <selection condition>®

Loan- Branch-Name Amount


Number
11 Round Hill 900
14 Down Town 1500
15 Perryridge 1500
16 Perryridge 1300
17 Down Town 1000
23 Red Wood 2000
93 Milanus 500
Figure: Loan Relation

Now in order to select those types of the loan relation where the branch name is
“Perryridge” we write

() Branch Name = “Perryridge” (Loan)

4
“Database Management”

Here the result of this predicate from the Loan relation is shown below:

Loan-Number Branch-Name Amount

15 Perryridge 1500

16 Perryridge 1300

Figure: - Result of () Branch Name = “Perryridge” (Loan)

2. The Project Operation:-


The Project operation is unary operation that returns its argument relation with certain
attributes left out. Since a relation is a set, any duplicate rows are eliminated.
Projection is denoted by the Greek letter pi (π). We list those attributes that we wish
to appear in the result as a subscript to pi (π). The argument relation follows in
parentheses. The general format of the project operation is:- π <attribute Lists>®

Now in order to write the query to list all loan-numbers and amount of the Loan
Relation as:
π <Loan-number, Amount> (Loan)
And the result of these query is given below:

Loan-Number Amount
11 900
14 1500
15 1500
16 1300
17 1000
23 2000
93 500
Figure: - Result of π<Loan-Number, Amount> (Loan)

5
“Database Management”

3. The Rename Operation:-


We can also define a formal Rename operation which can rename either the relation
name or the attributes names, or both in a manner similar to the way we defined select
and project operations. The general rename operation when applied to a relation R of
degree n is denoted by any of the following three forms.

S (B1, B2, B3,………..,Bn) ® or S ® or (B1, B2,………..,Bn) ®

Where the symbol  (rho) denotes the rename operation. S is the new relation name
and B1, B2, B3,………..Bn are the new attribute names. The first expression renames
both the relation and its attributes, the second expression renames the relation only
and the third expression renames the attributes only. If the attributes of R are (A1, A2,
A3,………..An) in that order, then each Ai is renamed as Bi.

4. The Union Operation:-


The result of this operation denoted by RS, is a relation that includes all tuples that
are either in R or in S relations or in both R and S. Duplicate tuples are eliminated.
Let us see an example.

Customer-Name Account-Number Customer-Name Loan-Number


Hayes 101 Adams 16
Jhonson 102 Curry 17
Jhonson 103 Hayes 18
Jones 104 Jones 19
Turner 105 Smith 20

Figure: The Depositor Relation Figure: The Borrower Relation.

Now by the use of Union Operation, we can find the name of all customers with a loan in
the bank and also with an account in the bank. Hence the expression is:

π customer-name (borrower)  π customer-name (depositor)

The result of this expression which is extracted based on the above two relations are
shown below:

6
“Database Management”

Customer Name
Adams
Curry
Hayes
Jhonson
Jones
Smith
Turner
Figure: Name of all customer s who have either a loan or an account.

5. The Set Difference (Minus) Operation:-


The set difference operation, denoted by R-S, allows us to find tuples that are in one
relation but are not in another. The expression R-S produces a relation containing
those tuples in R but not in S.

In case of example we can find all customers of the bank who an account but not a
loan by anything:

π Customer-Name (depositor) – π Customer-Name (borrower).

The result of this above expression is shown below.

Customer-Name

Jhonson

Turner

Figure: Customer with an Account but not Loan

6. The Cartesian Product Operation:


The Cartesian product operation which is also known as Cross Product or Cross
Join operation is denoted by (*), allows us to combine tuples from two relations in a
combinational fashion. We write the Cartesian product of relations R and S as R*S.

In general, the result of R(A1, A2, A3,………..An)*S(B1, B2, B3,………..Bm) is a


relation Q with degree n+m attributes Q(A1, A2, A3,………..An, B1, B2, B3, ………..,
Bm), in that order. The resulting relation Q has one tuple for each combination of
tuples- one from R and one from S.

7
“Database Management”

For example; Suppose that one want to find the name of all customers also have a
loan at the Perryridge branch we need the information in both the Loan relation and
Borrower relation to do so. If we write

 branch-name = “Perryridge” (borrower*loan)

Customer-Name Loan-Number Loan- Branch-Name Amount


Number
Adams 16
11 Round Hill 900
Curry 17 14 Down Town 1500
Hayes 18 15 Perryridge 1500
Jones 19 16 Perryridge 1300
Smith 20 17 Down Town 1000
Fig:- Borrower Relation 23 Red Wood 2000
93 Milanus 500

Fig:- Loan Relation

The result of the above query is shown in the following Table

Customer- Borrower Loan Branch-Name Amount


Name Loan- Loan-
Number Number
Adams 16 15 Perryridge 1500
Adams 16 16 Perryridge 1300
Curry 17 15 Perryridge 1500
Curry 17 16 Perryridge 1300
Hayes 18 15 Perryridge 1500
Hayes 18 16 Perryridge 1300
Vanes 19 15 Perryridge 1500
Vanes 19 16 Perryridge 1300
Smith 20 15 Perryridge 1500
Smith 20 16 Perryridge 1300

8
“Database Management”

Some Other Operations of Relational Algebra


.
1. The Set- Intersection Operation:-
This operation produces a relation that includes all the tuples in both R and S
relations and is denoted by ∩. For example; we wish to find all customers who
have both a loan and account. Using set intersection, we can write

 customer-name (borrower) ∩  customer-name (depositor).


The result of this query is:

Customer-Name
Hayes
Jones

2. The Natural Join Operation: -


The natural Join is a binary operation that allows us to produces all the
combination of tuples from R and S relations that satisfy a join condition with
only equality comparisons except that the join attributes of S relation are not
included in the resulting relation. It is denoted by the “Join”  symbol

9
“Database Management”

Let us consider an example to find the name of all customers who have a loan at
the bank, and find the amount of the loan. We express this query by using the
natural join as:
 customer-name, loan-number, amount (borrower Join loan)

Borrower Relation Loan Relation


Customer- Loan-
Name Number
Adams 16
Curry 17
Hayes 18
Vanes 19
Smith 20

Loan- Branch- Amount


Number Name
11 Roundhill 900
14 Downtown 1500
15 Perryridge 1500
16 Perryridge 1300
17 Downtown 1000
23 Redwood 2000
93 Milanus 500

The result of this above query is:

Customer- Loan-Number Amount


Name
Adams 16 1300
Curry 17 1000

3. The Division Operation:-


The division operation denoted by ÷ is useful for special kinds of query that
sometimes occurs in database applications. Formally, let R(X) and S(Y) be
relations and let YX, that is every attributes of Y is also in schema X. The
relations R÷S is a relation on schema X-Y (that is, on the schema containing all

10
“Database Management”

attributes of schema X that are not schema Y). A tuple t is in R÷S if and only if
both of conditions hold:

i. t is in π X-Y ®
ii. For every tuple ts in S, there is a tuple tR in R satisfying both of the
following:
 tR (Y) = ts (Y)
 tR (X-Y) = t

4. The Assignment Operation:-


It is convenient at times to write a relational algebra expression by assigning parts
of it to temporary relation variables. The assignment operation denoted by ←,
works like assignment in a programming language. For eg.

Temp1← R
Temp2← S
Result = Temp1- temp2.

11

You might also like