Relational Algebra
V.Deepa M.Sc.,M.Phil.,
Assistant Professor
Department of Computer Science
Sri Ramakrishna College of Arts and Science
Relational Algebra
• Five operators:
• Union: 
• Difference: -
• Selection: s
• Projection: P
• Cartesian Product: 
• Derived operators:
• Intersection, complement
• Joins (natural,equi-join, theta join, semi-join)
• Renaming: r
DBMS
• Database Management System or DBMS in short
refers to the technology of storing and retrieving
users data.
What is data and Information?
How relational algebra related to
DBMS?
• Relational algebra mainly provides theoretical
foundation for relational databases
Introduction to Relation Algebra
• It was introduction by 1970
• By EF Code who is the father of DBMS.
• It is called procedural or formal query language.
Relational Algebra
• Frame for creating new relations from existing ones
• Its place in the big picture:
Declartive
query
language
Algebra
Implementation
SQL,
relational calculus
Relational algebra
Executable code
1. Union and 2. Difference
• R1  R2
• Example:
• ActiveEmployees  RetiredEmployees
• R1 – R2
• Example:
• AllEmployees -- RetiredEmployees
What about Intersection ?
• It is a derived operator
• R1  R2 = R1 – (R1 – R2)
• Also expressed as a join (will see later)
• Example
• UnionizedEmployees  RetiredEmployees
3. Selection
• Returns all tuples which satisfy a condition
• Notation: sc(R)
• Examples
• sSalary > 40000 (Employee)
• sname = “Smith” (Employee)
• The condition c can be =, <, , >, , <>
sSalary > 40000 (Employee)
SSN Name Salary
1234545 John 200000
5423341 Smith 600000
4352342 Fred 500000
SSN Name Salary
5423341 Smith 600000
4352342 Fred 500000
4. Projection
• Eliminates columns, then removes duplicates
• Notation: P A1,…,An (R)
• Example: project social-security number and names:
• P SSN, Name (Employee)
• Output schema: Answer(SSN, Name)
P Name,Salary (Employee)
SSN Name Salary
1234545 John 200000
5423341 John 600000
4352342 John 200000
Name Salary
John 20000
John 60000
5. Cartesian Product
• Each tuple in R1 with each tuple in R2
• Notation: R1  R2
• Example:
• Employee  Dependents
• Very rare in practice; mainly used to express joins
Cartesian Product Example
Employee
Name SSN
John 999999999
Tony 777777777
Dependents
EmployeeSSN Dname
999999999 Emily
777777777 Joe
Employee x Dependents
Name SSN EmployeeSSN Dname
John 999999999 999999999 Emily
John 999999999 777777777 Joe
Tony 777777777 999999999 Emily
Tony 777777777 777777777 Joe
Relational Algebra
• Five operators:
• Union: 
• Difference: -
• Selection: s
• Projection: P
• Cartesian Product: 
• Derived or auxiliary operators:
• Intersection, complement
• Joins (natural,equi-join, theta join, semi-join)
• Renaming: r
Operations on Bags
A bag = a set with repeated elements
All operations need to be defined carefully on bags
• {a,b,b,c}{a,b,b,b,e,f,f}={a,a,b,b,b,b,b,c,e,f,f}
• {a,b,b,b,c,c} – {b,c,c,c,d} = {a,b,b,c,d}
• sC(R): preserve the number of occurrences
• PA(R): no duplicate elimination
• Cartesian product, join: no duplicate elimination
Important ! Relational Engines work on bags, not sets !
Joins
• Joins is a combinatio of Cartesian product by a
selection process.
• Inner Joins:
Theta join
EQUI join
Natural join
• Outer joins:
Left outer join
Right outer join
Full outer join
• Also known as conditional join.
• Used when you want to join two or more relation
based on some conditions.
• Is a special case of conditional join.
• When a theta join uses only equivalence condition, it
become a equi join
• As value of two attributes will be equal in result of
equaijoin, only one attribute will be appeared in
result.
Thank you

Relation algebra (1).ppt

  • 1.
    Relational Algebra V.Deepa M.Sc.,M.Phil., AssistantProfessor Department of Computer Science Sri Ramakrishna College of Arts and Science
  • 2.
    Relational Algebra • Fiveoperators: • Union:  • Difference: - • Selection: s • Projection: P • Cartesian Product:  • Derived operators: • Intersection, complement • Joins (natural,equi-join, theta join, semi-join) • Renaming: r
  • 3.
    DBMS • Database ManagementSystem or DBMS in short refers to the technology of storing and retrieving users data.
  • 4.
    What is dataand Information?
  • 5.
    How relational algebrarelated to DBMS? • Relational algebra mainly provides theoretical foundation for relational databases
  • 7.
    Introduction to RelationAlgebra • It was introduction by 1970 • By EF Code who is the father of DBMS. • It is called procedural or formal query language.
  • 8.
    Relational Algebra • Framefor creating new relations from existing ones • Its place in the big picture: Declartive query language Algebra Implementation SQL, relational calculus Relational algebra Executable code
  • 9.
    1. Union and2. Difference • R1  R2 • Example: • ActiveEmployees  RetiredEmployees • R1 – R2 • Example: • AllEmployees -- RetiredEmployees
  • 10.
    What about Intersection? • It is a derived operator • R1  R2 = R1 – (R1 – R2) • Also expressed as a join (will see later) • Example • UnionizedEmployees  RetiredEmployees
  • 11.
    3. Selection • Returnsall tuples which satisfy a condition • Notation: sc(R) • Examples • sSalary > 40000 (Employee) • sname = “Smith” (Employee) • The condition c can be =, <, , >, , <>
  • 12.
    sSalary > 40000(Employee) SSN Name Salary 1234545 John 200000 5423341 Smith 600000 4352342 Fred 500000 SSN Name Salary 5423341 Smith 600000 4352342 Fred 500000
  • 13.
    4. Projection • Eliminatescolumns, then removes duplicates • Notation: P A1,…,An (R) • Example: project social-security number and names: • P SSN, Name (Employee) • Output schema: Answer(SSN, Name)
  • 14.
    P Name,Salary (Employee) SSNName Salary 1234545 John 200000 5423341 John 600000 4352342 John 200000 Name Salary John 20000 John 60000
  • 15.
    5. Cartesian Product •Each tuple in R1 with each tuple in R2 • Notation: R1  R2 • Example: • Employee  Dependents • Very rare in practice; mainly used to express joins
  • 16.
    Cartesian Product Example Employee NameSSN John 999999999 Tony 777777777 Dependents EmployeeSSN Dname 999999999 Emily 777777777 Joe Employee x Dependents Name SSN EmployeeSSN Dname John 999999999 999999999 Emily John 999999999 777777777 Joe Tony 777777777 999999999 Emily Tony 777777777 777777777 Joe
  • 17.
    Relational Algebra • Fiveoperators: • Union:  • Difference: - • Selection: s • Projection: P • Cartesian Product:  • Derived or auxiliary operators: • Intersection, complement • Joins (natural,equi-join, theta join, semi-join) • Renaming: r
  • 18.
    Operations on Bags Abag = a set with repeated elements All operations need to be defined carefully on bags • {a,b,b,c}{a,b,b,b,e,f,f}={a,a,b,b,b,b,b,c,e,f,f} • {a,b,b,b,c,c} – {b,c,c,c,d} = {a,b,b,c,d} • sC(R): preserve the number of occurrences • PA(R): no duplicate elimination • Cartesian product, join: no duplicate elimination Important ! Relational Engines work on bags, not sets !
  • 19.
    Joins • Joins isa combinatio of Cartesian product by a selection process. • Inner Joins: Theta join EQUI join Natural join • Outer joins: Left outer join Right outer join Full outer join
  • 20.
    • Also knownas conditional join. • Used when you want to join two or more relation based on some conditions.
  • 21.
    • Is aspecial case of conditional join. • When a theta join uses only equivalence condition, it become a equi join • As value of two attributes will be equal in result of equaijoin, only one attribute will be appeared in result.
  • 22.