Presentation is loading. Please wait.

Presentation is loading. Please wait.

Relational Algebra The mathematical foundation for SQL Basic operators  select  project  union  intersect  set difference  cross product (i.e. Cartesian.

Similar presentations


Presentation on theme: "Relational Algebra The mathematical foundation for SQL Basic operators  select  project  union  intersect  set difference  cross product (i.e. Cartesian."— Presentation transcript:

1 Relational Algebra The mathematical foundation for SQL Basic operators  select  project  union  intersect  set difference  cross product (i.e. Cartesian product)  rename

2 Select Operation Notation:  p (r) Select only the records satisfying the condition p The condition p is composed of one or more terms connected by :  (and),  (or),  (not) each term is one of the following two forms: op where op is one of: =, , >, . <. 

3 Question 1 Relation r ABCD   1 5 12 23 7 3 10  C>D  A= B (r) What is the result of the following operation?

4 Project Operation Notation:  A1, A2, …, Ak (r) where A 1, A 2 are column names and r is a relation name. The result is defined as a new relation of k columns obtained by erasing the columns that are not listed Duplicate rows removed from result

5 Question 2 Relation r: ABC  10 20 30 40 11121112 What is the result of  A (r) ?

6 Union Operation Notation: r  s Meaning: The set composed of records appearing in r or s or both. r  s is well defined if and only if they are compatible in the following sense: 1. r, s have the same number of columns 2. The corresponding columns have same types (e.g., i-th column of r deals with the same type of values as does the i-th column of s)

7 Question 3 Relations r, s: What is the result of r  s? AB  131131 AB  1313 r s

8 Set Difference Operation Notation r – s Meaning: The set composed of records appearing in r but not in s. r  s is well defined if and only if they are compatible in the following sense: 1. r, s have the same number of columns 2. The corresponding columns have same types (e.g., i-th column of r deals with the same type of values as does the i-th column of s)

9 Question 4 Relations r, s: What is the result of r – s ? What is the result of r – (r – s) ? AB  131131 AB  1313 r s

10 Set-Intersection Operation Notation: r  s Meaning: The set composed of records appearing both in r or s. r  s is well defined if and only if they are compatible in the following sense: 1. r, s have the same number of columns 2. The corresponding columns have same types (e.g., i-th column of r deals with the same type of values as does the i-th column of s)

11 Questions 5 Relation r, s: What is the result of r  s? What is the result of r – (r- s)? What is the result of s – (s- r)? Are the three expressions above always equivalent to one another no matter what r and s are? A B  121121  2323 r s

12 Cross-Product Operation Notation r x s Defined as: r x s = {t q | t is a record in r and q is a record ins} In other words, r x s Is the set of all possible pairs of records with one from r and the other one from s to form a pair.

13 Question 6 Relations r, s: What is the result of r x s? AB  1212 CD  10 20 10 E XXYYXXYY r s

14 Rename Operation Allows us to name a resulting relation and its columns.  x (E) returns the relation E under the name X If a relation E has arity n, then  x (A1, A2, …, An) (E) returns the result of expression E under the name X, and with the columns renamed to A 1, A2, …., An.

15 Composing Expressions in Relational Algebra A basic expression in the relational algebra consists of :  Constants or A table as a relation in the database With any relational-algebra expressions E 1 and E 2,you can recursively compose more complex relational-algebra expressions as follow:   p (E 1 ), P is a logic condition on columns in E 1   s (E 1 ), S is a list consisting of some of the columns in E 1  E 1 x E 2   x (E 1 ), x is the new name for the result of E 1  E 1  E 2  E 1  E 2  E 1 - E 2

16 16 Relation: Students Jones Smith Curry Linda Fugitive sid 01 02 03 04 05 sname CA OR WA RI state Students columns records

17 17 Relation : Courses CS105 CS440 CS320 CS480 cid C++ Databases IC Design AI Research cname Yes No Yes No required courses columns records

18 18 Relation: Enrollment CS105 CS320 CS440 CS105 CS440 CS105 CS320 CS440 CS480 CS320 CS440 CS480 cid 01 02 03 04 sid 555344133445555344133445 grade enrollment columns records

19 Question 7 What are the results of the following relational-algebra expressions with respect to the three relations: courses, enrollment, and students 1.  sid (  grade<2 (enrollment)) 2.  cname (  required=“Yes” (courses) ) 3.  sid (  enrollment.cid = courses.cid and required=“Yes” ( enrollment x courses ))

20 Question 8 What are the results of the following relational-algebra expressions with respect to the three relation instances: courses, enrollment, and students 1.  sid (  enrollment.grade<2 and enrollment.cid = courses.cid and enrollment.required=“Yes” (enrollment x courses ) ) 2.  sid ( (  enrollment.cid = courses.cid (  grade<2 (enrollment) x  required=“Yes” (courses) ) )) 3.  sid (  grade<2 (enrollment)x  required=“Yes” (courses) )

21 Question 9 Assume that a student fails in a course if the grade is less than 2. Identify what expressions (if any) in questions 7 and 8 can provide the following information (given the relation instances: courses, enrollment, and students) : 1. The list of sids of students who fail in some required courses 2. The list of sids of students who fail in some courses 3. The list of sids of students who take some required courses

22 Question 10 What are the results of the following relational-algebra expressions with respect to the three relation instances: courses, enrollment, and students 1.  courses.cname (  enrollment.sid =“04 ” (  enrollment.cid = courses.cid (enrollment x courses))) 2.  cname ( courses ) –  courses.cname (  enrollment.sid =“04 ” (  enrollment.cid = courses.cid (enrollment x courses))) 3.  cname (  required=“Yes ” (courses )) –  courses.cname (  enrollment.sid =“04 ” and courses.required=“Yes ” (  enrollment.cid = courses.cid (enrollment x courses)))

23 Question 11 Assume that a student fails in a course if the grade is less than 2. Identify what expressions (if any) in question 10 can provide the following information (given the relation instances: courses, enrollment, and students) : 1. The list of courses taken by the student with the sid ’04’ 2. The list of courses that the student with the sid ’04’ hasn’t taken yet 3. The list of required courses that the student with the sid ’04’ hasn’t taken yet


Download ppt "Relational Algebra The mathematical foundation for SQL Basic operators  select  project  union  intersect  set difference  cross product (i.e. Cartesian."

Similar presentations


Ads by Google