Relational Algebra

Outline

Relational algebra ? What is it ?

  • Relational algebra is one two formal query languages associated with the relational database model
  • Relational algebra is a collection operations whose arguments are relational tables, sets of attributes, and formulas of prepositional calculus
  • An operation of relational algebra accepts one or two relational tables as the arguments and it returns a relational table as the result
  • The other arguments of relational algebra operations are set of attributes (columns) and formulas of prepositional calculus
  • What about the other formal language?
  • The other formal language is called as a relational calculus and it is based on the language of First Order Logic
TOP          Created by Janusz R. Getta, CSCI317 Database Performance Tuning, SIM, Session 3, 2023

Relational Algebra

Outline

Operations

  • Query
    • A query expressed in the language of relational algebra is an expression built over the operations of relational algebra and the arguments like relational tables, sets of attributes and fromulas of prepositional calculus
  • Operations
    • Selection: σφ(r)
    • Projection: πX(r)
    • Cross-product: r × s
    • Join: r ⨝φ s
    • Antijoin: r ∼φ t
    • Set operations: r ∪ t (union), r ∩ t (intersection) r - t (difference)
TOP          Created by Janusz R. Getta, CSCI317 Database Performance Tuning, SIM, Session 3, 2023

Operations

  • Selection
  • Let r be a relational table built over a schema (a set of attributes) Y
  • Selection operation σ applied to a relational table r is denoted by σφ(r) and it returns a set of all rows v ∈ r such that evaluation of φ(v) returns TRUE
  • For example σsalary >80K(EMPLOYEE) returns all rows from EMPLOYEE table that have a value of attribute salary greater than 80K
  • Selection operation horizontally restricts a relational table

      ORM

TOP          Created by Janusz R. Getta, CSCI317 Database Performance Tuning, SIM, Session 3, 2023

Operations

  • Projection
  • Let r be a relational table built over a schema (a set of attributes) Y,
    let X ⊆ Y
  • Projection operation π applied to a relational table r is denoted by πX(r) and it returns a set of all rows v such that there exists a row w ∈ r and
    v = w[X]
  • For example π{e#,salary} (EMPLOYEE) returns a table built over a schema {e#,salary} (two columns) such that each one of its rows in "included" in a relational table EMPLOYEE
  • Projection operation vertically restricts a relational table

      ORM

TOP          Created by Janusz R. Getta, CSCI317 Database Performance Tuning, SIM, Session 3, 2023

Operations

  • Cross-product
  • Let r be a relational table built over a schema (a set of attributes) Y, and s be a relational table built over a schema X
  • Cross-product operator × applied to the relational tables r and s is denoted by r × s and it returns all rows v such that v = concat(u,w),
    u ∈ r and w ∈ s
  • Cross product operation returns all possible concatenations of the rows from r and s

      ORM

TOP          Created by Janusz R. Getta, CSCI317 Database Performance Tuning, SIM, Session 3, 2023

Operations

  • Cross-product
  • For example, an expression
    EMPLOYEE(e#, name, salary, dname) × DEPARTMENT(dname, budget)
  • returns a relational table
    ED(e#, name, salary, E.dname, D.dname, budget)
    that consists of the concatenations of all rows from the relational tables EMPLOYEE and DEPARTMENT
TOP          Created by Janusz R. Getta, CSCI317 Database Performance Tuning, SIM, Session 3, 2023

Operations

  • Join
  • Let r be a relational table built over a schema (a set of attributes) Y, and s be a relational table built over a schema X, let φ be a formula of propositional logic over X ∪ Y
  • Join operation ⨝φ applied to the relational tables r and s is denoted by
    r ⨝φ s and it returns a value of relational algebra expression σφ(r × s)
  • Join operation concatenates all pairs of rows(v,w), where v ∈ r and w ∈ s that satisfy a given condition φ(v,w)

      Join operation

TOP          Created by Janusz R. Getta, CSCI317 Database Performance Tuning, SIM, Session 3, 2023

Operations

  • Join
  • For example, an expression
    EMP1(e#, salary, dname)⨝ EMP1.salary > EMP2.salary EMP2(e#, salary, dname)
  • returns a relational table
    EMP12(E1.e#,E1.salary,E1.dname,E2.e#,E2.salary,E2.dname)
  • that contains the concatenations of all rows from EMP1 and EMP2 and such that EMP1.salary > EMP2.salary
TOP          Created by Janusz R. Getta, CSCI317 Database Performance Tuning, SIM, Session 3, 2023

Operations

  • Equijoin
  • Let r be a relational table built over a schema (a set of attributes) Y, and s be a relational table built over a schema X, let A ∈ Y and B ∈ X
  • Equijoin operation ⨝A=B applied to the relational table r and s is denoted by
    r ⨝A=B s and it returns a value of relational algebra expression
    σr.A=s.B(r × s)

      Equijoin

TOP          Created by Janusz R. Getta, CSCI317 Database Performance Tuning, SIM, Session 3, 2023

Operations

  • Equijoin
  • For example
  • EMP(e#, name, salary, dname) ⨝ EMP.dname, DEPT.dname DEPT(dname, budget)
  • returns a relational table
  • ED(e#, name, salary E.dname, D.dname, budget)
  • that consists of the concatenations of all rows the relational tables EMP and DEPT such that EMP.dname = DEPT.dname
TOP          Created by Janusz R. Getta, CSCI317 Database Performance Tuning, SIM, Session 3, 2023

Operations

  • Natural join
  • Let r be a relational table built over a schema (a set of attributes) Y, and s be a relational table built over a schema X, let A = Y ∩ X
  • Natural join operation ⨝ applied to the relational tables r and s is denoted by r ⨝ s and it returns a value of relational algebra expression
    π schema(r) ∪ schema(s) ( σr.A=s.A (r × s) )

      Natural join

TOP          Created by Janusz R. Getta, CSCI317 Database Performance Tuning, SIM, Session 3, 2023

Operations

  • Natural join
  • For example, an expression
    EMP(e#, name, salary, dname) ⨝DEPT(dname, budget)
  • returns a relational table ED(e#,name, dname, budget)
  • that consists of the concatenations of all rows from the relational tables EMP and DEPT and EMP.dname = DEPT.dname = ED.dname
TOP          Created by Janusz R. Getta, CSCI317 Database Performance Tuning, SIM, Session 3, 2023

Operations

  • Antijoin
  • Let r be a relational table built over a schema (a set of attributes) Y, and s be a relational table built over a schema X, let φ be a propositional formula over X ∪ Y
  • Antijoin operation ∼φ applied to the relational tables r and s is denoted by
    r ∼φ s and it returns a value of relational algebra expression
    r - πY( σφ(r × s))

      Antijoin

TOP          Created by Janusz R. Getta, CSCI317 Database Performance Tuning, SIM, Session 3, 2023

Operations

  • Antijoin
  • For example, an expression
    EMP1(e#, salary, dname)∼EMP1.salary = EMP2.salary EMP2(e#, salary, dname)
  • returns a relational table EMP12(E1.e#,E1.salary,E1.dname)
  • that contains all rows from a relational table EMP1 such that for all values in EMP1.salary does not exist a value EMP2.salary where
    EMP1.salary = EMP2.salary
TOP          Created by Janusz R. Getta, CSCI317 Database Performance Tuning, SIM, Session 3, 2023

Operations

  • Set operators
  • Let r be a relational table built over a schema (a set of attributes) Y, and s be a relational table built over a schema Y
  • Visualizations of set operations are the following

      ORM

TOP          Created by Janusz R. Getta, CSCI317 Database Performance Tuning, SIM, Session 3, 2023

Relational Algebra

Outline

SELECT statement versus relational algebra

  • Any SELECT statement that does not contain GROUP BY clause and group functions can be translated into a relational algebra expression
      
      SELECT S_NAME, S_ADDRESS
      FROM SUPPLIER
      WHERE S_ACCTBAL > 100;
  • πS_NAME,S_ADDRESS( σS_ACCTBAL > 100 (SUPPLIER))
    • 
      SELECT P_NAME
      FROM PART
      WHERE P_SIZE = 6
       INTERSECT
      SELECT P_NAME
      FROM PART
      WHERE P_TYPE = 'XYZ';
  • πP_NAME ( σP_SIZE=6 (PART)) ∩ πP_NAME ( σP_TYPE='XYZ' (PART))
TOP          Created by Janusz R. Getta, CSCI317 Database Performance Tuning, SIM, Session 3, 2023

SELECT statement versus relational algebra

    • 
      SELECT L_SHIPDATE
      FROM LINEITEM, ORDERS
      WHERE LINEITEM.L_ORDERKEY = ORDERS.O_ORDERKEY AND
            ORDERS.O_TOTALPRICE > 100;
  • πL_SHIPDATE (σO_TOTALPRICE>100 (ORDERS ⨝L_ORDERKEY = O_ORDERKEY LINEITEM) )
    • 
      SELECT C_NAME, N_NAME
      FROM CUSTOMER JOIN ORDERS
                    ON CUSTOMER.C_CUSTKEY = ORDERS.O_CUSTKEY
                    JOIN NATION
                    ON CUSTOMER.C_NATIONKEY = NATION.N_NATIONKEY
      WHERE C_ACCTBAL = 0;
  • πC_NAME, N_NAME ( σC_ACCTBAL=0 (CUSTOMER)⨝ NATION)⨝ ORDERS) ) )
TOP          Created by Janusz R. Getta, CSCI317 Database Performance Tuning, SIM, Session 3, 2023

SELECT statement versus relational algebra

    • 
      SELECT C_CUSTKEY, O_ORDERKEY
      FROM CUSTOMER LEFT OUTER JOIN ORDERS
                    ON C_CUSTKEY = O_CUSTKEY;
  • ( (πC_CUSTKEY (CUSTOMER) - πO_CUSTKEY (ORDERS) ) × {NULL} ) ∪
    πC_CUSTKEY,O_ORDERKEY (CUSTOMER ⨝ C_CUSTKEY=O_CUSTKEY ORDERS)
    • 
      SELECT O_TOTALPRICE
      FROM ORDERS
      WHERE O_CUSTKEY IN (SELECT C_CUSTKEY
                          FROM CUSTOMER
                          WHERE C_ACCTBAL = 0);
  • πO_TOTALPRICE (ORDERS ⨝ O_CUSTKEY=C_CUSTKEY ( σC_ACCTBAL=0 (CUSTOMER) ) )
TOP          Created by Janusz R. Getta, CSCI317 Database Performance Tuning, SIM, Session 3, 2023

SELECT statement versus relational algebra

    • 
      SELECT O_TOTALPRICE
      FROM ORDERS
      WHERE O_CUSTKEY NOT IN (SELECT C_CUSTKEY
                              FROM CUSTOMER
                              WHERE C_ACCTBAL = 0);
  • πO_TOTALPRICE (ORDERS ∼ O_CUSTKEY=C_CUSTKEY ( σC_ACCTBAL=0 (CUSTOMER) ) )
    • 
      SELECT O_TOTALPRICE
      FROM ORDERS
      WHERE EXISTS (SELECT C_CUSTKEY
                    FROM CUSTOMER
                    WHERE ORDERS.O_CUSTKEY = CUSTOMER.C_CUSTKEY AND C_ACCTBAL = 0);
  • πO_TOTALPRICE (ORDERS ⨝ O_CUSTKEY=C_CUSTKEY ( σC_ACCTBAL=0 (CUSTOMER) ) )
TOP          Created by Janusz R. Getta, CSCI317 Database Performance Tuning, SIM, Session 3, 2023

SELECT statement versus relational algebra

    • 
      SELECT O_TOTALPRICE
      FROM ORDERS
      WHERE NOT EXISTS (SELECT C_CUSTKEY
                        FROM CUSTOMER
                        WHERE ORDERS.O_CUSTKEY = CUSTOMER.C_CUSTKEY AND C_ACCTBAL = 0);
  • πO_TOTALPRICE (ORDERS ∼ O_CUSTKEY=C_CUSTKEY ( σC_ACCTBAL=0 (CUSTOMER) ) )
TOP              Created by Janusz R. Getta, CSCI317 Database Performance Tuning, Autumn 2023

References

  • R. Ramakrishnan and J. Gherke Database Management Systems, 3rd ed. Mc Graw-Hill, 2003,chapter 4.2
TOP          Created by Janusz R. Getta, CSCI317 Database Performance Tuning, SIM, Session 3, 2023