Your SlideShare is downloading. ×
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

# Lecture 06 relational algebra and calculus

9,812

Published on

Published in: Education
2 Comments
4 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here
No Downloads
Views
Total Views
9,812
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
362
Comments
2
Likes
4
Embeds 0
No embeds

No notes for slide

### Transcript

• 1. Lecture 6 Relational Algebra And Calculus
• 2. Data Manipulation Languages
• In order for a database to be useful, it should be possible to store and retrieve information from it. This is the role of the data manipulation language.
• One of the attractions of the relational data model is that it comes with a well-defined data manipulation language.
• 3. Types of DML
• Two types of data manipulation languages
• Navigational (procedural)
• The query specifies (to some extent) the strategy used to find the desired result e.g. relational algebra.
• Non-navigational(non-procedural)
• The query only specifies what data is wanted, not how to find it e.g. relational calculus.
• 4. Relational Algebra
• Codd defined a number of algebraic operations for the relational model.
• Unary operations take as input a single table and produce as output another table.
• Binary operations take as input two tables and produce as output another table.
• 5. Types of Relational
• Traditional Set Operation
• Union
• Intersection
• Difference
• Cartesian product
• Special Operations
• Selection
• Projection
• Join
• Division
• 6. Unary Operations: Select
• Select produces a table that only contains the tuples that satisfy a particular condition, in other words a “horizontal” subset.
• Appearance:
•  C (R)  Sigma
• where C is a selection condition
• and R is the relation over which the selection takes place
• 7. Example of Select
• Student
• sid name addr
• 123 Fred 3 Oxford
• 345 John 6 Hope Rd.
• 567 Ann 5 Garden
•  sid > 300 (Student) yields
• 345 John 6 Hope Rd.
• 567 Ann 5 Garden
• 8. Unary Operations: Project
• Project produces a table consisting of only some of the attributes. It creates a “vertical” subset.
• Note that a project eliminates duplicates.
• Appearance:
• П A (R) П =Pi
• where A is a set of attributes of R
• and R is the relation over which the project takes place.
• 9. Example of Project
• Enrol
• sid cid grade
• 123 CS51T 76
• 234 CS52S 50
• 345 CS52S 55
• П cid (Enrol) yields
• CS51T
• CS52S
• 10. Binary Operations
• Two relations are (union) compatible if they have the same set of attributes.
• Example, one table may represent suppliers in one country, while another table with same schema represents suppliers in another country.
• For the union, intersection and set-difference operations, the relations must be compatible.
• 11. Union, Intersection, Set-difference
• R1   R2
• The union is the table comprised of all tuples in R1 or R2.
• R1  R2
• The intersection is the table comprised of all tuples in R1 and R2
• R1 - R2
• The set-difference between R1 and R2 is the table consisting of all tuples in R1 but not in R2.
• 12. Cartesian Product
• R1  R2
• The Cartesian product is the table consisting of all tuples formed by concatenating each tuple in R1 with a tuple in R2, for all tuples in R2.
• 13. Example of a Cartesian Product
• R1 A B
• 1 x
• 2 y
• R2 C D
• a s
• b t
• c u
• R1  R2 A B C D
• 1 x a s
• 1 x b t
• 1 x c u
• 2 y a s
• 2 y b t
• 2 y c u
• 14. Natural Join
• R1 R2
• Assume R1 and R2 have attributes A in common. Natural join is formed by concatenating all tuples from R1 and R2 with same values for A, and dropping the occurrences of A in R2
• R1 R2 = П A’ (  C ( R1  R2 ))
• where C is the condition that the values for R1 and R2 are the same for all attributes in A and A’ is all attributes in R1 and R2 apart from the occurrences of A in R2.
• hence, natural join is syntactic sugar
• 15. Example of a Natural Join I
• Course
• cid title eid
• CS51T DBMS 123
• CS52S OS 345
• CS52T Networking 345
• CS51S ES 456
• Instructor
• eid ename
• 123 Rao
• 345 Allen
• 456 Mansingh
• 16. Example of a Natural Join II
• Course Instructor
• cid title eid ename
• CS51T DBMS 123 Rao
• CS52S OS 345 Allen
• CS52T Net... 345 Allen
• CS51S ES 456 Mansingh
• 17. Division
• R1  R2
• Assume that the schema for R2 is a proper subset of the one for R1.
• We form the division by
• Ordering the tuples in R1 so that all the tuples with the same value for the non-common attributes are grouped together.
• Each group contributes a tuple to the result if the group’s values on the common attributes form a superset of the values of these attributes in R2.
• 18. Example of Division I
• Enrol cid sid grade
• CS51T 123 A
• CS52S 123 A
• CS51T 234 C
• CS52S 234 B
• CS51T 345 C
• CS52S 345 C
• Temp sid grade
• 123 A
• 234 B
• 19. Example of Division II
• Enrol cid sid grade
• CS51T 123 A
• CS51T 234 C
• CS51T 345 C
• CS52S 123 A
• CS52S 234 B
• CS52S 345 C
• Enrol  Temp cid
• CS52S
• Thus, the division gives all courses for which 123 got an A and 234 a B.
• 20. Assignment
• Allows the expression to be written in parts.
• Assigns the part to a temporary variable.
• This variable can be used in subsequent expressions.
• E.g.
•  sid (  title = ‘DBMS’ (Enrol Course)
• Could be re-written as:
• r Enrol Course
•  sid (  title = ‘DBMS’ (r))
• 21. Database Modification
• Insert
• r r E
• e.g.
• Course Course {(‘CS51T’,’DBMS’)}
• Delete
• r r - E
• e.g.
• Student Student -  sid=‘1’ (Student)
• Update
• r  F 1 ,F 2 ,…,F n (r)
• e.g.
• Enrol  sid,cid,grade grade + 2 (Enrol)
 
• 22. Examples
• Assume the following schema:
• Student( sid ,sname,saddr)
• Course( cid ,title,lid)
• Enrol( sid, cid , grade)
• Lecturer( lid ,lname,deptname)
• Query 1: Find the name of all students that have taken the course entitled ‘Expert Systems’.
• Query 2: Find the titles of all courses that student ‘Mark Smith’ has done.
• Query 3: Find the id of students that have enrolled in all the courses that lecturer with id. = ‘234’ has taught.
• Query 4: Find the highest grade for ‘CS51T’.
• 23. Relational Calculus
• A relational calculus expression defines a new relation in terms of other relations.
• A tuple variable ranges over a named relation. So, its values are tuples from that relation.
• Example:
• Get grades for CS51T
•  e(Enrol)
• {<e.grade>: e.cid = ‘CS51T’ }
• 24. Basic Syntax for Relational Calculus Expressions
•  r(R),…,  s(S)
• { <target> : predicate}
• where
• R,..,S are tables
• r,..,s are tuple variables
• target specifies the attributes of the resulting relation
• predicate is a formula giving a condition that tuples must satisfy to qualify for the resulting relation.
• 25. The Predicate
• Predicate is constructed from
• attribute names
• constants
• comparison operators
•      
• logical connectives
•     
• quantified tuple variables
•  t(R),  t(R)
• 26. Examples of Relational Calculus
• Example 2
• Get names and grades for students enrolled in CS51T
•  e(Enrol), s(Student)
• {<s.name, e.grade>:
• e.cid = ‘CS51T’ 
• s.sid = e.sid}
• In relation algebra
• П cid, name(  CID =‘ CS51T’ (Grade Student))
• 27. Example 3
• Give the names of all students who got at least one A.
•  s(Student)
• {<s.name>:
•  e(Enrol)
• (e.grade = ‘A’ 
• s.sid = e.sid)}
• Tuple variables not mentioned in the target list must be bound in the predicate.
• 28. Example 4
• Get the names of all students who only got A’s
•  s(Student)
• {<s.name>:
•  e(Enrol)( s.sid = e.sid  e.grade = ‘A’)
•  e2(Enrol) (s.sid = e2.sid)}
• 29. Example 5
• Get the names of all students who got an A and a B
•  s(Student)
• {<s.name>:
•  e(Enrol) (e.grade = ‘B’ 
• s.sid = e.sid)
•  e2(Enrol) (e2.grade = ‘A’ 
• s.sid = e2.sid)}
• 30. Example 6
• Get the course titles and names for the courses for which the student did not get an A
•  c(Course), s(Student)
• {<s.name, c.title>:
•  g(Enrol) s.sid = g.sid 
• g.cid = c.cid 
• g.grade  ‘A’}