Unit 4: Relational Model:: " Database Management"
Unit 4: Relational Model:: " Database Management"
Students: Course:
30117 12 80 C
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.
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 -
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.
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.
Now in order to select those types of the loan relation where the branch name is
“Perryridge” we write
4
“Database Management”
Here the result of this predicate from the Loan relation is shown below:
15 Perryridge 1500
16 Perryridge 1300
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”
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.
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:
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.
In case of example we can find all customers of the bank who an account but not a
loan by anything:
Customer-Name
Jhonson
Turner
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
8
“Database Management”
Customer-Name
Hayes
Jones
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)
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
Temp1← R
Temp2← S
Result = Temp1- temp2.
11