Sie sind auf Seite 1von 55

Chapter 3 : Relational Model

Structure of Relational Databases


Fundamental Relational-Algebra Operations
Additional Relational-Algebra Operations
Extended Relational-Algebra Operations
Null Values
Modification of Database

Terminologies related To Relational Model


Relation Defined as a table with collection of rows
and columns. Data can be stored in the form of a 2D
table. Rows will be records and columns will be
attributes
Attribute Defined as a named column of a relation.
The entities are relations and their attributes are
columns of the relation
Domain Defined as a set of allowed values for one
or more attributes
Tuple It is a row of a relation. Tuple can appear in
any order and the relation will still remain the same
relation
Extension (state) of a relation Defined as the set of
tuples that appear in that relation at any given instant
of time. Extension is same as view of the table

Terminologies related To Relational Model


Intension of a relation A permanent part of the
relation, independent of time.
Degree of a relation Defined as the number of
attributes it contains. It is property of the intension of
a relation
Cardinality of a relation Defined as the number of
tuples it contains. It is the property of the extension
of the relation
Relational database Defined as the collection of
normalized / structured relations with distinct relation
names
N-ary relation A relation with degree N

Structure of Relational Databases


A relational database consist of collection
of Tables, each is assigned a unique name
A row in a table represents a relationship
among a set of values
A table is an ENTITY SET and a row is an
ENTITY
Since a table is a collection of such
relationships, there is a close
correspondence between the concept of
TABLE and the mathematical concept of
relation, from which the relational data
model takes its name

Basic Structure
Let us consider the account table. It has three
column headers,
account_number, branch_number, balance
These headers are attributes
For each, there is a set of permitted values,
called the DOMAIN of that attribute.
For attribute branch_name the domain is the set of all
branch names. At physical level the domain of branch
name is string of characters

Let,
D1 - set of all account no.s
D2 set of all branch_names
D3 - set of all balances

Basic Structure
Any row of Account table must consist of a 3tuples(v1, v2, v3)
v1 account no. (v1 is in D1)
v2 branch name (v2 is in D2)
v3 balance (v3 is in D3)
i.e. Account is a subset of
D1 x D2x D3

A Relation is a subset of Cartesian product of


list of domains

Basic Structure

A TUPLE variable is a variable whose domain is


the set of all tuples
i.e. tuple variable t[account_number] denotes
the value of t on account number attribute
t[account_number] = A-101 or
t[1] = A-101
t is in relation r
For all relations r, the domains of all attributes
of r must be atomic. A domain is Atomic if
elements of the domain are individual units
Null attributes should be eliminated if at all
possible

Database Schema

We must differentiate between


Database Schema
Database instance

A relation corresponds to the programming


language notation of a variable

A relational schema corresponds to the


programming language notation type definition

A relation instance corresponds to the


programming language notation of a value of a
variable
e.g. Account_schema(account_number,
branch_name, balance)
Account is a relation on Account_schema
account(Account_schema) i.e. r(R)

Relational Model Notation

A relation schema R of degree n is denoted by R(A 1, A2,


, An)

An n-tuple t in a relation r(R) is denoted by t =<v1, v2,


, vn> where vi is the value corresponding to attribute Ai
For component value of a tuple

T[Ai], t.Ai and t[i] refers to the value vi in t for attribute Ai

T[Au, Aw, , Az] and t.(Au, Aw, , Az) where Au, Aw, , Az is a
list of attributes from R, refers to the subtuple of values
<vu, vw, , vz) from t corresponding to the attributes
specified in the list

The letter Q, R, S denote relation names


The letter q, r, s denote relation states
The letter t, u, v denote tuples
Name of relation indicates the current set of tuples in
that relation whereas STUDENT(name, rollno,) refers
relation schema only

Keys
A super key of an entity set is a set of one or
more attributes whose values uniquely
determine each entity.
A candidate key of an entity set is a minimal
super key
Customer-id is candidate key of customer
account-number is candidate key of account

Although several candidate keys may exist,


one of the candidate keys is selected to be
the primary key.

Composite primary key - A primary


key which is a combination of more
than one attributes
Overlapping candidate keys - Two
candidate keys overlap if they
involve any attribute in common.
e.g., in an Rusult table, S#, C#
and Emailid, C# are two overlapping
candidate keys. (they have C# in
common)

Non-Key Attributes
The attributes other than the Candidate Key
attributes are called Non-Key attributes.
OR
The attributes which do not participate in
any of the Candidate keys
Foreign key
A foreign key is a copy of a primary key
that has been exported from one relation
into another to represent the existence of
a relationship between them. A foreign
key is a copy of the whole of its parent
primary key i.e. if the primary key is
composite, then so is the foreign key

Relational algebra

Relational algebra and relational


calculus are formal languages
associated with the relational model
Both operands and results are
relations, so output from one
operation can become input to
another operation
Allows expressions to be nested,
just as in arithmetic. This property
is called closure.

Relational Algebra

Five basic operations in relational


algebra are Selection, Projection,
Cartesian product, Union, and Set
Difference
These operations perform most of
the data retrieval operations
needed
Other operations Join, Intersection,
and Division operations, which can
be expressed in terms of 5 basic
operations

Selection (or Restriction)

Assignment Operation

Used to write relational-algebra


expression in parts using
assignment to a temporary relation
variable
Denoted by
Similar to assignment in
programming language

Modification of the Database

How to add, remove, or change


information in the database
We express database modification
using the assignment operation

Modification of the DB - Deletion

Deletion in relational-algebra is much same as


a query
Instead of displaying tuples to the user,
selected tuples are removed from the database
We can delete only whole tuples; we cannot
delete values on only particular attributes
Deletion is expressed by
r
rE
Where,
r relation
E relational-algebra query

Modification of the DB - Insertion

To insert data into a relation, we specify a tuple


to be inserted or write a query whose result is
a set of tuples to be inserted
The attribute values for inserted tuples must be
members of the attributes domain
Tuples inserted must be of correct arity
Insertion is expressed by
r
rUE
Where,
r relation
E relational-algebra expression

Modification of the DB - Updation

In some situations we wish to change a value


in a tuple without changing all values in the
tuple
Updation is expressed by
r
F1, F2, . . ., Fn(r)
Where,
Fi ith attribute of r, if the ith attribute is not
updated , or if the attribute is to be updated Fi
is an expression, involving only constants and
the attributes of r, which gives new value to r
To update selected tuples from r
r
F1, F2, . . ., Fn(p (r)) U (r - p (r))

Extended Relational-algebra
operations

Generalized projection
Outer join
Left
Right
Full

Aggregate Functions

Generalized Projection

It extends the projection operation


by allowing arithmetic functions to
be used in the projection list
Denoted by
F1, F2, . . ., Fn(E)
Where,
E relational-algebra expression
F1, F2, . . ., Fn arithmetic expressions
involving constants and attributes in the
schema of E

Rename Operation

Used to give a name (alias) to relationalalgebra expressions or relation


Denoted by Greek letter rho

X (E)

E relational algebra expression


X name given to relational-algebra query
expression
How to Read it
Result of relational-algebra query expression E
under the name X

Rename Operation

Assume relational algebra expression E


has n-arity then
X(A1, A2, . . . , An)(E)

How to Read it
Result of relational-algebra query
expression E under the name X, and with
the attributes renamed to A1, A2, . . . , An

Aggregate Functions

It take collection of values and return a


single value as a result
List of aggregate functions
Sum returns the sum of values
Avg returns the average of values
Count returns no. of elements in the collection
Min returns the minimum value in the collection
Max - returns the maximum value in the collection
Note - To eliminate duplicates, same function
names with the addition of the hyphenated
string distinct

Aggregate Functions

To find out the total sum of salaries of all


employees

sum

salary

(employee)

Find the number of departments in the


employee relation

count-distinct

departmentno

(employee)

Aggregate Functions on Groups

In some circumstances we would like to apply


aggregate function not only to the single set of
tuples but to several groups, where each group
is set of tuples
Can be done by using an operation called
Grouping
Denoted by aggregation operator
G1, G2, . . ., Gn F1 A1, F2 A2, . . ., Fm Am ( E )
Where,
E relational-algebra expression
G1, G2, , Gn list of attributes on which to
group
Fi an aggregate function
Ai an attribute name

Aggregate Functions on Groups

The tuples in result of expression E are


partitioned into groups such that
All tuples in a group have same values for
G1, G2, . . . , Gn
All tuples in different groups have
different values for G1, G2, . . . , Gn
For each group (g1, g2, . . ., gn) the result
has a tuple (g1, g2, . . ., gn, a1, a2, . . .,
an)
Where for each i, ai is the result of applying
aggregate function Fi on the multiset of
values for attribute Ai in the group

View Definition

A view is defined using the create


view statement. To define a view, we
must give the view name, and must
state the query that computes the
view. The create view statement is
CREATE VIEW v as <query-expression>
Where
query-expression is any relational-algebra
query expression

View Definition

A clerk who needs to see all loan data


in the loan relation except loan-amount
CREATE VIEW branch-loan as
branch_name, loan_number(loan)
Insertion using view in loan relation
branch-loan branch-loan U {(new panvel, L-37)}

Views Defined Using Other


Views

CREATE VIEW branch-loanno as


loan_number(branch-loan)

Das könnte Ihnen auch gefallen