Relational Query Languages Dictionary  Data Manipulation Language DML

Relational Query Languages Dictionary  Data Manipulation Language DML

Languages of DBMS Data Definition Language DDL define the schema and storage stored in a Data Relational Query Languages Dictionary Data Manipulation Language DML Manipulative populate schema, update database Retrieval querying content of a database Data Control Language DCL permissions, access control etc... Data Manipulation Language Relational operators transform either a single relation or a pair of relations into a result that is a relation that can be used as an operand on Theory behind operations is formally defined later operations and equivalent to a first-order logic (FOL) For every operator operand and result, ∀ ∃ ≡ Relational Calculus ( , ) Relational relations are free of duplicates Algebra Operators are tuple oriented or set oriented Relational algebra is a retrieval query language Structured Query Language (SQL) based on set operators and relational an ANSI standard for relational databases, operators based on relational algebra/calculus SQL2 1992 SQL3 1998 Operations in the Relational Model Query Operators Theory behind operations is formally defined and equivalent to a first order logic (FOL) Relational Algebra Relational operators transform either a simple tuple (unary) Selection, Projection relation or a pair of relations into a result that is a relation set (binary) Union, Intersection, Difference tuple (binary) Join, Division The result can be used as an operand on Additional Operators later activities Outer Join, Outer Union For every operand and result, relations are free of duplicates Operators are tuple oriented or set oriented 1 Relational Algebra A Retrieval DML Must Express Select Project Attributes required in a result target list Union Intersection Difference Criteria for selecting tuples for that result R R R S S S qualifier The relations that take part in the query Join Division set generators * Independent of the instances in the database Expressions are in terms of the database Cartesian product schema X SQL Retrieval Statement π Project Operator SELECT[all|distinct] selects a subset of the attributes of a relation {*|{table.*|expr[alias]|view.*} [,{table.*|expr[alias]}]...} attribute list are drawn from the specified relation; FROM table [alias][,table[alias]] ... if the key attribute is in the list then card(result) = card(relation) [WHERE condition] [CONNECT BY condition [START WITH condition]] π Result = (attribute list)(relation name) [GROUP BY expr [,expr] ...] [HAVING condition] resulting relation [{UNION|UNION ALL|INTERSECT|MINUS} has only the the degree(result) = number of attributes in the attribute list SELECT ...] attributes in the list, in same order [ORDER BY {expr|position} as they appear in no duplicates in the result [ASC|DESC][,expr|position}[ASC|DESC]. the list [FOR UPDATE OF column [,column] ... [NOWAIT]] π Project Operator π Project Operator SELECT STUDENT STUDENT studno name hons tutor year studno name hons tutor year s1 jones ca bush 2 select * s1 jones ca bush 2 s2 brown cis kahn 2 from student; s2 brown cis kahn 2 s3 smith cs goble 2 s3 smith cs goble 2 s4 bloggs ca goble 1 s4 bloggs ca goble 1 s5 jones cs zobel 1 s5 jones cs zobel 1 s6 peters ca kahn 3 s6 peters ca kahn 3 tutor tutor bush kahn select tutor bush π goble from student; kahn tutor(STUDENT) goble goble zobel zobel kahn 2 σ Select Operator σ Select Operator selects a subset of the tuples in a relation that STUDENT satisfy a selection condition studno name hons tutor year s1 jones ca bush 2 s2 brown cis kahn 2 a boolean expression specified on the attributes s3 smith cs goble 2 of a specified relation s4 bloggs ca goble 1 s5 jones cs zobel 1 σ s6 peters ca kahn 3 Result = (selection condition)(relation name) a relation that • stands for the usual comparison operators ‘<‘, studno name hons tutor year has the same ‘<>‘, ‘<=‘, ‘>‘, ‘>=‘, etc s4 bloggs ca goble 1 attributes as the • clauses can be arbitrarily connected with boolean operators AND, NOT, OR source relation; σ name=‘bloggs’(STUDENT) degree(result) = degree(relation); card(result) <= card(relation) retrieve tutor who tutors Bloggs SQL retrieval expressions STUDENT studno name hons tutor year s1 jones ca bush 2 select studentno, name from student s2 brown cis kahn 2 s3 smith cs goble 2 where hons != ‘ca’ and s4 bloggs ca goble 1 s5 jones cs zobel 1 (tutor = ‘goble’ or tutor = ‘kahn’); s6 peters ca kahn 3 select * from enrol where labmark > 50; π (σ (STUDENT)) tutor name=‘bloggs’ select * from enrol select tutor from student where labmark between 30 and 50; where name = ‘bloggs’; Cartesian Product Operator select * from enrol where labmark in (0, 100); Definition: select * from enrol The cartesian product of two relations where labmark is null; R1(A1,A2,...,An) with cardinality i and select * from student R2(B1,B2,...,Bm) with cardinality j is a relation R3 with degree k=n+m, cardinality i*j and where name is like ‘b%’; attributes (A1,A2,...,An,B1,B2,...,Bm) select studno, courseno, The result, denoted by R1XR2, is a relation exammark+labmark total from enrol that includes all the possible combinations of tuples from R1 and R2 where is not NULL; labmark Used in conjunction with other operations 3 Cartesian Product Example X Cartesian Product STUDENT studno name hons tutor year lecturer roomno STUDENT COURSE studno name hons tutor year s1 jones ca bush 2 kahn IT206 studno name courseno subject equip s1 jones ca bush 2 bush 2.26 s1 jones ca bush 2 s1 jones ca bush 2 goble 2.82 s1 jones cs250 prog sun s2 brown cis kahn 2 s1 jones ca bush 2 zobel 2.34 s1 jones ca bush 2 watson IT212 s2 brown cs150 prog sun s3 smith cs goble 2 s1 jones ca bush 2 woods IT204 s6 peters cs390 specs sun s4 bloggs ca goble 1 s1 jones ca bush 2 capon A14 s1 jones ca bush 2 lindsey 2.10 s5 jones cs zobel 1 s1 jones ca bush 2 barringer 2.125 s2 brown cis kahn 2 kahn IT206 s6 peters ca kahn 3 s2 brown cis kahn 2 bush 2.26 STUDENT X COURSE s2 brown cis kahn 2 goble 2.82 studno name courseno subject equip s2 brown cis kahn 2 zobel 2.34 STAFF s2 brown cis kahn 2 watson IT212 s1 jones cs250 prog sun s2 brown cis kahn 2 woods IT204 s1 jones cs150 prog sun lecturer roomno s2 brown cis kahn 2 capon A14 s2 brown cis kahn 2 lindsey 2.10 s1 jones cs390 specs sun kahn IT206 s2 brown cis kahn 2 barringer 2.125 s2 brown cs250 prog sun bush 2.26 s3 smith cs goble 2 kahn IT206 s3 smith cs goble 2 bush 2.26 s2 brown cs150 prog sun goble 2.82 s3 smith cs goble 2 goble 2.82 s2 brown cs390 specs sun zobel 2.34 s3 smith cs goble 2 zobel 2.34 s3 smith cs goble 2 watson IT212 s6 peters cs250 prog sun watson IT212 s3 smith cs goble 2 woods IT204 s6 peters cs150 prog sun s3 smith cs goble 2 capon A14 woods IT204 s3 smith cs goble 2 lindsey 2.10 s6 peters cs390 specs sun capon A14 s3 smith cs goble 2 barringer 2.125 s4 bloggs ca goble 1 kahn IT206 lindsey 2.10 s4 bloggs ca goble 1 bush 2.26 barringer 2.125 θ θ Join Operator Join Operator STAFF lecturer roomno Definition: STUDENT kahn IT206 studno name hons tutor year bush 2.26 The join of two relations R1(A1,A2,...,An) and s1 jones ca bush 2 goble 2.82 s2 brown cis kahn 2 R2(B1,B2,...,Bm) is a relation R3 with degree k=n+m zobel 2.34 and attributes (A ,A ,...,A , B ,B ,...,B ) that satisfy s3 smith cs goble 2 watson IT212 1 2 n 1 2 m s4 bloggs ca goble 1 woods IT204 the join condition s5 jones cs zobel 1 capon A14 s6 peters ca kahn 3 lindsey 2.10 Result = R1 (θ join condition) R2 barringer 2.125 stud name hons tutor year lecturer roomno •stands for the usual comparison no The result is a concatenated operators ‘<‘, ‘<>‘, ‘<=‘, ‘>‘, ‘>=‘, etc s1 jones ca bush 2 bush 2.26 set but only for those tuples s2 brown cis kahn 2 kahn IT206 where the condition is true. Θ s3 smith cs goble 2 goble 2.82 •comparing terms in the clauses can s4 bloggs ca goble 1 goble 2.82 It does not require union be arbitrarily connected with boolean compatibility of R1 and R2 s5 jones cs zobel 1 zobel 2.34 operators AND, NOT, OR s6 peters ca kahn 3 kahn IT206 ENROL More joins stud course lab exam no no mark mark STUDENT s1 cs250 65 52 Natural Join Operator studno name hons tutor year s1 cs260 80 75 s1 jones ca bush 2 s1 cs270 47 34 θ s2 brown cis kahn 2 s2 cs250 67 55 Of all the types of -join, the equi-join is the s3 smith cs goble 2 s2 cs270 65 71 only one that yields a result in which the s4 bloggs ca goble 1 s3 cs270 49 50 compared columns are redundant to each s5 jones cs zobel 1 s4 cs280 50 51 s6 peters ca kahn 3 s5 cs250 0 3 other—possibly different names but same s6 cs250 2 7 values The natural join is an equi-join but one of the stud name hons tutor year stud course lab exam no no no mark mark redundant columns (simple or composite) is s1 jones ca bush 2 s1 cs250 65 52 s1 jones ca bush 2 s1 cs260 80 75 omitted from the result s1 jones ca bush 2 s1 cs270 47 34 s2 brown cis kahn 2 s2 cs250 67 55 Relational join is the principle algebraic s2 brown cis kahn 2 s2 cs270 65 71 s3 smith cs goble 2 s3 cs270 49 50 counterpart of queries that involve the s4 bloggs ca goble 1 s4 cs280 50 51 s5 jones cs zobel 1 s5 cs250 0 3 existential quantifier ∃ s6 peters ca kahn 3 s6 cs250 2 7 4 Self Join: Joins on the same relation Exercise π (lecturer, (staff (appraiser = lecturer) staff) Get student’s name, all their courses, subject roomno,appraiser, approom) of course, labmark for course, lecturer of course and lecturer’s roomno for ‘ca’ students STAFF lecturer roomno appraiser lecturer roomno appraiser approom kahn IT206 watson kahn IT206 watson IT212 University Schema bush 2.26 capon bush 2.26 capon A14 goble 2.82 capon goble 2.82 capon A14

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    10 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us