Download presentation
Presentation is loading. Please wait.
Published byElvin Stafford Modified over 9 years ago
1
Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science
2
Relational Query Languages Languages for describing queries on a relational database Structured Query LanguageStructured Query Language (SQL) –Predominant application-level query language –Declarative Relational AlgebraRelational Algebra –Intermediate language used within DBMS –Procedural
3
What is an 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 relational algebraWhen the domain is a set of all relations (and the operators are as described later), we get the relational algebra query queryresultWe refer to the expression as a query and the value produced as the query result
4
Relational Algebra Domain: set of relations selectprojectunionset differenceCartesianproductBasic operators: select, project, union, set difference, Cartesian product set intersectiondivisionjoinDerived 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
5
The Role of Relational Algebra in a DBMS
6
Select Operator Produce table containing subset of rows of argument table satisfying condition condition (relation) Example: Person Person Person Hobby=‘stamps’ ( Person ) 1123 John 123 Main stamps 1123 John 123 Main coins 5556 Mary 7 Lake Dr hiking 9876 Bart 5 Pine St stamps 1123 John 123 Main stamps 9876 Bart 5 Pine St stamps Id Name Address Hobby
7
Selection Condition Operators:, =, Simple selection condition: – operator AND OR NOT
8
Selection Condition - Examples Person Id>3000 OR Hobby=‘hiking’ (Person) Person Id>3000 AND Id <3999 (Person) Person NOT (Hobby=‘hiking’) (Person) Person Hobby ‘hiking’ (Person)
9
Project Operator Produces table containing subset of columns of the table taken as argument attribute list (relation) Example: PersonPerson 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
10
Project Operator 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 Result is a table (no duplicates); can have fewer tuples than the original Id Name Address Hobby Name Address Example: PersonPerson Person Name,Address (Person)
11
Expressions 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 Person Result Person Id, Name ( Hobby=’stamps’ OR Hobby=’coins’ (Person) )
12
Set Operators Relation is a set of tuples, so set operations should apply: , , (set difference) Result of combining two relations with a set operator is a relation; hence all its elements must be tuples having the same structure union compatible relationsHence, scope of set operations limited to union compatible relations
13
Union Compatible Relations union compatibleTwo 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 unionintersectionset differenceUnion compatible relations can be combined using union, intersection, and set difference
14
Example Tables: Person Person (SSN, Name, Address, Hobby) Professor Professor (Id, Name, Office, Phone) are not union compatible. But PersonProfessor Name (Person) and Name (Professor) are union compatible so PersonProfessor Name (Person) - Name (Professor) makes sense.
15
Cartesian Product RSRS R S (but see naming problem next)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 (but see naming problem next) RSR S is expensive to compute: –Factor of two in the size of each row –Quadratic in the number of rows 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 RS R S x3 x4 y3 y4 RS R S
16
Renaming in Cartesian Product Result of expression evaluation is a relation. Attributes of relation must have distinct names. So what do we do if they don’t? E.g., suppose R(A,B) and S(A,C) RS and we wish to compute R S. One solution is to rename the attributes of the answer: RS( R.A, R.B, S.A, S.C) R S( R.A, R.B, S.A, S.C) Although only A needs to be renamed, it is“cleaner” to rename them all.
17
Renaming Operator Previous solution is used whenever possible but it won’t work when R is the same as S. Renaming operator resolves this. It allows to assign any desired names, say A 1, A 2,… A n, to the attributes of the n column relation produced by expression expr with the syntax expr [A 1, A 2, … A n ]
18
Example This is a relation with 4 attributes: StudId, CrsCode1, ProfId, CrsCode2 Transcript Transcript (StudId, CrsCode, Semester, Grade) Teaching Teaching (ProfId, CrsCode, Semester) Transcript StudId, CrsCode (Transcript)[StudId, CrsCode1] Teaching ProfId, CrsCode (Teaching) [ProfId, CrsCode2]
19
Derived Operation: Join generalthetajoin A (general or theta) join of R and S is the expression R join-condition S where join-condition is a conjunction of terms: A i oper B i in which A i is an attribute of R; B i is an attribute of S; and oper is one of =,, , . The meaning is: join-condition ´ (R S) where join-condition and join-condition ´ are the same, except for possible renamings of attributes caused by the Cartesian product.
20
Theta Join – Example Employee(Name,Id,MngrId,Salary Employee(Name,Id,MngrId,Salary) Manager(Name,Id,Salary Manager(Name,Id,Salary) Output the names of all employees that earn more than their managers. Employee EmployeeManager Employee.Name ( Employee MngrId=Id AND Salary>Salary Manager ) The join yields a table with attributes: EmployeeEmployee Employee.Name, Employee.Id, Employee Employee.Salary, Employee.MngrId ManagerManagerManager Manager.Name, Manager.Id, Manager.Salary
21
Relational Algebra Relational algebra operations operate on relations and produce relations ( “ closure ” ) f: Relation -> Relationf: Relation x Relation -> Relation Six basic operations: –Projection (R) –Selection (R) –UnionR 1 [ R 2 –DifferenceR 1 – R 2 –ProductR 1 £ R 2 –(Rename) (R)
22
Equijoin Join - Example Student Name,CrsCode ( Student Transcript Id=StudId Grade=‘A’ (Transcript)) Id Name Addr Status 111 John ….. ….. 222 Mary ….. ….. 333 Bill ….. ….. 444 Joe ….. ….. StudId CrsCode Sem Grade 111 CSE305 S00 B 222 CSE306 S99 A 333 CSE304 F99 A Mary CSE306 Bill CSE304 The equijoin is used very frequently since it combines related data in different relations. Student Transcript Equijoin Equijoin: Join condition is a conjunction of equalities.
23
Natural Join Special case of equijoin + a special projection –join condition equates all and only those attributes with the same name (condition doesn’t have to be explicitly stated) –duplicate columns eliminated (projected out) from the result Transcript Transcript (StudId, CrsCode, Sem, Grade) Teaching ( Teaching (ProfId, CrsCode, Sem) Transcript Teaching Teaching = StudId, Transcript.CrsCode, Transcript.Sem, Grade, ProfId Transcript ( Transcript Sem Teaching CrsCode=CrsCode AND Sem=Sem Teaching ) [ StudId, CrsCode, Sem, Grade, ProfId ]
24
Natural Join (cont’d) More generally: R SRS S = attr-list ( join-cond (R × S) ) where RS attr-list = attributes (R) attributes (S) (duplicates are eliminated) and join-cond has the form: A 1 = A 1 AND … AND A n = A n where RS {A 1 … A n } = attributes(R) attributes(S)
25
Natural Join Example List all Ids of students who took at least two different courses: StudId ( CrsCode CrsCode2 ( Transcript Transcript Transcript [ StudId, CrsCode2, Sem2, Grade2 ] )) We don’t want to join on CrsCode, Sem, and Grade attributes, hence renaming!
26
Example Data Instance sidname 1Jill 2Qun 3Nitin 4Marty fidname 1Ives 2Saul 8Roth sidexp-gradecid 1A550-0103 1A700-1003 3A 3C500-0103 4C cidsubjsem 550-0103DBF03 700-1003AIS03 501-0103ArchF03 fidcid 1550-0103 2700-1003 8501-0103 STUDENT Takes COURSE PROFESSOR Teaches
27
Natural Join and Intersection Natural join: special case of join where is implicit – attributes with same name must be equal: STUDENT ⋈ Takes ´ STUDENT ⋈ STUDENT.sid = Takes.sid Takes Intersection: as with set operations, derivable from difference A-B B-A A B A B
28
Division A somewhat messy operation that can be expressed in terms of the operations we have already defined Used to express queries such as “ The fid's of faculty who have taught all subjects ” Paraphrased: “ The fid ’ s of professors for which there does not exist a subject that they haven ’ t taught ”
29
Division Using Our Existing Operators All possible teaching assignments: Allpairs: NotTaught, all (fid,subj) pairs for which professor fid has not taught subj: Answer is all faculty not in NotTaught: fid,subj (PROFESSOR £ subj (COURSE)) Allpairs - fid,subj (Teaches ⋈ COURSE) fid (PROFESSOR) - fid (NotTaught) ´ fid (PROFESSOR) - fid ( fid,subj (PROFESSOR £ subj (COURSE)) - fid,subj (Teaches ⋈ COURSE))
30
Division: R 1 R 2 Requirement: schema(R 1 ) ¾ schema(R 2 ) Result schema: schema(R 1 ) – schema(R 2 ) “ Professors who have taught all courses ” : What about “ Courses that have been taught by all faculty ” ? fid ( fid,subj ( Teaches ⋈ COURSE) subj (COURSE))
31
Division Goal: Produce the tuples in one relation, r, that match all tuples in another relation, s –r –r (A 1, …A n, B 1, …B m ) –s –s (B 1 …B m ) –rs s r –r/s, with attributes A 1, …A n, is the set of all tuples such that for every tuple in s, is in r Can be expressed in terms of projection, set difference, and cross-product
32
Division (cont’d)
33
Division - Example List the Ids of students who have passed all courses that were taught in spring 2000 Numerator: –StudId and CrsCode for every course passed by every student: Transcript StudId, CrsCode ( Grade ‘F’ (Transcript) ) Denominator: – CrsCode of all courses taught in spring 2000 Teaching CrsCode ( Semester=‘S2000’ (Teaching) ) Result is numerator/denominator
34
Relational Calculus Important features: –Declarative formal query languages for relational model –Based on the branch mathematical logic known as predicate calculus –Two types of RC: 1) tuple relational calculus 2) domain relational calculus –A single statement can be used to perform a query
35
Tuple Relational Calculus based on specifying a number of tuple variables a tuple variable refers to any tuple
36
Generic Form {t | COND (t)} –where – t is a tuple variable and –COND(t) is Boolean expression involving t
37
Simple example 1 To find all employees whose salary is greater than $50,000 –{t| EMPLOYEE(t) and t.Salary>5000} where EMPLOYEE(t) specifies the range of tuple variable t –The above operation selects all the attributes
38
Simple example 2 To find only the names of employees whose salary is greater than $50,000 –{t.FNAME, t.NAME| EMPLOYEE(t) and t.Salary>5000} The above is equivalent to SELECT T.FNAME, T.LNAME FROM EMPLOYEE T WHERE T.SALARY > 5000
39
Elements of a tuple calculus In general, we need to specify the following in a tuple calculus expression: –Range Relation (I.e, R(t)) = FROM –Selected combination= WHERE –Requested attributes= SELECT
40
More Example:Q0 Retrieve the birthrate and address of the employee(s) whose name is ‘John B. Smith’ {t.BDATE, t.ADDRESS| EMPLOYEE(t) AND t.FNAME=‘John’ AND t.MINIT=‘B” AND t.LNAME=‘Smith}
41
Formal Specification of tuple Relational Calculus A general format: {t 1.A 1, t 2.A 2,…,t n.A n |COND ( t 1,t 2,…, t n, t n+1, t n+2,…,t n+m )} –where –t 1,…,t n+m are tuple var –A i : attribute R(t i ) –COND (formula) Where COND corresponds to statement about the world, which can be True or False
42
Elements of formula A formula is made of Predicate Calculus atoms: – an atom of the from R(ti) –t i.A op t j.B op {=,,..} –F1 And F2 where F1 and F2 are formulas –F1 OR F2 –Not (F1) –F’=( t) (F) or F’= ( t) (F) Y friends (Y, John) X likes(X, ICE_CREAM)
43
Example Queries Using the Existential Quantifier Retrieve the name and address of all employees who work for the ‘ Research ’ department {t.FNAME, t.LNAME, t.ADDRESS| EMPLOYEE(t) AND ( d) (DEPARTMENT (d) AND d.DNAME=‘Research’ AND d.DNUMBER=t.DNO)}
44
More Example For every project located in ‘Stafford’, retrieve the project number, the controlling department number, and the last name, birthrate, and address of the manger of that department.
45
Cont. {p.PNUMBER,p.DNUM,m.LNAME,m.BD ATE, m.ADDRESS|PROJECT(p) and EMPLOYEE(M) and P.PLOCATION=‘Stafford’ and ( d) (DEPARTMENT(D) AND P.DNUM=d.DNUMBER and d.MGRSSN=m.SSN))}
46
Safe Expressions A safe expression R.C: –An expression that is guaranteed to generate a finite number of rows (tuples) Example: –{t | not EMPLOYESS(t))} results values not being in its domain (I.e., EMPLOYEE)
47
Domain Relational Calculus (DRC) Another type of formal predicate calculus- based language QBE is based on DRC The language shares a lot of similarities with the tuple calculus
48
DRC The only difference is the type of variables: –variables range over singles values from domains of attributes An expression of DRC is: –{x 1, x 2,…,x n |COND(x 1,x 2,…,x n, x n+2,…,x n+m )} where x 1,x 2,…,x n+m are domain var range over attributers COND is a condition (or formula)
49
Examples Retrieve the birthdates and address of the employee whose name is ‘John B. Smith’ {uv| ( q)( r)( s) (EMPLOYEE(qrstuvwxyz) and q=‘John’ and r=‘B’ and s=‘Smith’
50
Alternative notation Ssign the constants ‘John’, ‘B’, and ‘Smith’ directly {uv|EMPLOYEE (‘John’, ’B’, ’Smith’,t,u,v,x,y,z)}
51
More example Retrieve the name and address of all employees who work for the ‘Reseach’ department {qsv | ( z) EMPLOYEE(qrstuvwxyz) and ( l) ( m) (DEPARTMENT (lmno) and l=‘Research’ and m=z))}
52
More example List the names of managers who have at least on e dependent {sq| ( t) EMPLOYEE(qrstuvwxyz) and (( j)( DEPARTMENT (hijk) and (( l) | (DEPENTENT (lmnop) and t=j and t=l))))}
53
QBE Query-By-Example –Supports graphical query language based on DRC –Implemented in commercial db such as Access/Paradox –Query can be specified by filling in templates of relations –Fig 9.5
54
Summary It can be shown that any query that can be expressed in the relational algebra, it can also be expressed in domain and tuple relational calculus
55
Quiz In what sense doe R.C differ from R.A, and in what sense are they similar?
56
The Big Picture: SQL to Algebra to Query Plan to Web Page SELECT * FROM STUDENT, Takes, COURSE WHERE STUDENT.sid = Takes.sID AND Takes.cID = cid STUDENT Takes COURSE Merge Hash by cid Optimizer Execution Engine Storage Subsystem Web Server / UI / etc Query Plan – an operator tree
57
Relational Calculus: A Logical Way of Expressing Query Operations First-order logic (FOL) can also be thought of as a query language, and can be used in two ways: –Tuple relational calculus –Domain relational calculus –Difference is the level at which variables are used: for attributes (domains) or for tuples The calculus is non-procedural (declarative) as compared to the algebra –More like what we ’ ll see in SQL –More convenient to express certain things
58
Domain Relational Calculus Queries have form: { | p} Predicate: boolean expression over x 1,x 2, …, x n –Precise operations depend on the domain and query language – may include special functions, etc. –Assume the following at minimum: RX op Y X op constconst op X where op is , , , , , x i,x j, … are domain variables domain variables predicate
59
More Complex Predicates Starting with these atomic predicates, build up new predicates by the following rules: –Logical connectives: If p and q are predicates, then so are p q, p q, p, and p q (x>2) (x<4) (x>2) (x>0) –Existential quantification: If p is a predicate, then so is x.p x. (x>2) (x<4) –Universal quantification: If p is a predicate, then so is x.p x.x>2 x. y.y>x
60
Some Examples Faculty ids Course names for courses with students expecting a “ C ” Courses taken by Jill
61
Logical Equivalences There are two logical equivalences that will be heavily used: –p q p q (Whenever p is true, q must also be true.) – x. p(x) x. p(x) (p is true for all x) The second can be a lot easier to check!
62
Free and Bound Variables A variable v is bound in a predicate p when p is of the form v … or v … A variable occurs free in p if it occurs in a position where it is not bound by an enclosing or Examples: –x is free in x>2 –x is bound in x.x>y
63
Can Rename Bound Variables Only When a variable is bound one can replace it with some other variable without altering the meaning of the expression, providing there are no name clashes Example: x.x>2 is equivalent to y.y>2 Otherwise, the variable is defined outside our “ scope ”…
64
Safety Pitfall in what we have done so far – how do we interpret: { | STUDENT} –Set of all binary tuples that are not students: an infinite set (and unsafe query) A query is safe if no matter how we instantiate the relations, it always produces a finite answer –Domain independent: answer is the same regardless of the domain in which it is evaluated –Unfortunately, both this definition of safety and domain independence are semantic conditions, and are undecidable
65
Safety and Termination Guarantees There are syntactic conditions that are used to guarantee “ safe ” formulas –The definition is complicated, and we won ’ t discuss it; you can find it in Ullman ’ s Principles of Database and Knowledge-Base Systems –The formulas that are expressible in real query languages based on relational calculus are all “ safe ” Many DB languages include additional features, like recursion, that must be restricted in certain ways to guarantee termination and consistent answers
66
Mini-Quiz How do you write: –Which students have taken more than one course from the same professor? –What is the highest course number offered?
67
Translating from RA to DRC Core of relational algebra: , , , x, - We need to work our way through the structure of an RA expression, translating each possible form. –Let TR[e] be the translation of RA expression e into DRC. Relation names: For the RA expression R, the DRC expression is { | R}
68
Selection: TR[ R] Suppose we have (e ’ ), where e ’ is another RA expression that translates as: TR[e ’ ]= { | p} Then the translation of c (e ’ ) is { | p ’ } where ’ is obtained from by replacing each attribute with the corresponding variable Example: TR[ #1=#2 #4>2.5 R] (if R has arity 4) is { | R x 1 =x 2 x 4 >2.5}
69
Projection: TR[ i 1, …,i m (e)] If TR[e]= { | p} then TR[ i 1,i 2, …,i m (e)]= { | x j 1,x j 2, …, x j k.p}, where x j 1,x j 2, …, x j k are variables in x 1,x 2, …, x n that are not in x i 1,x i 2, …, x i m Example: With R as before, #1,#3 (R)={ | x 2,x 4. R}
70
Union: TR[R 1 R 2 ] R 1 and R 2 must have the same arity For e 1 e 2, where e 1, e 2 are algebra expressions TR[e 1 ]={ |p} and TR[e 2 ]={ |q} Relabel the variables in the second: TR[e 2 ]={ |q ’ } This may involve relabeling bound variables in q to avoid clashes TR[e 1 e 2 ]={ |p q ’ }. Example: TR[R 1 R 2 ] = { | R 1 R 2
71
Other Binary Operators Difference: The same conditions hold as for union If TR[e 1 ]={ |p} and TR[e 2 ]={ |q} Then TR[e 1 - e 2 ]= { |p q} Product: If TR[e 1 ]={ |p} and TR[e 2 ]={ |q} Then TR[e 1 e 2 ]= { | p q} Example: TR[R S]= { | R S }
72
Summary Can translate relational algebra into (domain) relational calculus. Given syntactic restrictions that guarantee safety of DRC query, can translate back to relational algebra These are the principles behind initial development of relational databases –SQL is close to calculus; query plan is close to algebra –Great example of theory leading to practice!
73
Limitations of the Relational Algebra / Calculus Can ’ t do: –Aggregate operations –Recursive queries –Complex (non-tabular) structures Most of these are expressible in SQL, OQL,
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.