Giáo trình SQL bài 10

37 200 0
Giáo trình SQL bài 10

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Lecture The Relational Algebra and Relational Calculus – Objectives • Relational Algebra Unary Relational Operations Relational Algebra Operations From Set Theory • Reference: Chapter Faculty of Science and Technology Database Fundamentals Review Relational Database Model • Database: relations (tables) • Relation: attributes (columns) – tuples (rows) • Attribute: domain (data type) Faculty of Science and Technology Database Fundamentals Relational Algebra Overview • Relational algebra is the basic set of operations for the relational model • These operations enable a user to specify basic retrieval requests (or queries) • The result of an operation is a new relation, which may have been formed from one or more input relations This property makes the algebra “closed” (all objects in relational algebra are relations) • A sequence of relational algebra operations forms a relational algebra expression The result of a relational algebra expression is also a relation that represents the result of a database query (or retrieval request) Faculty of Science and Technology Database Fundamentals Relational Algebra Overview • Relational Algebra consists of several groups of operations Unary Relational Operations • SELECT (σ (sigma)) • PROJECT (π (pi)) • RENAME (ρ (rho)) Relational Algebra Operations From Set Theory • UNION ( ∪ ), INTERSECTION ( ∩ ), DIFFERENCE (or MINUS, – ) • CARTESIAN PRODUCT ( x ) Binary Relational Operations (next session) • JOIN (several variations of JOIN exist) • DIVISION Additional Relational Operations (next session) • OUTER JOINS, OUTER UNION • AGGREGATE FUNCTIONS (These compute summary of information: for example, SUM, COUNT, AVG, MIN, MAX) Faculty of Science and Technology Database Fundamentals Database State for COMPANY • All examples discussed below refer to the COMPANY database shown here Faculty of Science and Technology Database Fundamentals Database State for COMPANY Faculty of Science and Technology Database Fundamentals Unary Relational Operations: SELECT • The SELECT operation (σ) is used to select a subset of the tuples from a relation based on a selection condition The selection condition acts as a filter Keeps only those tuples that satisfy the qualifying condition Tuples satisfying the condition are selected whereas the other tuples are discarded (filtered out) • Examples: Select the EMPLOYEE tuples whose department number is 4: σ Dno = (EMPLOYEE) Select the employee tuples whose salary is greater than $30,000: σ Salary > 30,000 (EMPLOYEE) Faculty of Science and Technology Database Fundamentals Unary Relational Operations: SELECT (2) In general, the select operation is denoted by σ (R) where • the symbol σ (sigma) is used to denote the select operator • the selection condition is a Boolean (conditional) expression specified on the attributes of relation R • tuples that make the condition true are selected appear in the result of the operation • tuples that make the condition false are filtered out discarded from the result of the operation Faculty of Science and Technology Database Fundamentals Unary Relational Operations: SELECT (3) • SELECT Operation Properties The SELECT operation σ (R) produces a relation S that has the same schema (same attributes) as R SELECT σ is commutative: • σ (σ < condition2> (R)) = σ (σ < condition1> (R)) • σ (σ (σ (R))) = σ (σ (σ ( R))) Conjunction of all the conditions: • σ (σ < cond2> (σ (R))) = σ AND < cond2> AND < cond3>(R) The number of tuples in the result of a SELECT is less than (or equal to) the number of tuples in the input relation R Faculty of Science and Technology Database Fundamentals 10 Example of Union • To retrieve the social security numbers of all employees who either work in department (RESULT1 below) or directly supervise an employee who works in department (RESULT2 below) • We can use the UNION operation as follows: DEP5_EMPS ← σDno=5 (EMPLOYEE) RESULT1 ← π Ssn(DEP5_EMPS) RESULT2(SSN) ← πSuperSsn(DEP5_EMPS) RESULT ← RESULT1 ∪ RESULT2 • The union operation produces the tuples that are in either RESULT1 or RESULT2 or both Faculty of Science and Technology Database Fundamentals 23 Example of Union (2) Faculty of Science and Technology Database Fundamentals 24 Relational Algebra Operations from Set Theory: INTERSECTION • INTERSECTION is denoted by ∩ • The result of the operation R ∩ S, is a relation that includes all tuples that are in both R and S The attribute names in the result will be the same as the attribute names in R • The two operand relations R and S must be “type compatible” Faculty of Science and Technology Database Fundamentals 25 Example of Intersection • To retrieve the social security numbers of all employees who either work in department (RESULT1 below) and directly supervise an employee who works in department (RESULT2 below) • We can use the UNION operation as follows: DEP5_EMPS ← σDno=5 (EMPLOYEE) RESULT1 ← π Ssn(DEP5_EMPS) RESULT2(SSN) ← πSuperSsn(DEP5_EMPS) RESULT ← RESULT1 ∩ RESULT2 • The intersection operation produces the tuples that are in RESULT1 and RESULT2 Faculty of Science and Technology Database Fundamentals 26 Example of Intersection (2) Faculty of Science and Technology Database Fundamentals 27 Relational Algebra Operations from Set Theory: SET DIFFERENCE • SET DIFFERENCE (also called MINUS or EXCEPT) is denoted by – • The result of R – S, is a relation that includes all tuples that are in R but not in S The attribute names in the result will be the same as the attribute names in R • The two operand relations R and S must be “type compatible” Faculty of Science and Technology Database Fundamentals 28 Example of Minus • To retrieve the social security numbers of all employees who work in department (RESULT1 below) and not directly supervise an employee who works in department (RESULT2 below) • We can use the UNION operation as follows: DEP5_EMPS ← σDno=5 (EMPLOYEE) RESULT1 ← π Ssn(DEP5_EMPS) RESULT2(SSN) ← πSuperSsn(DEP5_EMPS) RESULT ← RESULT1 – RESULT2 • The intersection operation produces the tuples that are in RESULT1 but not in RESULT2 Faculty of Science and Technology Database Fundamentals 29 Example of Minus (2) Faculty of Science and Technology Database Fundamentals 30 Other Example Faculty of Science and Technology Database Fundamentals 31 Some properties of UNION, INTERSECT, and DIFFERENCE • Notice that both union and intersection are commutative operations; that is R ∪ S = S ∪ R, and R ∩ S = S ∩ R • Both union and intersection can be treated as nary operations applicable to any number of relations as both are associative operations; that is R ∪ (S ∪ T) = (R ∪ S) ∪ T (R ∩ S) ∩ T = R ∩ (S ∩ T) • The minus operation is not commutative; that is, in general R–S≠S–R Faculty of Science and Technology Database Fundamentals 32 Relational Algebra Operations from Set Theory: CARTESIAN PRODUCT CARTESIAN (or CROSS) PRODUCT Operation This operation is used to combine tuples from two relations in a combinatorial fashion Denoted by R(A1, A2, , An) x S(B1, B2, , Bm) Result is a relation Q with degree n + m attributes: • Q(A1, A2, , An, B1, B2, , Bm), in that order The resulting relation state 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 x S will have nR * nS tuples The two operands NOT have to be "type compatible” Faculty of Science and Technology Database Fundamentals 33 Relational Algebra Operations from Set Theory: CARTESIAN PRODUCT (2) Generally, CROSS PRODUCT is not a meaningful operation Can become meaningful when followed by other operations • Example (not meaningful): FEMALE_EMPS ← σ Sex=’F’(EMPLOYEE) EMPNAMES ← π Fname, Lname, Ssn (FEMALE_EMPS) EMP_DEPENDENTS ← EMPNAMES x DEPENDENT • EMP_DEPENDENTS will contain every combination of EMPNAMES and DEPENDENT whether or not they are actually related Faculty of Science and Technology Database Fundamentals 34 Relational Algebra Operations from Set Theory: CARTESIAN PRODUCT (3) To keep only combinations where the DEPENDENT is related to the EMPLOYEE, we add a SELECT operation as follows • Example (meaningful): FEMALE_EMPS ← σ Sex=’F’(EMPLOYEE) EMPNAMES ← π Fname, Lname, Ssn (FEMALE_EMPS) EMP_DEPENDENTS ← EMPNAMES x DEPENDENT ACTUAL_DEPS ← σ Ssn=Essn(EMP_DEPENDENTS) RESULT ← π Fname, Lname, Dependent_name (ACTUAL_DEPS) • RESULT will now contain the name of female employees and their dependents Faculty of Science and Technology Database Fundamentals 35 Example of applying CARTESIAN PRODUCT FEMALE_EMPS ← σ Sex=’F’(EMPLOYEE) EMPNAMES ← π Fname, Lname, Ssn (FEMALE_EMPS) EMP_DEPENDENTS ← EMPNAMES x DEPENDENT ACTUAL_DEPS ← σ Ssn=Essn(EMP_DEPENDENTS) RESULT ← π Fname, Lname, Dependent_name (ACTUAL_DEPS) Faculty of Science and Technology Database Fundamentals 36 ... number of tuples in the input relation R Faculty of Science and Technology Database Fundamentals 10 Example of Select • σ(Dno=4 AND Salary>25000) OR (Dno=5 AND Salary> 30000)(EMPLOYEE) Faculty

Ngày đăng: 03/12/2015, 03:58

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan