
Codd’s Relational Algebra (RA) § Data is stored as a set of relations CS 2451 • Relations implemented as tables Database Systems: • Tuple in a relation is a row in the table • Attribute (from domain) in relation is column in table Relational Algebra & § RA = A set of mathematical operators that compose, modify, Relational Calculus and combine tuples within different relations § Relations are sets. http://www.seas.gwu.edu/~bhagiweb/cs2541 • Mapping: maps elements in one set to another Spring 2020 § Relational algebra operations operate on relations and Instructor: Dr. Bhagi Narahari produce relations (“closure”) f: Relation à Relation f: Relation x Relation à Relation Based on slides © Ramakrishnan&Gerhke, Dr R. Lawrence, UBC 1 2 Preliminaries Relational Algebra § query language used to update and retrieve data that is § A query is applied to relation instances, and the result of stored in a data model. a query is also a relation instance. § Relational algebra is a set of relational operations for • Schemas of input relations for a query are fixed (but query will run retrieving data. regardless of instance!) • Just like algebra with numbers, relational algebra consists of operands • The schema for the result of a given query is also fixed -- (which are relations) and a set of operators. Determined by definition of query language constructs. § Every relational operator takes as input one or more § Notation: Positional ( R[0] ) vs. named-field notation relations and produces a relation as output. (R.name): • Closure property - input is relations, output is relations • Positional notation easier for formal definitions, named-field • Unary operations - operate on one relation notation more readable. • Binary operations - have two relations as input We will use named field notation R.name § A sequence of relational algebra operators is called a • Both used in SQL relational algebra expression. 3 4 1 Relational Algebra Operators Relational Algebra Expression: Syntax § RA operators operate on relations and produce relations – § Basic operations: closed algebra • Selection ( s ) Selects a subset of rows from relation. • Defined recursively • Projection ( p ) Deletes unwanted columns from relation. § (Basis) basic expression consists of a relation in the schema • Cross-product ( ´ ) Allows us to combine two relations. or a constant relation • Set-difference ( - ) Tuples in relation 1, but not in relation 2. • What is a constant relation ? • Union ( ) Tuples in relation. 1 or in relation. 2. ! § (R) Let E1 and E2 be RA expressions, then § Note: cross-product, set-difference, union are the set operations you have seen before § Additional operations: • Intersection, join, assignment, division, renaming: Not essential, but (very!) useful. § Since each operation returns a relation, operations can be composed! (Algebra is “closed”.) 5 6 RA expressions..contd.. Operator Precedence • (E1 ∪ E2) is a RA expression § Just like mathematical operators, the relational operators • (E1 – E2) is a RA expression have precedence. • (E1 × E2) is a RA expression • σP (E1) is a RA expression Where P is a predicate (conditional statement) § The precedence of operators from highest to lowest is: • πS(E1) is a RA expression • unary operators - s, P, r Where S is subset of the attributes in the schema • Cartesian product and joins - X, ⨝ , division • ρR (E1) is a RA expression • intersection § Operations Can be composed • union and set difference • If R1, R2 are relations (sets), then R1 <op> R2 is also a relation (set) § Parentheses can be used to changed the order of • Closed algebra – how is closure defined ?? operations. § Operations are defined as Set operations • Input is a set, output is a set § It is a good idea to always use parentheses around the SQL allows duplicated, RA does not argument for both unary and binary operators. § Above definition can be used to define syntax and construct a parser 7 8 2 Data Instance for Mini-Banner Example Projection Operator § We want to query the database and fetch only some STUDENT Takes COURSE column/attribute from the relation sid name sid exp-grade cid cid subj sem § Example: We want student name only from students table 1 Jill 1 A 550-0103 550-0103 DB F03 2 Matt 1 A 700-1003 700-1003 Math S03 3 Jack 3 A 700-1003 501-0103 Arch F03 Pname(Student) 4 Maury 3 C 500-0103 4 C 500-0103 PROFESSOR Teaches fid name fid cid 1 Narahari 1 550-0103 2 Youssef 2 700-1003 8 Choi 8 501-0103 9 10 Projection Operation Formal Definition Projection Example § Given a list of column names a and a relation R, pa (R) extracts the columns in a from the relation. Emp Relation P eno,ename (Emp) § The projection operation on relation R with output attributes eno ename title salary eno ename E1 J. Doe EE 30000 E1 J. Doe A1,…,Am is denoted by PA1,…,Am(R). E2 M. Smith SA 50000 E2 M. Smith E3 A. Lee ME 40000 E3 A. Lee E4 J. Miller PR 20000 E4 J. Miller PA1,…,Am(R)={t[A1,…, Am] | tÎR} E5 B. Casey SA 50000 E5 B. Casey E6 L. Chu EE 30000 E6 L. Chu where E7 R. Davis ME 40000 E7 R. Davis E8 J. Jones SA 50000 E8 J. Jones • R is a relation, t is a tuple variable • {A1,…,Am} is a subset of the attributes of R over which the projection will be performed. • Order of A1,…, Am is significant in the result. • Cardinality of PA1,…,Am(R) is not necessarily the same as R because of duplicate removal. 11 12 3 Recap- Projection, Pa Selection Operation § Given a list of column names a and a relation R, pa § We want to query table and fetch only the rows that satisfy (R) extracts the columns in a from the relation. come condition Output is relation…so removes duplicates! • Example: Students whose grade = B § Example: find sid and grade from enrollment table § The selection operation s (sigma) is a unary operation Takes P sid,exp-grade (Takes) that takes in a relation as input and returns a new relation as output that contains a subset of the tuples of the input sid exp-grade cid sid exp-grade relation. 1 A 550-0103 1 A • output relation has the same number of columns as the input relation, but may have less rows. 1 A 700-1003 3 A 3 A 700-1003 3 C 3 C 500-0103 4 C § To determine which tuples are in the output, the selection 4 C 500-0103 operation has a specified condition, called a predicate, that tuples must satisfy to be in the output. Note: duplicate elimination. In contrast, SQL returns by default • The predicate is similar to a condition in an if statement. a multiset and duplicates must be explicitly removed. § Selection sq R takes a relation R and extracts those rows from it that satisfy the condition q 13 14 Select Operation s Select Operation s § The selection operation on relation R with predicate F is § Notation: spredicate(Relation) § Relation: can be table or result of another query denoted by sF(R). § Predicate: 1. Simple predicate sF(R)={t | tÎR and F(t) is true} • Attribute1 = attribute2 • Attribute = constant (also >, <, >=, <=, <> ) where 2. Complex predicate • R is a relation, t is a tuple variable • Predicate AND predicate • F is a formula (predicate) consisting of • Predicate OR predicate operands that are constants or attributes • NOT predicate comparison operators: <, >, =, ¹, £, ³ § Idea: select rows from a Relation based on a predicate logical operators:AND, OR, NOT § Similarity between formal definition of operators in RA and the syntax of Relational Calculus 15 16 4 Complex Predicate Conditions Selection Example Emp Relation stitle = 'EE' (Emp) § Conditions are built up from boolean-valued eno ename title salary operations on the field names. E1 J. Doe EE 30000 eno ename title salary • exp-grade <> “A”, name = “Jill”, STUDENT.sid=Takes.sid E2 M. Smith SA 50000 E1 J. Doe EE 30000 § RA allows comparison predicate on attributes E3 A. Lee ME 40000 E6 L. Chu EE 30000 • =, not=, >, <, >=, <= E4 J. Miller PR 20000 E5 B. Casey SA 50000 § Larger predicates can be formed using logical ssalary > 35000 Ú title = 'PR' (Emp) connectives – or (∨ ) and and (∧ ) and not ( ⌉ ) E6 L. Chu EE 30000 eno ename title salary § Selection predicate can include comparison between E7 R. Davis ME 40000 E8 J. Jones SA 50000 E2 M. Smith SA 50000 attributes E3 A. Lee ME 40000 § We don't lose any expressive power if we don't have E4 J. Miller PR 20000 complex predicates in the language, but they are E5 B. Casey SA 50000 convenient and useful in practice. E7 R. Davis ME 40000 E8 J. Jones SA 50000 Logic operators: Ù AND, Ú OR, ¬ NOT 17 18 Union È (same as set union operation) Union Example § If two relations have the same structure (Database eno ename title salary terminology: are union-compatible. Programming Emp E1 J. Doe EE 30000 language terminology: have the same type) we can E2 M. Smith SA 50000 perform set operations. E3 A. Lee ME 40000 E4 J. Miller PR 20000 § All persons who are students or faculty E5 B. Casey SA 50000 E6 L. Chu EE 30000 Peno(Emp) È Peno(WorksOn) E7 R. Davis ME 40000 STUDENT FACULTY STUDENT È FACULTY E8 J. Jones SA 50000 eno E1 fid name id name E2 sid name WorksOn eno pno resp dur 1 Darby 1 Darby E1 P1 Manager 12 E3 1 Darby E4 E2 P1 Analyst 24 12 Youssef 2 Matt E5 2 Matt E2 P2 Analyst 6 18 Choi 3 Dan E6 3 Dan E3 P4 Engineer 48 E7 4 Marty E5 P2 Manager 24 4 Maury E8 12 Youssef E6 P4 Manager 48 E7 P3 Engineer 36 18 Choi E7 P5 Engineer 23 19 20 5 Type Matching for Set operations Set Difference § Same number of attributes § Set difference is a binary operation that takes two relations § Same type of attributes R and S as input and produces an output relation that • Each position must match domain contains all the tuples of R that are not in S.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages12 Page
-
File Size-