Docsity

Relational Algebra: A Comprehensive Guide with Examples, Lecture notes of Algebra

Partial preview of the text

Download Relational Algebra: A Comprehensive Guide with Examples and more Lecture notes Algebra in PDF only on Docsity!

Relational algebra

Relational Algebra

  • (^) Procedural language
  • (^) Six basic operators
    • (^) select: σ
    • (^) project: ∏
    • (^) union: ∪
    • (^) set difference: –
    • (^) Cartesian product: x
    • (^) rename: ρ
  • (^) The operators take one or two relations as inputs and produce a new relation as a result.

Select Operation

  • Notation:^ σ^ p ( r )
  • (^) p is called the selection predicate
  • (^) Defined as: σ p ( r ) = { t | t ∈ r and p(t) } Where p is a formula in propositional calculus consisting of terms connected by : ∧ ( and ), ∨ ( or ), ¬ ( not ) Each term is one of: op or where op is one of: =, ≠, >, ≥. <. ≤
  • (^) Example of selection: σ dept_name=“Physics” ( instructor )

Project Operation – Example

Relation r : ∏ A,C ( r )

Union Operation – Example

Relations r, s: r ∪ s:

Union Operation

  • (^) Notation: r ∪ s
  • (^) Defined as: r ∪ s = { t | t ∈ r or t ∈ s }
  • (^) 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 (example: 2 nd column of r deals with the same type of values as does the 2 nd column of s )
  • (^) Example: to find all courses taught in the Fall 2009 semester, or in the Spring 2010 semester, or in both ∏ course_id ( σ semester=“Fall” Λ year= ( section )) ∪ ∏ course_id ( σ semester=“Spring” Λ year= ( section ))

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
  • (^) Example: to find all courses taught in the Fall 2009 semester, but not in the Spring 2010 semester ∏ course_id ( σ semester=“Fall” Λ year= ( section )) − ∏ course_id ( σ semester=“Spring” Λ year= ( section ))

Cartesian-Product Operation – Example

Relations r, s : r x s :

Composition of Operations

  • (^) Can build expressions using multiple operations
  • Example:^ σ A=C ( r x s )
  • (^) r x s
  • σ A=C ( r x s )

Rename Operation

  • (^) Allows us to name, and therefore to refer to, the results of relational-algebra expressions.
  • (^) Allows us to refer to a relation by more than one name.
  • (^) Example:

x

( E )

returns the expression E under the name X

  • (^) If a relational-algebra expression E has arity n , then returns the result of expression E under the name X , and with the attributes renamed to A 1 , A 2 , …., A n

,..., ) 2 , 1 (

E

n x A A A

Example Queries

  • (^) Find the names of all instructors in the Physics department, along with the course_id of all courses they have taught l (^) Query 1 ∏ instructor.ID,course_id (σ dept_name=“ Physics” ( σ instructor.ID=teaches.ID ( instructor x teaches ))) l (^) Query 2 ∏ instructor.ID,course_id (σ instructor.ID=teaches.ID ( σ dept_name=“ Physics” ( instructor) x teaches ))

Formal Definition

  • (^) A basic expression in the relational algebra consists of either one of the following: - (^) A relation in the database - (^) A constant relation
  • Let^ E 1 and E 2 be relational-algebra expressions; the following are all relational-algebra expressions: - E 1 ∪^ E 2 - E 1 - E 2 - E 1 x^ E 2 - σ p ( E 1 ), P is a predicate on attributes in E 1 - ∏ s ( E 1 ),^ S^ is a list consisting of some of the attributes in^ E 1 - (^) ρ x ( E 1 ), x is the new name for the result of E 1

Set-Intersection Operation

  • (^) Notation: r ∩ s
  • (^) Defined as:
  • (^) r ∩ s = { t | t ∈ r and t ∈ s }
  • (^) Assume:
    • (^) r , s have the same arity
    • (^) attributes of r and s are compatible
  • (^) Note: r ∩ s = r – ( r – s )

Set-Intersection Operation – Example

  • (^) Relation r, s :
  • (^) r ∩ s