Relational Algebra
Relational algebra Consists of collection of operators –Restrict, project, join, … Takes relations as operands Returns relations as results
E. Codd ’ s original algebra 8 operators –Traditional set operators union, intersection, difference, Cartesian product –Special relational operators restrict, project, join, divide
Restrict, Project Restrict –All tuples from a specified relation that satisfy a specified condition Project –All tuples that remains as subtuples in a specified relation after eliminating specified attributes
Product, union Product –Combination of tuples in two tables Union –All tuples that appear either or both of two relations a b c x y a x a y b x b y c x c y
Intersect, difference Intersect –All tuples that appear in both of two specified relations Difference –All tuples that appear in the first and not the second of two specified relations
Join (natural join) –All possible tuples that are combination of two relations such that two tuples contributing to any given combination have a common value for the common attributes –The common value appears just once. a1b1 a2b1 c1 b2c2 a1b1c1 a2b1c1
Divide Use two unary relations and one binary relation All tuples from a unary relation that appear in the binary relation matched with all tuples in the other unary relation a b x z ax ay az bx a
Closure property –Output of any operation is relation –So the output can be used as the input for further operation –Possible to make nested expression Type inference rule –If we know the type of input relation (heading), we can infer the type of output relation after operation
S, P, and SP S#SNAMESTATUSCITY S1 홍길동 20 서울 S2 손오공 10 부산 S3 박문수 30 부산 S4 신수동 20 서울 S5 반월성 30 경주 P#PNAMECOLORWEIGHTCITY P1 너트빨강 12.0 서울 P2 볼트초록 17.0 부산 P3 스크류파랑 17.0 광주 P4 스크류빨강 14.0 서울 P5 캠파랑 12.0 부산 P6 콕빨강 19.0 서울 S#P#QTY S1 … P1 P2 P3 P4 P5 P6 … … S P SP
RENAME operator Used to rename attributes Ex) S RENAME CITY AS SCITY –Creates the same relation with S except that the CITY attribute is named SCITY –See the tables below: S#SNAMESTATUSCITY S1 홍길동 20 서울 S2 손오공 10 부산 S3 박문수 30 부산 S4 신수동 20 서울 S5 반월성 30 경주 S#SNAMESTATUSSCITY S1 홍길동 20 서울 S2 손오공 10 부산 S3 박문수 30 부산 S4 신수동 20 서울 S5 반월성 30 경주
RENAME operator(cont.) Ex) P RENAME PNAME AS PN, WEIGHT AS WT –See the tables below: P#PNAMECOLORWEIGHTCITY P1 너트빨강 12.0 서울 P2 볼트초록 17.0 부산 P3 스크류파랑 17.0 광주 P4 스크류빨강 14.0 서울 P5 캠파랑 12.0 부산 P6 콕빨강 19.0 서울 P#PNCOLORWTCITY P1 너트빨강 12.0 서울 P2 볼트초록 17.0 부산 P3 스크류파랑 17.0 광주 P4 스크류빨강 14.0 서울 P5 캠파랑 12.0 부산 P6 콕빨강 19.0 서울
Semantics - union Given 2 relations A, B of the same type, A UNION B –A relation of the same type –Body of the relation consists of all tuples of A and B (no duplication) Ex) A UNION B S#SNAMESTATU S CITY S1 홍길동 20 서울 S4 신수동 20 서울 S#SNAMESTATU S CITY S1 홍길동 20 서울 S2 손오공 10 부산 S#SNAMESTATU S CITY S1 홍길동 20 서울 S4 신수동 20 서울 S2 손오공 10 부산
Semantics - intersect Given 2 relations A, B of the same type, A INTERSECT B –A relation of the same type –Body of the relation consists of all tuples of A and B that appear in both A and B Ex) A INTERSECT B S#SNAMESTATU S CITY S1 홍길동 20 서울 S4 신수동 20 서울 S#SNAMESTATU S CITY S1 홍길동 20 서울 S2 손오공 10 부산 S#SNAMESTATU S CITY S1 홍길동 20 서울
Semantics - minus Given 2 relations A, B of the same type, A MINUS B –A relation of the same type –Body of the relation consists of all tuples of A that appear in A and not in B Ex) A MINUS B S#SNAMESTATU S CITY S1 홍길동 20 서울 S4 신수동 20 서울 S#SNAMESTATU S CITY S1 홍길동 20 서울 S2 손오공 10 부산 S#SNAMESTATU S CITY S4 신수동 20 서울
Semantics - Times Given 2 relations A, B of headings –{A 1 :a 1, A 2 :a 2, …, A m :a m }, –{B 1 :b 1, B 2 :b 2, …, B n :b n } A TIMES B –A relation of the heading, {A 1 :a 1, A 2 :a 2, …, A m :a m, B 1 :b 1, B 2 :b 2, …, B n :b n } –Body of the relation consists of all tuples that are made by the combination of tuples in A and B Ex) See the next slide
Semantics – Times(cont.) Ex) A TIMES B S# S1 S2 S3 S4 S5 P# P1 P2 P3 P4 P5 P6 S#P# S1P1 S1P2 S1P3 S1P4 S1P5 S1P6 S2P1 S2P2 S2P3 S2P4 S2P5 S2P6.. S5P1 S5P2 S5P3 S5P4 S5P5 S5P6...