Download presentation

Presentation is loading. Please wait.

Published byCamryn Oborn Modified about 1 year ago

1
Relational Algebra (RA)

2
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) Relational Algebra 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 SidSnameSaddressPhno 011 ARameshSagar 1111 012 AAjithSagar 3333 013 ASajiSS 4444 014 ARamaRR 5555 SidSnameSaddressPhno 011 ARameshSagar1111 012 AAjithSagar3333

7
Relational Algebra (RA) Relational-oriented operation Selection (σ) Select those rows of Account relation whose Amt > 8000 σ Amt>8000 (Account) Output Account AcnoBr-NameAmt AAPTA-Center7000 ABPTA-North8000 ACPTA-South1000 ADPTA-West9000 AcnoBr-NameAmt ADPTA-West9000

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 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 Person Person Name,Hobby (Person) 1123 John 123 Main stamps 1123 John 123 Main coins 5556 Mary 7 Lake Dr hiking 9876 Bart 5 Pine St stamps John stamps John coins Mary hiking Bart stamps Id Name Address Hobby Name Hobby

14
Relational Algebra (RA) Projection ( ) Example: Person Person Person Name,Address (Person) Result is a table (no duplicates) 1123 John 123 Main stamps 1123 John 123 Main coins 5556 Mary 7 Lake Dr hiking 9876 Bart 5 Pine St stamps John 123 Main Mary 7 Lake Dr Bart 5 Pine St Id Name Address Hobby Name Address

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 1123 John 123 Main stamps 1123 John 123 Main coins 5556 Mary 7 Lake Dr hiking 9876 Bart 5 Pine St stamps 1123 John 9876 Bart Id Name Address Hobby Id Name

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 ( ) union compatible 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. D egree 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 SidSnameSaddress 011 ARameshSagar 012 AAjithPOPO Student 2 SidSnameSaddress 013 ASajiSS 014 ARamaRR SidSnameSaddress 011 ARameshSagar 012 AAjithPOPO 013 ASajiSS 014 ARamaRR

22
Relational Algebra (RA) Set-operations Union ( ) Find R S with duplicates removed.

23
Relational Algebra (RA) Set-operations Union ( ) Example (relation) (relation) To find the name of all students who are in either student1 or in student2 sname (Student1) sname (Student2) Output Sname Ramesh Ajith Saji Rama Student 1 SidSnameSaddress 011 ARameshSagar 012 AAjithPOPO Student 2 SidSnameSaddress 013 ASajiSS 014 ARamaRR

24
Relational Algebra (RA) Set-operation Intersection ( ) R1 R2 returns the set of tuples that are both in R1 and R2. compatible 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 SidSnameSaddress 013 ASajiSS 012 AAjithPOPO Student 2 SidSnameSaddress 011 ARameshSagar 012 AAjithPOPO SidSnameSaddress 012 AAjithPOPO

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 (relation) (relation To find the name of all students who are both in student1 and student2 sname (Student1) sname (Student2) Output Sname Ajith Student 1 SidSnameSaddress 013 ASajiSS 012 AAjithPOPO Student 2 SidSnameSaddress 011 ARameshSagar 012 AAjithPOPO

28
Relational Algebra (RA) Set-operations Set Difference (-) R1 - R2 returns the set of tuples that are in R1, but not in R2 compatible 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 SidSnameSaddress 011 ARameshSagar 012 AAjithPOPO 013 ASajiSS Student 2 SidSnameSaddress 013 ASajiSS 014 ARamaRR SidSnameSaddress 011 ARameshSagar 012 AAjithPOPO

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, 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 x2 y1 y2 x1 x2 y1 y2 x3 x4 y3 y4 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 NameDept AjiCSE JinsonMCA ShellyMCA JubyMCA Emp Name1Name2 AjiCSE JinsonMCA ShellyMCA JubyMCA

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 AB 34 57 CD 27 68 ABCD 3427 5727 ABCD 3427 3468 5727 5768 ABCD 3427 3468 5727 5768

39
Relational Algebra (RA) Join ( ) Theta join less than join R S R X S R B

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 SidnameCourse 100AnkitaCSE 200MonuIT 300KalpnaIT Coursename CSEComputer Science ITInformation Technology SidnameStudent.coursecourses.coursename 100AnkitaCSE Computer Science 200MonuIT Information Technology 300KalpnaIT Information Technology SidnameCourse name 100AnkitaCSE Computer Science 100AnkitaCSEITInformation Technology 200MonuITCSEComputer Science 200MonuIT Information Technology 300KalpnaITCSEComputer Science 300KalpnaIT Information Technology SidnameCourse name 100AnkitaCSE Computer Science 100AnkitaCSEITInformation Technology 200MonuITCSEComputer Science 200MonuIT Information Technology 300KalpnaITCSEComputer Science 300KalpnaIT Information Technology

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 AB 34 57 CD 27 68 ABCD 5727 ABCD 3427 3468 5727 5768 ABCD 3427 3468 5727 5768

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 Transcript (StudId, CrsCode, Sem, Grade) Teaching ( Teaching (ProfId, CrsCode, Sem) TranscriptTeaching Transcript Teaching = StudId, Transcript.CrsCode, Transcript.Sem, Grade, ProfId Transcript Teaching ) ( 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) AB 12 34 BD 45 48 67 ABD 345 348 ABBD 1245 1248 1267 3445 3448 3467 ABBD 1245 1248 1267 3445 3448 3467

46
Relational Algebra (RA) Natural join R S R X S R S = R. Name,Dept, Place( R R.Name=S.Name S) Namedept AjiCSE sajiIT namePlace AjiPTA sajiKollam RajeshKollam NamedeptNamePlace AjiCSEAjiPTA AjiCSEsajiKollam AjiCSERajeshKollam sajiITAjiPTA sajiITsajiKollam sajiITRajeshKollam NamedeptNamePlace AjiCSEAjiPTA AjiCSEsajiKollam AjiCSERajeshKollam sajiITAjiPTA sajiITsajiKollam sajiITRajeshKollam NamedeptPlace AjiCSEPTA sajiITKollam

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 namedept KalpnaCSE JyotiIT nameAdd KalpnaBhopal AnjaliIndore RashmiKollam NamedeptnameAdd KalpnaCSEKalpnaBhopal KalpnaCSEAnjaliIndore KalpnaCSERashmiKollam JyotiITKalpnaBhopal JyotiITAnjaliIndore JyotiITRashmiKollam NamedeptnameAdd KalpnaCSEKalpnaBhopal KalpnaCSEAnjaliIndore KalpnaCSERashmiKollam JyotiITKalpnaBhopal JyotiITAnjaliIndore JyotiITRashmiKollam namedeptAdd KalpnaCSEBhopal

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 namedeptAdd KalpnaCSEBhopal JyotiITNULL

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 namedeptAdd KalpnaCSEBhopal AnjaliNULLIndore RashmiNULLKollam

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 namedeptAdd KalpnaCSEBhopal JyotiITNULL AnjaliNULLIndore RashmiNULLKollam

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 (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 AB joexyz joepqr jimlmn tomabc joelmn joeabc joeabc simb3 A xyz pqr lmn abc AB joexyz joepqr jimlmn tomabc joelmn joeabc joeabc simb3 joe

62
Division R S R / S Relational Algebra (RA) AB a1b1 a2b1 a3b1 a4b1 a1b2 a3b2 a2b3 a3b3 a4b3 a1b4 a2b4 a3b4 A a1 a2 a3 B b1 b4 AB a1b1 a2b1 a3b1 a4b1 a1b2 a3b2 a2b3 a3b3 a4b3 a1b4 a2b4 a3b4

63
Relational Algebra (RA)

64
Projection Example Display the column salary Display nr & Salary

65
Relational Algebra (RA) Selection Example Display all rows, salary <200 Display all rows salary =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

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google