Presentation is loading. Please wait.

Presentation is loading. Please wait.

Relational Algebra.

Similar presentations


Presentation on theme: "Relational Algebra."— Presentation transcript:

1 Relational Algebra

2 What do we mean by an "algebra"?
"Ordinary" algebra works on numbers, and contains operators that work on numbers: 2+4, 6-3, 95/5, 8*7. Relational Algebra works on relations, and has operators which work on relations (tables).

3 Relational Algebra Algebra is a formal structure consisting of sets and operations on those sets. Relational algebra is a formal system for manipulating relations.

4 Relational Algebra A language based on operators and a domain of values Operators map values taken from the domain into other domain values Hence, an expression involving operators and arguments produces a value in the domain When the domain is a set of all relations (and the operators are as described later), we get the relational algebra We refer to the expression as a query and the value produced as the query result

5 Relational Algebra-Terminology
Domain: set of relations Basic operators: select, project, union, set difference, Cartesian product and rename Derived operators: set intersection, division, join Procedural: Relational expression specifies query by describing an algorithm (the sequence in which operators are applied) for determining the result of an expression

6 Select Operation() Syntax for the Select operator is  (relation)
predicate

7 What does the Select operator do?
The Select operator, s , selects tuples (rows) from the relation which satisfy (make TRUE) the specified predicate. So, we start with a given relation, and produce a new relation which is a (horizontal) subset of the old one, depending upon which rows are extracted from the old relation.

8 The STUDENT Relation The query s (STUDENT) STUID = 241504 STUNAME
MAJOR CREDITS 151234 Jones,Ted MGMT 47 241504 Smith, Jane CS 101 310927 Kool, Bill MATH 68 428541 Lan, Jackie IS 93 529624 Witt, Stu COE 75 629632 Kahn, Fran CS 65 The query s (STUDENT) STUID = STUID STUNAME MAJOR CREDITS 241504 Smith, Jane CS 101

9 s (Student) STUNAME MAJOR CREDITS 151234 Jones,Ted MGMT 47 241504
STUID STUNAME MAJOR CREDITS 151234 Jones,Ted MGMT 47 241504 Smith, Jane CS 101 310927 Kool, Bill MATH 68 428541 Lan, Jackie IS 93 529624 Witt, Stu COE 75 629632 Kahn, Fran CS 65 s (Student) MAJOR = 'CS' STUID STUNAME MAJOR CREDITS 241504 Smith, Jane CS 101 629632 Kahn, Fran CS 65

10 The 'Usual' Comparisons are Allowed in the Predicate
We may include the "usual" comparison operators ( < , > , =, etc.) in the predicate. We can form "compound" predicates with an AND ( L ) and the OR ( V ).

11  ( employee) - it will select rows having salary > 500
Selection ( ) eno ename sal desig IT1 ALI 500 TUTOR BUS2 AHMED 1000 HEAD IT2 SABA 400 CLERK IT3 SALEH BUS1 BADER 650  ( employee) it will select rows having salary > 500 sal > 500 eno ename sal desig BUS2 AHMED 1000 HEAD BUS1 BADER 650 TUTOR

12 Selection Condition Operators: <, , , >, =, 
Simple selection condition: <attribute> operator <constant> <attribute> operator <attribute> <condition> AND <condition> <condition> OR <condition> NOT <condition>

13 s (Student) STUID STUNAME MAJOR CREDITS 428541 Lan, Jackie IS 93
151234 Jones,Ted MGMT 47 241504 Smith, Jane CS 101 310927 Kool, Bill MATH 68 428541 Lan, Jackie IS 93 529624 Witt, Stu COE 75 629632 Kahn, Fran CS 65 s (Student) ((MAJOR = 'CS' ) L (CREDITS < 100))V(MAJOR='IS') STUID STUNAME MAJOR CREDITS 428541 Lan, Jackie IS 93 629632 Kahn, Fran CS 65

14 The Project Operator p a unary operator (works on a single relation)
produces a vertical subset of a relation, extracting the values of the attributes we specify, eliminating duplicates, and placing the values in a new relation

15 p (relation) The Project Operator, p
attribute(1),attribute(2),...attribute(n) This will produce a list of the attribute values given, for all entities in the specified relation.

16 p (Student) STUNAME, MAJOR Query states : What are the names and majors of all students? STUNAME MAJOR Jones,Ted MGMT Smith, Jane CS Kool, Bill MATH Lan, Jackie IS Witt, Stu COE Kahn, Fran CS

17 p ( s (student)) STUNAME STUID Lan, Jackie 428541
Use the Relational Algebra to write a query which lists the names and id's of all students who are IS majors p ( s (student)) STUNAME, STUID MAJOR='IS' The innermost (select expression) produces: STUID STUNAME MAJOR CREDITS 428541 Lan, Jackie IS 93 The "project" is applied to this relation, yielding: STUNAME STUID Lan, Jackie 428541

18 Set Operators Relation is a set of tuples => set operations should apply Result of combining two relations with a set operator is a relation => all its elements must be tuples having same structure Hence, scope of set operations limited to union compatible relations

19 Union Compatible Relations
Two relations are union compatible if Both have same number of columns Type of attributes are the same in both Attributes with the same name in both relations have the same domain Union compatible relations can be combined using union, intersection, and set difference.

20 Examples of Union Compatibility and Non-Compatibility
Compatible (assuming expected domains): INVENTORY (Inumber, Description, SalesPrice) ORDER (Partnumber, Description, Price) And EMPLOYEE (Enumber, Name, Dept) VACATION_SCHEDULE(Number, Name, Dept-To-Be-Billed) Non-Compatible: INVENTORY (Pnum, Desc, Quantity-In-Stock) ORDER (Pnum, Desc, Supplier) And INVENTORY (Part, Description) ORDER( Pnum, Desc, Supplier)

21 Union Union Operator ... U a binary operator, having usual definition for Union: A U B consists of tuples (rows) in either A or B or both (duplicate rows eliminated)

22 Union ( ) Union compatible rule :-
1.Two relations should have same degree 2. Two relation corresponding same type of column domain. Bus-Student (Degree = 4 Cardinality = 3 IT-Student (Degree = 4 Cardinality = 3) Eno Ename addr gsm i1 ali ruwi i25 bushra nizwa I34 saleh seeb Eno Ename addr gsm B45 bilal wadi B2 saleh seeb aliya jebroo

23 Union ( ) Union returns those tuples that are either or both of two relations. IT-Student U Bus-Student (Degree = 4 Cardinality =6) Eno Ename addr gsm i1 ali ruwi i25 bushra nizwa I34 aliya seeb B45 bilal wadi B2 saleh jebroo

24 Example of Union Operator U
Consider the following relations and instances: M: MANUFACTURERS (Name, PeopleCount, Revenue) C: COMPANY (Name, NumberEmployees, Sales) IBM 50,000 9,000,000,000 Univac 25,000 500,000,000 Microsoft 35,000 1,500,000,000 M: IBM 50,000 9,000,000,000 Univac 25,000 500,000,000 C: IBM 50,000 9,000,000,000 Univac 25,000 500,000,000 Microsoft 35,000 1,500,000,000 M U C :

25 Intersection Represented by R1  R2
Produces a relation that includes all the tuples in both R1 and R2; R1 and R2 must be union compatible.

26 A B S# SNAME STATUS CITY S1 Smith 20 London S4 Clark S# SNAME STATUS

27 A  B S# SNAME STATUS CITY S1 Smith 20 London S4 Clark

28 Example of Intersection Operator 
Consider the following relations and instances: M: MANUFACTURERS (Name, PeopleCount, Revenue) C: COMPANY (Name, NumberEmployees, Sales) IBM 50,000 9,000,000,000 Univac 25,000 500,000,000 Microsoft 35,000 1,500,000,000 M: IBM 50,000 9,000,000,000 Univac 25,000 500,000,000 C: M  C IBM 50,000 9,000,000,000 Univac 25,000 500,000,000

29 Difference Represented by R1 - R2:
Produces a relation that includes all the tuples in R1 that are not in R2; R1 and R2 must be union compatible.

30 Set-difference (–) The minus operator returns only records which are only in the first relation. students Sports students rollno name 22045 saleh 22056 arif 22345 ahmed 23356 majid 23456 anwar rollno name 22045 saleh 22056 arif 23456 anwar students-sports students rollno name 22345 ahmed 23356 majid

31 Example of Difference Operator -
Consider the following relations and instances: M: MANUFACTURERS (Name, PeopleCount, Revenue) C: COMPANY (Name, NumberEmployees, Sales) IBM 50,000 9,000,000,000 Univac 25,000 500,000,000 Microsoft 35,000 1,500,000,000 M: IBM 50,000 9,000,000,000 Univac 25,000 500,000,000 C: M – C : Microsoft 35,000 1,500,000,000

32 Cartesian product Represented by R1 X R2:
Produces a relation that has the attributes of R1 and R2 and includes as tuples all possible combinations of tuples from R1 and R2. Build a relation from two specified relations consisting of all possible combinations of tuples, one from each of the two relations.

33 CROSS PRODUCT (X) or CARTESIAN Product
It takes 2 relations and it returns new relation where every record of first relation is combined with every record of second relation. EMP  (Degree = 5 Cardinality =4) DEPT  (Degree = 3 Cardinality = 3) Eno Ename sal Desig Dno E1 ali 500 tutor 10 E2 ahmed 1000 head E3 saleh 700 20 E4 sami 400 clerk 30 Dno Dname Loc 10 Business B2 20 Information Tech. B1 30 Foundation C1

34 EMPDEPT Degree = 5+3 = 8 Cardinality = 4*3 = 12 )
Eno Ename sal Desig Dno Dname Loc E1 ali 500 tutor 10 Business B2 20 Information Tech. B1 30 Foundation C1 E2 ahmed 1000 head E3 saleh 700 E4 sami 400 clerk

35 The Natural JOIN Operator in The Relational Algebra
"the heart of the relational algebra" a binary operator we "join" together two tables, based on a common attribute (the Join attribute) the new relation will contain the columns of both tables which have been joined, and its rows will be the concatenation of a row from the first table and a row from the second table which matches the Join attribute

36 The Natural Join Symbol: Ä
A ÄB means the Natural Join of relations A and B Note that if there is a tuple in relation A in which its attribute to be joined does not match any value of the attribute to be joined in relation B, it will not appear in the joined relation.

37 PC PC Ä EMPLOYEE would then be: EMPLOYEE COMPID EMPNUM 32808 M759 611
TAGNUM COMPID EMPNUM EMPNUM EMPNAME 32808 M759 611 124 Alvarez, R. 37691 B121 124 258 Lopez, M. 57772 C007 567 567 Feinstein, B. 59836 B221 124 77740 M759 567 611 Dinh, M. 80269 C007 852 PC Ä EMPLOYEE would then be: TAGNUM COMPID EMPNUM EMPNAME 32808 M759 611 Dinh. M. 37691 B121 124 Alvarez, R. 57772 C007 567 Feinstein, B. 59836 B221 124 Alvarez, R. 77740 M759 567 Feinstein, B.

38 List the tag number and computer ID together with the name of the employee to whom the PC is assigned. p ( PC Ä EMPLOYEE ) TAGNUM, COMPID, EMPNAME

39 Answer to the Query TAGNUM COMPID EMPNAME 32808 M759 Dinh. M. 37691
B121 Alvarez, R. 57772 C007 Feinstein, B. 59836 B221 Alvarez, R. 77740 M759 Feinstein, B.


Download ppt "Relational Algebra."

Similar presentations


Ads by Google