Anda di halaman 1dari 12

Relational Model

What are query languages?


Relational Model consists of the elements: relations,
Query languages are computer languages used to make which are made up of attributes.
queries into databases A relation is a set of attributes with values for each
One wants to avoid the use of general programming attribute such that:
languages for querying databases for various reasons: Each attribute value must be a single value only (atomic).
they usually require more eort, they are error-prone, and All values for a given attribute must be of the same type.
they are not conducive to query optimization. Each attribute name must be unique.
A query language allows users to formulate their queries in a The order of attributes is insignificant
simple and intuitive way, without having any special No two rows (tuples) in a relation can be identical.
prociency in the technicalities of the database besides The order of the rows (tuples) is insignificant.
knowledge of the (relevant part of the) database schema.
In particular, the user should not need to specify how the query is processed
but only which properties the result should have.

1 2

Relational Algebra Relational Algebra


Relational Algebra is a collection of operations on Relational Algebra is :
Relations. the formal description of how a relational database
Relations are operands and the result of an operation operates
is another relation. an interface to the data stored in the database itself

It is thus possible to apply an operation to the result of the mathematics which underpin SQL operations
another operation The DBMS must take whatever SQL statements the
Two main collections of relational operators: user types in and translate them into relational algebra
operations before applying them to the database.
Set theory operations:
Union, Intersection, Difference and Cartesian product.

Specific Relational Operations:


Selection, Projection, Join, Division

3 4

Select Operation Example Select Operation


Notation: p(r)
Relation r
p is called the selection predicate
A B C D

1 7 Defined as:
5 7
p(r) = {t | t r and p(t)}
12 3
Where p is a formula consisting of terms connected by :
23 10 (and), (or), (not)
Each term is one of:
<attribute> op <attribute> or <constant>
A=B ^ D > 5 (r)
A B C D where op is one of: =, , >, . <.

Example of selection:
branch-name=Perryridge(account)
1 7
23 10

5 6

1
Project Operation Example Project Operation
Relation r: A B C
Notation:
10 1
20 1 A1, A2, , Ak (r)
30 1 where A1, A2 are attribute names and r is a relation name.
40 2 The result is defined as the relation of k columns obtained
A,C (r) A C A C
by erasing the columns that are not listed
Duplicate rows removed from result, since relations are sets
1 1
E.g. To eliminate the branch-name attribute of account

account-number, balance (account)
1 = 1
1 2
2

7 8

Union Operation Example UNION Example


Relations r, s:
A B A B

1 2
2 3
1 s
r

r s: A B

1
2
1
3

9 10

Union Operation Set Difference Operation Example


Relations r, s:
A B A B

Notation: r s 1 2
Defined as: 2 3
r s = {t | t r or t s} 1 s
r
For r s to be valid.
1. r, s must have the same arity (same number of attributes)
2. The attribute domains must be compatible (e.g., 2nd column r s: A B
of r deals with the same type of values as does the 2nd
column of s) 1
E.g. to find all customers with either an account or a loan 1
customer-name (depositor) customer-name (borrower)

11 12

2
DIFFERENCE Example
Set Difference Operation
Notation r s
Defined as:
r s = {t | t r and t s}
Set differences must be taken between compatible
relations.
r and s must have the same arity
attribute domains of r and s must be compatible

13 14

Cartesian-Product Operation-Example
Cartesian-Product Operation
Relations r, s: A B C D E

1 10 a Notation r x s
2


10
20
a
b
Defined as:
r 10 b r x s = {t q | t r and q s}
s
r x s:
A B C D E
1 10 a
1 10 a
1 20 b
1 10 b
2 10 a
2 10 a
2 20 b
2 10 b

15 16

CARTESIAN PRODUCT example


CARTESIAN PRODUCT example
The Cartesian Product
is also an operator
which works on two
sets. It is sometimes
called the CROSS
PRODUCT or CROSS
JOIN.
It combines the tuples
of one relation with all
the tuples of the other
relation.

17 18

3
For each student, identified by name and
student number, return the name of the tutor
and their office number
we have to combine tuples from Student and
Staff that satisfy Student.tutor=Staff.lecturer
and keep the attributes studno, name, tutor and
lecturer.
In relational algebra:
studno,name,lecturer,roomno(tutor=lecturer (Student Staff ) )

19 20

Composition of Operations
Can build expressions using multiple operations
Example: A=C(r x s)
A B C D E
rxs 1 10 a
1 10 a
1 20 b
1 10 b
2 10 a
2 10 a
2 20 b
2 10 b

A B C D E

A=C(r x s)

1 10
20
a
2 a
2 20 b
21 22

Additional Operations Set-Intersection Operation


We define additional operations that do not add any power to the
relational algebra, but that simplify common queries. Notation: r s
Defined as:
Set intersection
r s ={ t | t r and t s }
Natural join
Assume:
Division
r, s have the same arity
Assignment
attributes of r and s are compatible

23 24

4
INTERSECTION Example
Set-Intersection Operation - Example
A B A B
1 2
Relation r, s: 2 3
1

r s

A B

rs

25 26

Natural-Join Operation Natural Join is a Derived Operation


Notation: r s
Let r and s be relations on schemas R and S respectively. The natural join of R and S can be written using
Then, r s is a relation on schema R S obtained as follows: Cartesian Product
Consider each pair of tuples tr from r and ts from s. Selection
If tr and ts have the same value on each of the attributes in R S, Projection
add a tuple t to the result, where
t has the same value as tr on r
t has the same value as ts on s
Example:
R = (A, B, C, D)
The tuples in the resulting relation are obtained by combining tuples in the
S = (E, B, D) operands with equal values on the common attributes
Result schema = (A, B, C, D, E)
r s is defined as:
r.A, r.B, r.C, r.D, s.E (r.B = s.B r.D = s.D (r x s))
27 28

Natural Join Operation Example Natural Join LastName DepartmentID

A `natural join' will remove


Rafferty 31

Relations r, s: the duplicate attribute(s).


Employee Table Jones 33
Steinberg 33

B D E
Robinson 34

A B C D Smith 34

1 a 1 a
2 a 3 a
4 b 1 a Department Table


DepartmentID DepartmentName

1 a 2 b 31 Sales

2 b 3 b 33 Engineering

r s
34 Clerical

35 Marketing

r s
A B C D E DepartmentID Employee.LastName Department.DepartmentName

1 a
34 Smith Clerical


33 Jones Engineering
1 a

34 Robinson Clerical

1 a 33 Steinberg Engineering

1 a 31 Rafferty Sales

2 b
29 30

5
31 32

Division Operation
rs
Suited to queries that include the phrase for all.
Let r and s be relations on schemas R and S
respectively where
R = (A1, , Am, B1, , Bn)
S = (B1, , Bn)
The result of r s is a relation on schema
R S = (A1, , Am)

rs={t | t R-S(r) u s ( tu r ) }

33 34

Division Operation Example Another Division Example


Relations r, s: A B B Relations r, s:
A B C D E D E
1 1 a a 1 a 1
2 a a 1 b 1
3 2
a b 1 s
1 s a a 1
1 a b 3
1
a a 1
3 a b 1
4 a b 1
6
1
e.g. r
2
A is customer name e.g.

r s: r s:
B is branch-name Students who have taken both "a
A r A B C and b courses, with instructor 1
1and 2 here show two specific branch-
names a

(Find students who have taken all
(Find customers who have an account in all a
branches of the bank) courses given by instructor 1)

35 B - tutor 36

6
Example Queries
Division Find all customers who have an account from at least the
Downtown and the Uptown branches.
Query 1

CN(BN=Downtown(depositor account))

CN(BN=Uptown(depositor account))

where CN denotes customer-name and BN denotes


branch-name.

Query 2
customer-name, branch-name (depositor account)
temp(branch-name) ({(Downtown), (Uptown)})

37 38

Example Queries Modification of the Database


Find all customers who have an account at all branches
located in Brooklyn city. The content of the database may be modified using the
customer-name, branch-name (depositor account) following operations:
Deletion
branch-name (branch-city = Brooklyn (branch))
Insertion
Updating
All these operations are expressed using the assignment
operator.

39 40

Deletion Deletion Examples


Delete all account records in the Perryridge branch.
A delete request is expressed similarly to a query, except account account branch-name = Perryridge (account)
instead of displaying tuples to the user, the selected
tuples are removed from the database. Delete all loan records with amount in the range of 0 to 50

Can delete only whole tuples; cannot delete values on loan loan amount 0and amount 50 (loan)
only particular attributes
A deletion is expressed in relational algebra by:
rrE
where r is a relation and E is a relational algebra query.

41 42

7
Insertion Insertion Examples
Insert information in the database specifying that Smith
has $1200 in account A-973 at the Perryridge branch.
To insert data into a relation, we either:
specify a tuple to be inserted account account {(Perryridge, A-973, 1200)}

write a query whose result is a set of tuples to be inserted depositor depositor {(Smith, A-973)}

in relational algebra, an insertion is expressed by:


r r E
where r is a relation and E is a relational algebra
expression.
The insertion of a single tuple is expressed by letting E be
a constant relation containing one tuple.

43 44

Update Examples
Updating Make interest payments by increasing all balances by 5 percent.
A mechanism to change a value in a tuple without account AN, BN, BAL * 1.05 (account)
charging all values in the tuple
where AN, BN and BAL stand for account-number, branch-name
Use the generalized projection operator to do this task and balance, respectively.
r F1, F2, , FI, (r) Pay all accounts with balances over $10,000 6 percent interest
Each Fi is either and pay all others 5 percent
the ith attribute of r, if the ith attribute is not updated, or,
account AN, BN, BAL * 1.06 ( BAL 10000 (account))
if the attribute is to be updated Fi is an expression,
AN, BN, BAL * 1.05 (BAL 10000 (account))
involving only constants and the attributes of r, which gives
the new value for the attribute

45 46

Banking Example Example Queries


branch (branch-name, branch-city, assets)
customer (customer-name, customer-street) Find the loan number for each loan of an amount greater than
$1200
account (account-number, branch-name, balance)

loan (loan-number, branch-name, amount) loan-number (amount > 1200 (loan))

depositor (customer-name, account-number)

borrower (customer-name, loan-number)

47 48

8
Example Queries Example Queries
Find the names of all customers who have a loan at the
Find the names of all customers who have a loan, an Perryridge branch.
account, or both, from the bank
customer-name (branch-name=Perryridge
customer-name (borrower) customer-name (depositor)
(borrower.loan-number = loan.loan-number(borrower x loan)))

Find the names of all customers who have a loan and an Find the names of all customers who have a loan at the
account at bank. Perryridge branch but do not have an account at any branch of
the bank.

customer-name (branch-name = Perryridge


customer-name (borrower) customer-name (depositor)

(borrower.loan-number = loan.loan-number(borrower x loan)))


customer-name(depositor)

49 50

Example Queries
Find the names of all customers who have a loan at the
Perryridge branch. Consider the University db with the tables:
Query 1 student(studno,name, hons, tutor, year)
customer-name(branch-name = Perryridge ( staff(lecturer, roomno)
borrower.loan-number = loan.loan-number(borrower x loan))) enrolled(studno, courseno, labmark, exammark)

Query 2
customer-name(loan.loan-number = borrower.loan-number(
(branch-name = Perryridge(loan)) x borrower))

51 52

Views
Write queries in relational algebra that return the following: In some cases, it is not desirable for all users to see the
The numbers of courses where a student had a better exam entire logical model (i.e., all the actual relations stored
mark than lab mark. in the database.)
The names of the lecturers who are tutoring a student who Consider a person who needs to know a customers loan
had an exam mark worse than the lab mark. number but has no need to see the loan amount. This
The names of the lecturers who are tutoring a 3rd year person should see a relation described, in the relational
student. algebra, by
The room numbers of the lecturers who are tutoring a 3rd customer-name, loan-number (borrower loan)
year student.
Any relation that is not of the conceptual model but is
The names of the lecturers who are tutoring more than one
made visible to a user as a virtual relation is called a

student
view.
The names of the lecturers who are tutoring no more than
one student
53 54

9
View Examples
View Definition Consider the view (named all-customer) consisting of
branches and their customers.
A view is defined using the create view statement which
has the form create view all-customer as
create view v as <query expression> branch-name, customer-name (depositor account)
branch-name, customer-name (borrower
where <query expression> is any legal relational algebra loan)
query expression. The view name is represented by v.
Once a view is defined, the view name can be used to We can find all customers of the Perryridge branch by writing:

refer to the virtual relation that the view generates.


customer-name
View definition is not the same as creating a new relation (branch-name = Perryridge (all-customer))
by evaluating the query expression
Rather, a view definition causes the saving of an
expression; the expression is substituted into queries using
the view.
55 56

Tuple Relational Calculus Predicate Calculus Formula


A nonprocedural query language, where each query is of
the form 1. Set of attributes and constants
{t | P (t) }
2.Set of comparison operators: (e.g., , , , , , )
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 3. Set of connectives: and (), or (v) not ()
attribute A 4.Implication (): x y, if x if true, then y is true
t r denotes that tuple t is in relation r x y x v y
P is a formula similar to that of the predicate calculus
5.Set of quantifiers:
t r (Q(t)) there exists a tuple in t in relation r
such that predicate Q(t) is true
t r (Q(t)) Q is true for all tuples t in relation r

57 58

Banking Example Example Queries


Find the loan-number, branch-name, and amount for
loans of over $1200
branch (branch-name, branch-city, assets) {t | t loan t [amount] 1200}

customer (customer-name, customer-street, customer-


city) Find the loan number for each loan of an amount greater than $1200

account (account-number, branch-name, balance) {t | s loan (t[loan-number] = s[loan-number] s [amount] 1200)}


loan (loan-number, branch-name, amount)
depositor (customer-name, account-number)
Notice that a relation on schema [loan-number] is implicitly defined
borrower (customer-name, loan-number) by the query

59 60

10
Example Queries Example Queries
Find the names of all customers having a loan, an Find the names of all customers having a loan at the
account, or both at the bank Perryridge branch
{t | s borrower( t[customer-name] = s[customer-name]) {t | s borrower(t[customer-name] = s[customer-name]
u depositor( t[customer-name] = u[customer-name]) u loan(u[branch-name] = Perryridge
u[loan-number] = s[loan-number]))}

Find the names of all customers who have a loan and an account Find the names of all customers who have a loan at the
at the bank Perryridge branch, but no account at any branch of the bank

{t | s borrower( t[customer-name] = s[customer-name]) {t | s borrower( t[customer-name] = s[customer-name]


u depositor( t[customer-name] = u[customer-name]) u loan(u[branch-name] = Perryridge
u[loan-number] = s[loan-number]))
not v depositor (v[customer-name] =
t[customer-name]) }

61 62

Example Queries Example Queries


Find the names of all customers having a loan from Find the names of all customers who have an
the Perryridge branch, and the cities they live in account at all branches located in Brooklyn:
{t | s loan(s[branch-name] = Perryridge {t | c customer (t[customer.name] = c[customer-name])
u borrower (u[loan-number] = s[loan-number] s branch(s[branch-city] = Brooklyn
t [customer-name] = u[customer-name]) u account ( s[branch-name] = u[branch-name]
v customer (u[customer-name] = v[customer-name] s depositor ( t[customer-name] = s[customer-name]
t[customer-city] = v[customer-city])))} s[account-number] = u[account-number] )) )}

63 64

Safety of Expressions Domain Relational Calculus


It is possible to write tuple calculus expressions that A nonprocedural query language equivalent in power to
generate infinite relations. the tuple relational calculus
For example, {t | t r} results in an infinite relation if the
domain of any attribute of relation r is infinite Each query is an expression of the form:
To guard against the problem, we restrict the set of
allowable expressions to safe expressions. { x1, x2, , xn | P(x1, x2, , xn)}
An expression {t | P(t)} in the tuple relational calculus is
safe if every component of t appears in one of the relations,
x1, x2, , xn represent domain variables
tuples, or constants that appear in P
NOTE: this is more than just a syntax condition. P represents a formula similar to that of the predicate
E.g. { t | t[A]=5 true } is not safe --- it defines an infinite set with calculus
attribute values that do not appear in any relation or tuples or
constants in P.

65 66

11
Example Queries Example Queries
Find the loan-number, branch-name, and amount for Find the names of all customers having a loan, an
loans of over $1200 account, or both at the Perryridge branch:
{ l, b, a | l, b, a loan a > 1200}
{ c | l ({ c, l borrower
b,a( l, b, a loan b = Perryridge))
Find the names of all customers who have a loan of over $1200 a( c, a depositor
{ c | l, b, a ( c, l borrower l, b, a loan a > 1200)} b,n( a, b, n account b = Perryridge))}

Find the names of all customers who have a loan from the Find the names of all customers who have an account at all
Perryridge branch and the loan amount: branches located in Brooklyn:

{ c, a | l ( c, l borrower b( l, b, a loan { c | s, n ( c, s, n customer)

b = Perryridge))} x,y,z( x, y, z branch y = Brooklyn)


a,b( x, y, z account c,a depositor)}
or { c, a | l ( c, l borrower l, Perryridge, a loan)}

67 68

Safety of Expressions
{ x1, x2, , xn | P(x1, x2, , xn)}

is safe if all of the following hold:


1. All values that appear in tuples of the expression are
values from dom(P) (that is, the values appear either in P
or in a tuple of a relation mentioned in P).
2.For every there exists subformula of the form x
(P1(x)), the subformula is true if and only if there is a
value of x in dom(P1) such that P1(x) is true.
3. For every for all subformula of the form x (P1 (x)),
the subformula is true if and only if
P1(x) is true for all values x from dom (P1).
69

12

Anda mungkin juga menyukai