Download presentation

Presentation is loading. Please wait.

1
**Relational Algebra (RA)**

2
**Relational Algebra (RA)**

A query language Language in which a user requests information from the database Query language allow manipulation and retrieval of data from database Relational Algebra A collection of operations on Relations. Relations are operands and the result of an operation is another relation.

3
**Relational Algebra (RA)**

Collection of formal operations acting on relations and producing relations as result Relational algebraic operations, divided into 2 Set-oriented operations Relational-oriented operations

4
**Relational Algebra (RA)**

Relational-oriented operations include Select Project Rename Join Division Set-oriented operations includes Union Intersection Set difference Cartesian-product

5
**Relational Algebra (RA)**

Relational-oriented operation Selection (σ) It select a subset of tubles (rows) from a relation that satisfy some condition Syntax: condition (relation) σ :- for select operation Condition :- attribute name or any condition Relation:- relation (table) unary operation because it works on one relation

6
**Relational Algebra (RA)**

Relational-oriented operation Selection (σ) Example Select those rows of the student relation where the Saddress is ‘Sagar’ σ Saddress=“Sagar” (Student) Output Student Sid Sname Saddress Phno 011 A Ramesh Sagar 1111 012 A Ajith 3333 013 A Saji SS 4444 014 A Rama RR 5555 Sid Sname Saddress Phno 011 A Ramesh Sagar 1111 012 A Ajith 3333

7
**Relational Algebra (RA)**

Relational-oriented operation Selection (σ) Select those rows of Account relation whose Amt > 8000 σ Amt>8000 (Account) Output Account Acno Br-Name Amt AA PTA-Center 7000 AB PTA-North 8000 AC PTA-South 1000 AD PTA-West 9000 Acno Br-Name Amt AD PTA-West 9000

8
**Relational Algebra (RA)**

Relational-oriented operation Selection (σ) σC(R) Returns only those tuples in R that satisfy condition C A condition C can be made up of any combination of comparison or logical operators that operate on the attributes of R. Comparison operators:<, , , >, =, Logical operators: ^ “and”, V “or”, ¬ ”not” Examples Id>3000 V Hobby=‘painting’(Person) Id>3000 Or Hobby=‘painting’(Person) Id>3000 ^ Id <3999 (Person) Id>3000 AND Id <3999 (Person) ¬ (Hobby=‘dancing’) (person) NOT(Hobby=‘dancing’) (person)

9
**Relational Algebra (RA)**

Selection (σ) Assume the following relation EMP has the following tuples Select only those Employees in the CS department: σ Dept = 'CS' (EMP) Result:

10
**Relational Algebra (RA)**

Selection (σ) Select only those Employees who are either Assistant Professors or in the Economics department: σ Rank = 'Assistant' V Dept = 'Econ' (EMP) Result:

11
**Relational Algebra (RA)**

Selection (σ) Select only those Employees who are not in the CS department or Associate: σ ¬ (Rank = ‘Associate' V Dept = 'CS') (EMP)

12
**Relational Algebra (RA)**

Projection () Produces subset of columns of table Unary operator ie it works on a single table Select all attributes (columns) that satisfy some condition Syntax attribute list(relation) projection operation Attribute-list attributes of the relation/table Relation relation/table

13
**Relational Algebra (RA)**

Projection () Example: Person Name,Hobby(Person) Id Name Address Hobby Name Hobby John Main stamps John Main coins Mary 7 Lake Dr hiking Bart 5 Pine St stamps John stamps John coins Mary hiking Bart stamps

14
**Relational Algebra (RA)**

Projection () Example: Person Name,Address(Person) Result is a table (no duplicates) Id Name Address Hobby Name Address John Main Mary 7 Lake Dr Bart 5 Pine St John Main stamps John Main coins Mary 7 Lake Dr hiking Bart 5 Pine St stamps

15
**Relational Algebra (RA)**

Projection () Project only the names and departments of the employees: name, dept (EMP)

16
**Relational Algebra (RA)**

Selection + Projection Person result Display Id & Name, whose Hobbies are either stamps or coins Id Name Address Hobby Id Name John Main stamps John Main coins Mary 7 Lake Dr hiking Bart 5 Pine St stamps John Bart

17
**Relational Algebra (RA)**

Selection + Projection Show the names of all employees working in the CS department: name σ ( Dept = 'CS' (EMP) ):

18
**Relational Algebra (RA)**

Set-operations Binary operation from two relations and build third relation The operations are union, intersection, set-difference & cartesian-product

19
**Relational Algebra (RA)**

Set-operations Union () R1 R2 returns the set of tuples that are in R1 or R2. Builds a relation from rows appearing in either or both of the specified relation It takes the set of rows in each relation and combine them Eliminate duplicates

20
**Relational Algebra (RA)**

Set-operations Union () Two relations are union compatible if Both have same number of columns Names of attributes are the same in both Attributes with the same name in both relations have the same domain OR Two relations R and S are union compatible if and only if they have the same degree and the domains of the corresponding attributes are the same. Domain is the data type and size of an attribute. Degree of relation R is the number of attributes it contains.

21
**Relational Algebra (RA)**

Set-operations Union () Relations student1 & student2 Both of the tables are union compatible, they having same types of columns Student1 student 2 Student 1 Sid Sname Saddress 011 A Ramesh Sagar 012 A Ajith POPO Student 2 Sid Sname Saddress 013 A Saji SS 014 A Rama RR Sid Sname Saddress 011 A Ramesh Sagar 012 A Ajith POPO 013 A Saji SS 014 A Rama RR

22
**Relational Algebra (RA)**

Set-operations Union () Find R S with duplicates removed.

23
**Relational Algebra (RA)**

Set-operations Union () Example <attribute name> (relation) <attribute name> (relation) To find the name of all students who are in either student1 or in student2 sname (Student1) sname (Student2) Output Student 1 Sid Sname Saddress 011 A Ramesh Sagar 012 A Ajith POPO Student 2 Sid Sname Saddress 013 A Saji SS 014 A Rama RR Sname Ramesh Ajith Saji Rama

24
**Relational Algebra (RA)**

Set-operation Intersection () R1 R2 returns the set of tuples that are both in R1 and R2. Two relations are Intersection compatible if Both have same number of columns Names of attributes are the same in both Attributes with the same name in both relations have the same domain

25
**Relational Algebra (RA)**

Set-operations Intersection () Relations student1 & student2 Both of the tables are intersection compatible, they having same types of columns Student1 student 2 Student 1 Sid Sname Saddress 013 A Saji SS 012 A Ajith POPO Student 2 Sid Sname Saddress 011 A Ramesh Sagar 012 A Ajith POPO Sid Sname Saddress 012 A Ajith POPO

26
**Relational Algebra (RA)**

Set-operations Intersection () Find R S Relation with tuples that appear in both R and S.

27
**Relational Algebra (RA)**

Set-operations Intersection () Example <attribute name> (relation) <attribute name> (relation To find the name of all students who are both in student1 and student2 sname (Student1) sname (Student2) Output Student 1 Sid Sname Saddress 013 A Saji SS 012 A Ajith POPO Student 2 Sid Sname Saddress 011 A Ramesh Sagar 012 A Ajith POPO Sname Ajith

28
**Relational Algebra (RA)**

Set-operations Set Difference (-) R1 - R2 returns the set of tuples that are in R1, but not in R2 Two relations are difference compatible if Both have same number of columns Names of attributes are the same in both Attributes with the same name in both relations have the same domain

29
**Relational Algebra (RA)**

Set-operations Set difference (-) Relations student1 & student2 Both of the tables are difference compatible, they having same types of columns Student1 - student 2 Student 1 Sid Sname Saddress 011 A Ramesh Sagar 012 A Ajith POPO 013 A Saji SS Student 2 Sid Sname Saddress 013 A Saji SS 014 A Rama RR Sid Sname Saddress 011 A Ramesh Sagar 012 A Ajith POPO

30
**Relational Algebra (RA)**

Set-operations Set difference (-) Find R - S Result: Relation with tuples from R but not from S

31
**Relational Algebra (RA)**

Set-operation Cartesian- product (X) If R and S are two relations, R S is the set of all concatenated tuples <x,y>, where x is a tuple in R and y is a tuple in S Also called cross product or cross join a b c d a b c d x1 x y1 y x1 x2 y1 y2 x3 x y3 y x1 x2 y3 y4 x3 x4 y1 y2 R S x3 x4 y3 y4 R S

32
**Relational Algebra (RA)**

Set-operation Cartesian- product (X) Find R X S Produce all combinations of tuples from two relations

33
**Relational Algebra (RA)**

Rename operation ( ) Represented by lower letter rho ( ) It improves the red ability We can rename relation and attribute Syntax P(A1,A2,A3,..An) (Relation) Eg Emp(Name ,Dept) P(Name1,Name2)(EmP) Emp Name Dept Aji CSE Jinson MCA Shelly Juby Emp Name1 Name2 Aji CSE Jinson MCA Shelly Juby

34
**Relational Algebra (RA)**

Rename operation (P) Rename relation P(Emp1)(EMP) rename the relation EMP to Emp1

35
**Relational Algebra (RA)**

Join ( ) Binary operation Combine two relations Retrieve columns or rows from more than one table simultaneously satisfying some condition Three types of join Theta join Inner join Natural join

36
**Relational Algebra (RA)**

Join ( ) Theta join (condition join) Join two relation with condition Use one of the comparison operators(=,<,>,<=,>=,<>) and specify the join condition If the join condition is = then the operation is called equijoin

37
**Relational Algebra (RA)**

Join ( ) Theta join (condition join) If R has a set of attributes A1,A2,..An S has a set if attributes B1,B2,..Bm then the result of the join is a relation with n+m attributes(A1,A2,..An,B1,B2,..Bm) First compute catesian product RXS Then select those rows which satisfy the join condition

38
**Relational Algebra (RA)**

Join ( ) Theta join greater than join R S R X S R A>C S A B 3 4 5 7 C D 2 7 6 8 A B C D 3 4 2 7 6 8 5 A B C D 3 4 2 7 6 8 5 A B C D 3 4 2 7 5

39
**Relational Algebra (RA)**

Join ( ) Theta join less than join R S R X S R B<D S A B C 1 2 3 4 5 6 7 8 9 D E 3 1 6 2 A B C D E 1 2 3 6 4 5 7 8 9 A B C D E 1 2 3 6 4 5 7 8 9 A B C D E 1 2 3 6 4 5

40
**Relational Algebra (RA)**

Equijoin or Inner join If condition is = (equijoin) Two relation connected with primary and foreign key R R.primary key = S.foregin key S

41
**Relational Algebra (RA)**

Equijoin or Inner join Relation Student & Courses Student X Course Student student.course=course.course Courses Sid name Course 100 Ankita CSE 200 Monu IT 300 Kalpna Course name CSE Computer Science IT Information Technology Sid name Course 100 Ankita CSE Computer Science IT Information Technology 200 Monu 300 Kalpna Sid name Course 100 Ankita CSE Computer Science IT Information Technology 200 Monu 300 Kalpna Sid name Student.course courses.course 100 Ankita CSE Computer Science 200 Monu IT Information Technology 300 Kalpna

42
**Relational Algebra (RA)**

Equijoin or Inner join Consider another example of relation R on B with S on D R S R X S R B=D S A B 3 4 5 7 C D 2 7 6 8 A B C D 3 4 2 7 6 8 5 A B C D 3 4 2 7 6 8 5 A B C D 5 7 2

43
**Relational Algebra (RA)**

Natural join Is Special case of equijoin In addition eliminate the duplicate columns Requires 2 relation with common column (P.K & F.K) It compute R X S (Cartesian product) It combines tuples which have equal values in attributes with the same name.

44
**Relational Algebra (RA)**

Natural join Automatically renames one copy of each common attribute before the cartesian product, and uses a projection to eliminate these double attributes at the end (duplicate columns eliminated from the result) Transcript (StudId, CrsCode, Sem, Grade) Teaching (ProfId, CrsCode, Sem) Transcript Teaching = StudId, Transcript.CrsCode, Transcript.Sem, Grade, ProfId ( Transcript Transcipt.CrsCode=Teaching.CrsCode AND Transcirpt.Sem=Teaching.Sem Teaching )

45
**Relational Algebra (RA)**

Natural join R S R X S R S = A,R.B,D ( R R.B=S.B S) A B 1 2 3 4 B D 4 5 8 6 7 A B D 1 2 4 5 8 6 7 3 A B D 1 2 4 5 8 6 7 3 A B D 3 4 5 8

46
**Relational Algebra (RA)**

Natural join R S R X S R S = R. Name,Dept, Place( R R.Name=S.Name S) Name dept Aji CSE saji IT name Place Aji PTA saji Kollam Rajesh Name dept Place Aji CSE PTA saji Kollam Rajesh IT Name dept Place Aji CSE PTA saji Kollam Rajesh IT Name dept Place Aji CSE PTA saji IT Kollam

47
**Relational Algebra (RA)**

Outer join Extension of join One of the major drawback of natural join is that it looses information about the tuples they do not match in both of the relation R and S Eg: Here we lost the information About ‘Jyoti’, ‘Anjali’, & ‘Rashmi’ Since they do not match in both relation Those tuples are called dangling tuples name dept Kalpna CSE Jyoti IT name Add Kalpna Bhopal Anjali Indore Rashmi Kollam Name dept name Add Kalpna CSE Bhopal Anjali Indore Rashmi Kollam Jyoti IT Name dept name Add Kalpna CSE Bhopal Anjali Indore Rashmi Kollam Jyoti IT name dept Add Kalpna CSE Bhopal

48
**Relational Algebra (RA)**

Outer join Same as natural join Avoid the loss of information Use NULL values to fill in dangling tuples Three type Left outer join ( ) Right outer join ( ) Full outer join ( )

49
**Relational Algebra (RA)**

Left outer join ( ) It takes all the tuples in the left relation that do not match with any tuple in the right Fill tuple with NULL values for all other attributes from the right relation and Adds them to the result of the natural join If we perform R S then all the information from the left relation present in the result name dept Add Kalpna CSE Bhopal Jyoti IT NULL

50
**Relational Algebra (RA)**

Right outer join ( ) It fills tuples from the right relation that do not match any other from left relation Fill tuple with NULL values for all other attributes from the left relation and Adds them to the result of the natural join If we perform R S then all the information from the left relation present in the result name dept Add Kalpna CSE Bhopal Anjali NULL Indore Rashmi Kollam

51
**Relational Algebra (RA)**

Full outer join ( ) It does both of those left & right Outer Join Fill tuples from the left relation that did not match any from the right relation as well as tuples from the right relation If we perform R S then all the information from the right and left relation is present in the result name dept Add Kalpna CSE Bhopal Jyoti IT NULL Anjali Indore Rashmi Kollam

52
**Relational Algebra (RA)**

Aggregate Functions SUM MINIMUM MAXIMUM AVERAGE, MEAN, MEDIAN COUNT

53
**Relational Algebra (RA)**

Aggregate Functions Aggregate operation is represented by “calli graphic G” Find the minimum Salary: G MIN (salary) (EMP)

54
**Relational Algebra (RA)**

Aggregate Functions Find the average Salary: G AVG (salary) (EMP)

55
**Relational Algebra (RA)**

Aggregate Functions Count the number of employees in the CS department: G COUNT (name) σ ( Dept = 'CS' (EMP) )

56
**Relational Algebra (RA)**

Aggregate Functions Find the total payroll for the Economics department: G SUM (salary) σ (Dept = 'Econ' (EMP) )

57
**Relational Algebra (RA)**

Modification of Database They are three Deletion Insertion Updating

58
**Relational Algebra (RA)**

Deletion Delete only one row Cannot delete values of only particular column Syn R R – E Eg To delete all rows from the relation salary, where employee=‘Ajay’ Salary Salary - Salary emp_name=‘Ajay’ Delete all rows where amount >=1500 and <2000 from salary Salary Salary - amount >= 1500 and amount <2000

59
**Relational Algebra (RA)**

Insertion To insert data into a relation Syn R R U E Eg Employee (name,age,salary) To insert a row of data Employee Employee U (‘saji’,34,5000)

60
**Relational Algebra (RA)**

Update Change values in a rows Syn R < A1,A2..An> (Relation) Eg To increment the salary of all employees with 500 Employee name,age,salary +500 (Employee)

61
**Relational Algebra (RA)**

Division Player(player_name,team_against, goals,venu) Details(venu,city,state) List the name of the player, who have scored goals in all of the Venus R= player_name,venu(Player) S= venu(Details) R S R / S We have 4 venus & Joe has score All of these venus A B joe xyz pqr jim lmn tom abc sim b3 A B joe xyz pqr jim lmn tom abc sim b3 A xyz pqr lmn abc joe

62
**Relational Algebra (RA)**

Division R S R / S A B a1 b1 a2 a3 a4 b2 b3 b4 A B a1 b1 a2 a3 a4 b2 b3 b4 A a1 a2 a3 B b1 b4

63
**Relational Algebra (RA)**

64
**Relational Algebra (RA)**

Projection Example Display the column salary Display nr & Salary

65
**Relational Algebra (RA)**

Selection Example Display all rows, salary <200 Display all rows salary< 200 & nr>=7

66
**Relational Algebra (RA)**

Selection & projection Example Display name & salary where salary< 200

67
**Relational Algebra (RA)**

Cartesian Product table E (for EMPLOYEE) table D (for DEPARTMENT)

68
**Relational Algebra (RA)**

Inner Join (Equivalent join) table E (for EMPLOYEE) table D (for DEPARTMENT) Join where dept=dnr

69
**Relational Algebra (RA)**

Aggregate functions Table E (for EMPLOYEE) Sum of salary

70
**Relational Algebra (RA)**

Aggregate functions Table E (for EMPLOYEE) count of salary Duplicates are not eliminated. Null values are ignored.

Similar presentations

OK

Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus.

Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on bluetooth based smart sensor networks inc Ppt on personal financial planning Human body systems for kids ppt on batteries Ppt on mauryan Ppt on c language fundamentals reviews Ppt on meningitis Ppt on off the job training methods Ppt on review of literature template Ppt on social entrepreneurship in india Ppt on design patterns in java