Relational Model B.Ramamurthy 6/28/2016 1

advertisement
Relational Model
B.Ramamurthy
6/28/2016
B.Ramamurthy
1
Introduction
Relational database theory assist in definition
of databases and in efficient processing of
databases and in efficient processing of user
requests for information from a database.
ER model defined the problem and leads us
to the tables. Now we need to define,
manipulate and use the tables.. Relational
model provides the foundation for this.
6/28/2016
B.Ramamurthy
2
Topics for Discussion
Structure of Relational database
Relational Algebra
Tuple Relational Calculus
Domain Relational Calculus
Extended Relational-Algebra-Operations
Modifications of the Database
Views
Summary
6/28/2016
B.Ramamurthy
3
Relational Database
A relational database consists of a
collection of tables, each of which is
assigned a unique name.
A table will be referred to as a relation
in Relational DB.
Tuple is the term for row in a table.
On to more theoretical stuff …
6/28/2016
B.Ramamurthy
4
Basic Structure
Given sets A1, A2, A3…An, a relation r is a subset of
A1 X A2 X A3 X ….X An
A relation is a set of n-tuples (a1,a2,a3,..an) where ai Є Ai.
Example : if
customer-name = {Jones, Smith, Curry, Lindsay}
customer-street = {Main, North, Park}
customer-city = {Harrison, Rye, Pittsfield}
Then r = {(Jones,Main,Harrison), (Smith,North,Rye),
{Curry,North,Rye), (Lindsay, Park, Pittsfield)} is a relation
over
customer-name X customer-street X customer-city
6/28/2016
B.Ramamurthy
5
Relation Schema
(This concept is analogous to class, instance
of a class in OOP)
A1, A2, A3.. An are attributes
R = (A1, A2, A3, …An) is a relation schema.
Example : Customer-schema = (customername, customer-street, customer-city)
r(R) is a relation on the relational schema R
Example : customer(Customer-schema)
6/28/2016
B.Ramamurthy
6
More Examples
Branch-scheme= (banch-name, branchcity,assets)
Relation is a table of specific branches
branch-name branch-city assets
Downtown
Brooklyn
90000000
Redwood
Palo Alto
89760000
North Town
Rye
37900000
6/28/2016
B.Ramamurthy
7
Relation Instance
Current values (relational instance) of a
relation are specified by a table.
An element t of r is a tuple: represented
by a row in the “relation” table
See the above example
6/28/2016
B.Ramamurthy
8
Query Language
Language in which user requests information
from a database.
Categories of languages: procedural, non
procedural
Pure Languages:
 Relational algebra
 Tuple relational calculus
 Domain relational calculus
Pure languages form the basis of query
languages.
6/28/2016
B.Ramamurthy
9
Relational Algebra
Procedural Language
Six fundamental operations
 Select
 Project
 Union
 Set difference
 Cartesian product
 Rename
The operators take two or more relations as inputs
and give a new relation as a result.
6/28/2016
B.Ramamurthy
10
Select Operation
Select operation selects tuples that satisfy a
given predicate.
Lowercase sigma is used to denote selection.
The predicate appears as subscript to p
The argument relation is given in parenthesis.
Therefor notation for Select operation is p (r)
6/28/2016
B.Ramamurthy
11
Select: Formal Definition
P ( r ) = {t | t Є r and P(t)}
where t is a tuple in r that satisfies P(t)
P(t) is a formula in propositional calculus,
dealing with terms of the form:
<attribute> op <attribute> or constant
op represents an operator in { =, <>, > , >=,
<=}
simple terms can be connected by and, or, not
6/28/2016
B.Ramamurthy
12
Select: Example
Relation r :
A




B C D
 1 7
7
 5
 12 3
 23 10
A
A= B and D > 5 (r )
6/28/2016


B.Ramamurthy
B


c
1
23
D
7
10
13
Project Operation
Suppose you are interested in retrieving
only a few attributes of a relation.
A “Project” operation can be used.
It is a unary operation.
Basically a vertical subset of a table is
returned.
Duplicate rows are removed from the
result.
6/28/2016
B.Ramamurthy
14
Project: Formal Definition
A1,A2,…Ak ( r )
where A1, A2, …Ak are attribute names
and r is a relation name.
Result has k columns, each column
representing an attribute.
6/28/2016
B.Ramamurthy
15
Project: Example
A




Relation r :
A,B ( r )
6/28/2016
A



B C D
 1 7
7
 5
 12 3
 23 10
B



B.Ramamurthy
16
Union Operation
Binary operation.
Notation : r U s
Definition: r U s = {t | t Є r or t Є s}
Constraints are:
1. r and s must have same arity (same number
of attributes)
2. The attribute domains must be compatible
(ex: 2 nd column of r is of String type and so
should 2nd column of s)
6/28/2016
B.Ramamurthy
17
Union: Example
r
rUs
6/28/2016
A
B



1
2
1
s
A
B




1
2
1
3
B.Ramamurthy
A
B


2
3
18
Other Important Operations
We discussed Join, Division, Cartesian
Product, Rename and Assignment operations.
For each of these we studied:
1. The symbol
2. Formal expression
3. An example in tabular form
4. A sample query that needs (or uses) this
operation.
6/28/2016
B.Ramamurthy
19
Tuple Relational Calculus
A nonprocedural query language, where each
query is of the form
{ t | P(t) }
It is the set of all tuples t such that predicate P
is true for t.
t is a tuple variable; t[ A] denotes the value of
tuple t on attribute A and t  r denotes that tuple t
is in relation r.
P is a formula similar to that of the predicate
calculus.
6/28/2016
B.Ramamurthy
20
Sample Queries
{t | t  loan  t[amount] > 1200}
Write the following in tuple relational
calculus as well as in relational algebra.
Loan numbers such that loan-amount >
1200
Names of all customers who have a
loan from the PerryRidge branch.
6/28/2016
B.Ramamurthy
21
Extended Relational Algebra
Operations
Generalized projection: Apply arithmetic (or
any) function to the regular projection.
 Example : Credit info of customers.
Outer Join: Extension of join that deals with
missing information. Lossless join.
We have left outer join and right outer join.
Aggregate functions: to take a collection of
values and return a single value. Example :
sum of a column.
6/28/2016
B.Ramamurthy
22
Modification of the database
Deletion, insertion, updating : all these major
operations are actual a collection of lower
level operations we have discussed.
Views: View definitions will also have only
part of the data stored in the table depending
on the request. Once again we apply the
project , join and other operations we
studied.
6/28/2016
B.Ramamurthy
23
Summary
A relational database is made up of
tables.
Each table is a relation.
Each row of a table is a relationship.
E-R diagram --> relation-schema-->
tables --> RDB
Relational operations are used to
extract data from the tables.
6/28/2016
B.Ramamurthy
24
Tugas Individu
1. Kunjungi http://w3.org/TR/query-algebra
2. Kunjungi http://www.mhhe.com/mannino
3. Membuat ringkasan (salah satu) hasil
kunjungan ke URL di atas.
6/28/2016
B.Ramamurthy
25
Download