Download Fundamental of Database Systems 1401312-3

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Microsoft Jet Database Engine wikipedia , lookup

SQL wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Functional Database Model wikipedia , lookup

Concurrency control wikipedia , lookup

Clusterpoint wikipedia , lookup

Database wikipedia , lookup

Versant Object Database wikipedia , lookup

Database model wikipedia , lookup

Relational algebra wikipedia , lookup

Relational model wikipedia , lookup

Transcript
Fundamental of Database Systems
1401312-3
By
Dr Abdullah Alzahrani ‫ عبدهللا الزهراني‬.‫د‬
[email protected]
Part 2
Assessment ‫توزيع الدرجات‬
• 20% Mid-Term written exam
• 10% Mid-Term practical exam
• 20% Project
• 50% Final Exam
‫ اختبار نصفي نظري‬20%
‫ اختبار نصفي عملي‬10%
Dr Abdullah Alzahrani.
[email protected]
Fundamental of Database Systems 1401312-3
‫ مشروع‬20%
‫ اختبار نهائي‬50%
9/20/2016
2
•
•
•
•
Reference ‫الكتاب المرجع‬
• Fundamentals
•
of Database Systems, 5th ed., by
Elmasri and Navathe, Pearson International Edition,
2007.
http://dev.mysql.com/doc/
Dr Abdullah Alzahrani.
[email protected]
Fundamental of Database Systems 1401312-3
9/20/2016
3
Relational Algebra
•
Relational algebra
•
•
•
Introduction
Operations
•
Unary Operations
•
Binary Operations
•
Relational Algebra Operations from Set Theory
• SELECT
• PROJECT
• Rename
• JOIN and Cartesian product
• DIVISION
Some important terms
•
relations, tuples, attributes, relation schema
Dr Abdullah Alzahrani.
[email protected]
Fundamental of Database Systems 1401312-3
9/20/2016
4
Relational Algebra
•
•
•
•
•
•
Relational algebra is a query language for manipulating relations. It takes
instances of relations as input and produces instances of relations as output. It
uses operators to perform queries. An operator can be either unary or binary.
The relational algebra is often considered to be an primary part of the relational
data model
It provides a set of operations that allow creation and manipulation of relation.
What is an expression in relational algebra ?
•
A sequence of relational algebra operations
The relational algebra is very important for several reasons:
1.
2.
3.
formal language
4.
some of its concepts are incorporated into the SQL standard query language.
It provides a formal foundation for relational model operations.
it is used as a basis for implementing and optimizing queries in the query processing and
optimization
Relational algebra operations {σ,π, ρ,⨝, ×,  , ∪, ∩,–}
Dr Abdullah Alzahrani.
[email protected]
Fundamental of Database Systems 1401312-3
9/20/2016
5
Relational Algebra
• Some types of operations that, in general, cannot be
specified in the basic original relational algebra:
•
•
Recursive closure.
Aggregate functions
Dr Abdullah Alzahrani.
Fundamental of Database Systems
[email protected]
1401312-3
9/20/2016
6
Relational Algebra
• Operations
•
The fundamental operations of relational algebra are as follows −
•
Unary Relational Operations:
•
•
•
•
PROJECT
Rename
Binary Relational Operations:
•
•
•
SELECT
JOIN and Cartesian product
DIVISION
Relational Algebra Operations from Set Theory:
•
The UNION, INTERSECTION, and MINUS (SET DIFFERENCE) Operations
Dr Abdullah Alzahrani.
Fundamental of Database Systems
[email protected]
1401312-3
9/20/2016
7
Relational Algebra
Unary Relational Operations
•
•
SELECT operation
•
•
symbol σ (sigma)
•
The SELECT operation is used to choose a subset of the tuples from a
relation that satisfies a selection condition
In general, the SELECT operation is denoted by
•
•
σ <selection condition>(R)
where the symbol σ(sigma) is used to denote the SELECT operator and the
selection condition is a Boolean expression (condition) specified on the attributes
of relation R. Notice that R is generally a relational algebra expression whose
result is a relation
Examples:
Dr Abdullah Alzahrani.
[email protected]
Fundamental of Database Systems 1401312-3
9/20/2016
8
Relational Algebra
•
•
•
•
•
Unary Relational Operations
PROJECT operation
If we think of a relation as a table, the SELECT operation chooses
some of the rows from the table while discarding other rows. The
PROJECT operation, on the other hand, selects certain columns from
the table and discards the other columns
symbol π (pi)
In general, the PROJECT operation is denoted by
•
•
π <attribute list> (R)
where π(pi) is the symbol used to represent the PROJECT operation, and
<attribute list> is the desired sublist of attributes from the attributes of
relation R.
Examples:
π Bdate, Salary (EMPLOYEE)
Dr Abdullah Alzahrani.
Fundamental of Database Systems
[email protected]
1401312-3
9/20/2016
9
Relational Algebra
Unary Relational Operations
OK, How do we get Only the Bdate of employees whose Dno = 5?
•
•
•
•
•
Answer:
π Bdate(σ Dno=5( EMPLOYEE))
in-line expression
or
DEP5_EMPS ← σ Dno=5 (EMPLOYEE)
sequence of operations
RESULT ← π Bdate(DEP5_EMPS)
Dr Abdullah Alzahrani.
Fundamental of Database Systems
[email protected]
1401312-3
9/20/2016
10
Relational Algebra
Unary Relational Operations
•
•
•
•
RENAME operation
can RENAME either the relation name or the attribute names,
or both
symbol ρ(rho)
In general, the RENAME operation is denoted by
•
•
or
or
where S is the new relation name, and B1, B2, ..., Bn are the new
attribute names. The first expression renames both the relation and
its attributes, the second renames the relation only, and the third
renames the attributes only. If the attributes of R are (A1,A2, ...,An)
in that order, then each Ai is renamed as Bi.
Dr Abdullah Alzahrani.
[email protected]
Fundamental of Database Systems 1401312-3
9/20/2016
11
Relational Algebra
Unary Relational Operations
• Examples:
•
•
•
ρ Empl_table(EMPLOYEE)
ρ DoB, Sal_Monthly(π Bdate, Salary (EMPLOYEE))
ρ Empl_table(DoB, Sal_Monthly)(π Bdate, Salary (EMPLOYEE))
• What are the results of the above?
Dr Abdullah Alzahrani.
[email protected]
Fundamental of Database Systems 1401312-3
9/20/2016
12
Relational Algebra
Binary Relational Operations
•
•
•
•
•
•
JOIN operation
is used to combine related tuples from two relations into single “longer”
tuples
The sequence of Cartesian product followed by select is used quite
commonly to identify and select related tuples from two relations.
It is denoted by a
•
Symbol ⨝
This operation is very important for any relational database with more than a
single relation, because it allows us to process relationships among relations.
The general form of a join operation on two relations R(A1, A2, . . ., An) and
S(B1, B2, . . ., Bm) is:
•
R ⨝ <join condition>S
where R and S can be any relations that result from general relational
algebra expressions.
Dr Abdullah Alzahrani.
Fundamental of Database Systems
[email protected]
1401312-3
9/20/2016
13
Relational Algebra
• JOIN operation example
Dr Abdullah Alzahrani.
Fundamental of Database Systems
[email protected]
1401312-3
Binary Relational Operations
9/20/2016
14
Relational Algebra
•
•
•
Binary Relational Operations
CARTESIAN (or cross product) Operation
This operation is used to combine tuples from two relations in a combinatorial
fashion. In general, the result of R(A1, A2, . . ., An) x S(B1, B2, . . ., Bm) is a
relation Q with degree n + m attributes Q(A1, A2, . . ., An, B1, B2, . . ., Bm), in that
order. The resulting relation Q has one tuple for each combination of tuples—one
from R and one from S.
•
Hence, if R has nR tuples (denoted as |R| = nR ), and S has nS tuples, then
•
| R × S | will have nR * nS tuples.
The two operands do NOT have to be "type compatible”
In JOIN, only combinations of tuples satisfying the join condition appear in the
result, whereas in the CARTESIAN PRODUCT all combinations of tuples are
included in the result.
Example:
FEMALE_EMPS   SEX=’F’(EMPLOYEE)
EMPNAMES   FNAME, LNAME, SSN (FEMALE_EMPS)
EMP_DEPENDENTS  EMPNAMES × DEPENDENT
Dr Abdullah Alzahrani.
[email protected]
Fundamental of Database Systems 1401312-3
9/20/2016
15
Relational Algebra
•
CARTESIAN example
Dr Abdullah Alzahrani.
[email protected]
Fundamental of Database Systems 1401312-3
Binary Relational Operations
9/20/2016
16
Relational Algebra
•
CARTESIAN example
Dr Abdullah Alzahrani.
Fundamental of Database Systems
Binary Relational Operations
[email protected]
1401312-3
9/20/2016
17
Relational Algebra
•
CARTESIAN example
Dr Abdullah Alzahrani.
Fundamental of Database Systems
Binary Relational Operations
[email protected]
1401312-3
9/20/2016
18
Relational Algebra
•
CARTESIAN example
Binary Relational Operations
In slide 15
1. What was the required action (Questions)
for the aforementioned results?
2. Explain each operation?
Dr Abdullah Alzahrani.
[email protected]
Fundamental of Database Systems 1401312-3
9/20/2016
19
Relational Algebra
Binary Relational Operations
DIVISION Operation
• The division operation is applied to two relations
R(Z)  S(X), where X subset Z. Let Y = Z - X (and hence
Z = X  Y); that is, let Y be the set of attributes of R that
are not attributes of S.
• The result of DIVISION is a relation T(Y) that
includes a tuple t if tuples tR appear in R with tR [Y] =
t, and with
tR [X] = ts for every tuple ts in S.
• For a tuple t to appear in the result T of the
DIVISION, the values in t must appear in R in
combination with every tuple in S.
Dr Abdullah Alzahrani.
[email protected]
Fundamental of Database Systems 1401312-3
9/20/2016
20
Relational Algebra
DIVISION example
Dr Abdullah Alzahrani.
Fundamental of Database Systems
Binary Relational Operations
[email protected]
1401312-3
9/20/2016
21
Relational Algebra
Relational Algebra Operations from Set Theory:
We can define the three operations UNION,INTERSECTION, and SET
DIFFERENCE on two union-compatible relations R and S as follows:
•
•
•
UNION (∪): The result of this operation, denoted by R∪S, is a
relation that includes all tuples that are either in R or in S or in both R
and S. Duplicate tuples are eliminated.
INTERSECTION (∩): The result of this operation, denoted by R∩S, is
a relation that includes all tuples that are in both Rand S.
SET DIFFERENCE (or MINUS): The result of this operation, denoted
by R–S, is a relation that includes all tuples that are in R but not in S.
Dr Abdullah Alzahrani.
Fundamental of Database Systems
[email protected]
1401312-3
9/20/2016
22
Relational Algebra
• Some examples
Relational Algebra Operations from Set Theory:
∩
Dr Abdullah Alzahrani.
Fundamental of Database Systems
[email protected]
1401312-3
9/20/2016
23
Relational Algebra
Additional Relational Operations
Use of the Functional operator ℱ
ℱMAX Salary (Employee) retrieves the maximum salary value
from the Employee relation
ℱMIN Salary (Employee) retrieves the minimum Salary value
from the Employee relation
ℱSUM Salary (Employee) retrieves the sum of the Salary from
the Employee relation
Dr Abdullah Alzahrani.
[email protected]
Fundamental of Database Systems 1401312-3
9/20/2016
24
Relational Algebra
• Examples of Queries in Relational Algebra
•
Note: all needed relations (tables) are in slide 14 and 16
• Q1: Retrieve the name and address of all employees
who work for the ‘Research’ department.
Q2: Retrieve the names of employees who have no
dependents.
Rename attribute
Dr Abdullah Alzahrani.
[email protected]
Fundamental of Database Systems 1401312-3
9/20/2016
25
Relational Algebra
Some important terms:
• Relation:
•
is defined to be a set of tuples in the formal relational model
•
represents a same property of a same data type for all entities.
• Attribute:
• Tuple:
•
represents a set of properties of different data types for a
specific entity
• Relation schema:
•
Dr Abdullah Alzahrani.
Fundamental of Database Systems
[email protected]
1401312-3
9/20/2016
26
Relational Algebra
End
Dr Abdullah Alzahrani.
Fundamental of Database Systems
[email protected]
1401312-3
9/20/2016
27