Download presentation
Presentation is loading. Please wait.
1
Fundamental of Database Systems 1401312-3
By Dr Abdullah Alzahrani د. عبدالله الزهراني Part 2
2
Assessment توزيع الدرجات
20% Mid-Term written exam 10% Mid-Term practical exam 20% Project 50% Final Exam 20% اختبار نصفي نظري 10% اختبار نصفي عملي 20% مشروع 50% اختبار نهائي Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016
3
Reference الكتاب المرجع
Fundamentals of Database Systems, 5th ed., by Elmasri and Navathe, Pearson International Edition, Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016
4
Relational Algebra Relational algebra Introduction Operations
Unary Operations SELECT PROJECT Rename Binary Operations JOIN and Cartesian product DIVISION Relational Algebra Operations from Set Theory Some important terms relations, tuples, attributes, relation schema Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016
5
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: formal 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 some of its concepts are incorporated into the SQL standard query language. Relational algebra operations {σ,π, ρ,⨝, ×, , ∪, ∩,–} Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016
6
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 9/20/2016
7
Relational Algebra Operations
The fundamental operations of relational algebra are as follows − Unary Relational Operations: SELECT PROJECT Rename Binary Relational Operations: 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 9/20/2016
8
Relational Algebra SELECT operation
Unary Relational Operations SELECT operation The SELECT operation is used to choose a subset of the tuples from a relation that satisfies a selection condition symbol σ (sigma) 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. Fundamental of Database Systems 9/20/2016
9
Relational Algebra PROJECT operation
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 9/20/2016
10
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 9/20/2016
11
Relational Algebra RENAME operation
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. Fundamental of Database Systems 9/20/2016
12
Relational Algebra Examples: What are the results of the above?
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. Fundamental of Database Systems 9/20/2016
13
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 9/20/2016
14
Relational Algebra JOIN operation example Binary Relational Operations
Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016
15
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. Fundamental of Database Systems 9/20/2016
16
Relational Algebra CARTESIAN example Binary Relational Operations
Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016
17
Relational Algebra CARTESIAN example Binary Relational Operations
Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016
18
Relational Algebra CARTESIAN example Binary Relational Operations
Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016
19
Relational Algebra In slide 15
CARTESIAN example Binary Relational Operations In slide 15 What was the required action (Questions) for the aforementioned results? Explain each operation? Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016
20
Relational Algebra DIVISION Operation
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. Fundamental of Database Systems 9/20/2016
21
Relational Algebra DIVISION example Binary Relational Operations
Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016
22
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 9/20/2016
23
Relational Algebra Some examples
Relational Algebra Operations from Set Theory: ∩ Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016
24
Relational Algebra Use of the Functional operator ℱ
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. Fundamental of Database Systems 9/20/2016
25
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. Fundamental of Database Systems 9/20/2016
26
Relational Algebra Some important terms: Relation: Attribute: Tuple:
is defined to be a set of tuples in the formal relational model Attribute: represents a same property of a same data type for all entities. Tuple: represents a set of properties of different data types for a specific entity Relation schema: Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016
27
End Relational Algebra Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa
Fundamental of Database Systems 9/20/2016
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.