CS580 Advanced Database Topics - PowerPoint PPT Presentation

1 / 54
About This Presentation
Title:

CS580 Advanced Database Topics

Description:

Rules for embedding SQL into general purpose programming language such as C, ... the concatenate operator | |' can be used in a query to append two string values. ... – PowerPoint PPT presentation

Number of Views:58
Avg rating:3.0/5.0
Slides: 55
Provided by: IRE62
Category:

less

Transcript and Presenter's Notes

Title: CS580 Advanced Database Topics


1
CS580Advanced Database Topics
  • Chapter 8
  • SQL
  • Irena Pevac

2
SQL
  • Structured Query Language
  • SQL ( ANSI 1986 )
  • SQL2 ( SQL-92 )
  • SQL3 ( SQL-99 )

3
SQL
  • SQL database language contains statements for
  • DDL
  • Data definition
  • DML
  • Querying
  • Updating

4
SQL
  • SQL also contains statements to
  • Define views
  • Specify security and authorization
  • Defining integrity constraints
  • Specifying transaction controls
  • Rules for embedding SQL into general purpose
    programming language such as C, Pascal, VB,
    Delphi, Java

5
(No Transcript)
6
(No Transcript)
7
SQL-Querying
  • SELECT ltlist-of-attributesgt
  • FROM ltlist-or-relationsgt
  • WHERE ltconditiongt

8
SELECT-FROM-WHERE Structure of SQL Queries
  • QUERY 0
  • Retrieve the birthdate and address of the
    employee(s) whose name is John B. Smith
  • Q0
  • SELECT BDATE, ADDRESS   
  • FROM EMPLOYEE   
  • WHERE FNAMEJohn AND MINITB AND
    LNAMESmith

9
Query 1
  • Retrieve the name and address of all employees
    who work for the Research department.
  • SELECT FNAME, LNAME, ADDRESS   
  • FROM EMPLOYEE, DEPARTMENT   
  • WHERE DNAMEResearch AND DNUMBERDNO

10
Query 2
  • For every project located in Stafford, list the
    project number, the controlling department
    number, and the department managers last name,
    address, and birthdate.
  • SELECT PNUMBER, DNUM, LNAME, ADDRESS, BDATE   
  • FROM PROJECT, DEPARTMENT, EMPLOYEE   
  • WHERE DNUMDNUMBER AND MGRSSNSSN AND
    PLOCATIONStafford

11
Ambiguous Attribute Names
  • Suppose that in Figure 07.06 the DNO and LNAME
    attributes of the EMPLOYEE relation were called
    DNUMBER and NAME and the DNAME attribute of
    DEPARTMENT was also called NAME
  • SELECT FNAME, EMPLOYEE.NAME, ADDRESS  
  • FROM EMPLOYEE, DEPARTMENT  
  • WHERE DEPARTMENT.NAMEResearch AND
    DEPARTMENT.DNUMBEREMPLOYEE.DNUMBER

12
QUERY 8
  • For each employee, retrieve the employees first
    and last name and the first and last name of his
    or her immediate supervisor
  • SELECT E.FNAME, E.LNAME, S.FNAME, S.LNAME   
  • FROM EMPLOYEE AS E, EMPLOYEE AS S   
  • WHERE E.SUPERSSNS.SSN

13
Q1B
  • We could specify query Q1A as in Q1B just for
    convenience to shorten the relation names that
    prefix the attributes
  • Q1B
  • SELECT E.FNAME, E.NAME, E.ADDRESS   
  • FROM EMPLOYEE E, DEPARTMENT D   
  • WHERE D.NAMEResearch AND D.DNUMBERE.DNUMBER

14
Unspecified WHERE
  • Select all EMPLOYEE SSNs
  • Q9
  • SELECT SSN   
  • FROM EMPLOYEE

15
Unspecified WHERE
  • Select all combinations of EMPLOYEE SSN and
    DEPARTMENT DNAME (Q10) in the database.
  • Q10
  • SELECT SSN, DNAME   
  • FROM EMPLOYEE, DEPARTMENT

16
Use of Asterisk ()
  • To retrieve all the attribute values of the
    selected tuples, we do not have to list the
    attribute names explicitly in SQL we just
    specify an asterisk (), which stands for all the
    attributes.

17
Q1c
  • Retrieve all the attribute values of EMPLOYEE
    tuples who work in DEPARTMENT number 5.
  • SELECT   
  • FROM EMPLOYEE   
  • WHERE DNO5

18
Q1c
  • Retrieve all the attributes of an EMPLOYEE and
    the attributes of the DEPARTMENT he or she works
    in for every employee of the Research
    department
  • SELECT   
  • FROM EMPLOYEE, DEPARTMENT   
  • WHERE DNAMEResearch AND DNODNUMBER

19
Q10A
  • Specify the CROSS PRODUCT of the EMPLOYEE and
    DEPARTMENT relations.
  • Q10A
  • SELECT   
  • FROM EMPLOYEE, DEPARTMENT

20
Tables as Sets in SQL
  • SQL usually treats a table not as a set but
    rather as a multiset
  • duplicate tuples can appear more than once in a
    table, and in the result of a query.
  • SQL does not automatically eliminate duplicate
    tuples in the results of queries, for the
    following reasons

21
Reasons for Multiset in SQL
  • Duplicate elimination is an expensive operation.
    One way to implement it is to sort the tuples
    first and then eliminate duplicates.
  • The user may want to see duplicate tuples in the
    result of a query.
  • When an aggregate function is applied to tuples,
    in most cases we do not want to eliminate
    duplicates

22
DISTINCT Keyword
  • If we are interested only in distinct salary
    values, we want each value to appear only once,
    regardless of how many employees earn that
    salary.
  • By using the keyword DISTINCT as in Q11A we
    accomplish this.

23
Q11
  • Retrieve the salary of every employee  
  • Q11
  • SELECT ALL SALARY   
  • FROM EMPLOYEE  

24
Q11A
  • Retrieve all distinct salary values (Q11A). Q11A
  • SELECT DISTINCT SALARY   
  • FROM EMPLOYEE

25
SQL Set Operations
  • SQL has directly incorporated some of the set
    operations of relational algebra.
  • There is a
  • set union operation (UNION),
  • set difference (EXCEPT) and
  • set intersection (INTERSECT) operations

26
Set Operations Rules
  • The relations resulting from these set operations
    are sets of tuples
  • Duplicate tuples are eliminated from the result.
  • Because these set operations apply only to
    union-compatible relations, we must make sure
    that the two relations on which we apply the
    operation have the same attributes and that the
    attributes appear in the same order in both
    relations

27
UNION -QUERY 4
  • Make a list of all project numbers for projects
    that involve an employee whose last name is
    Smith, either as a worker or as a manager of
    the department that controls the project.
  • Q4
  • (SELECT DISTINCT PNUMBER   
  • FROM PROJECT, DEPARTMENT, EMPLOYEE   
  • WHERE DNUMDNUMBER AND MGRSSNSSN AND
    LNAMESmith)   
  • UNION   
  • (SELECT DISTINCT PNUMBER   
  • FROM PROJECT, WORKS_ON, EMPLOYEE  
  • WHERE PNUMBERPNO AND ESSNSSN AND
    LNAMESmith)

28
Q4
  • The first SELECT query retrieves the projects
    that involve a Smith as manager of the
    department that controls the project, and
  • the second retrieves the projects that involve a
    Smith as a worker on the project.
  • Notice that, if several employees have the last
    name Smith, the project names involving any of
    them will be retrieved.
  • Applying the UNION operation to the two SELECT
    queries gives the desired result.

29
Substring Comparisons, Arithmetic Operators
  • Comparison on only parts of a character string is
    done using the LIKE comparison operator.
  • Partial strings are specified by using two
    reserved characters
  • replaces an arbitrary number of characters,
    and
  • the underscore ( _ ) replaces a single character.

30
QUERY 12
  • Retrieve all employees whose address is in
    Houston, Texas.
  • Q12
  • SELECT FNAME, LNAME   
  • FROM EMPLOYEE   
  • WHERE ADDRESS LIKE Houston,TX

31
Using underscore _
  • Retrieve all employees who were born during the
    1950s.
  • Here, 5 must be the third character of the
    string (according to our format for date), so we
    use the value _ _ 5 _ _ _ _ _ _ _, with each
    underscore serving as a placeholder for an
    arbitrary character.

32
QUERY 12A
  • Find all employees who were born during the
    1950s.
  • Q12A
  • SELECT FNAME, LNAME   
  • FROM EMPLOYEE   
  • WHERE BDATE LIKE_ _ 5 _ _ _ _ _ _ _

33
Arithmetic Operators
  • Another feature allows the use of arithmetic in
    queries.
  • The standard arithmetic operators for
  • addition (),
  • subtraction (-),
  • multiplication (), and
  • division (/)
  • can be applied to numeric values or attributes
    with numeric domains.

34
QUERY 13
  • Show the resulting salaries if every employee
    working on the ProductX project is given a 10
    percent raise.
  •  Q13
  • SELECT FNAME, LNAME, 1.1SALARY   
  • FROM EMPLOYEE, WORKS_ON, PROJECT   
  • WHERE SSNESSN AND PNOPNUMBER AND
    PNAMEProductX

35
Other Operators
  • For string data types, the concatenate operator
    can be used in a query to append two string
    values.
  • For date, time, timestamp, and interval data
    types, operators include incrementing () or
    decrementing (-) a date, time, or timestamp by
    a type-compatible interval. In addition, an
    interval value can be specified as the difference
    between two date, time, or timestamp values.

36
QUERY 14
  • Another comparison operator is BETWEEN.
  • Retrieve all employees in department 5 whose
    salary is between 30,000 and 40,000.
  • Q14
  • SELECT   
  • FROM EMPLOYEE   
  • WHERE (SALARY BETWEEN 30000 AND 40000) AND DNO
    5

37
ORDER BY - Clause
  • SQL allows the user to order the tuples in the
    result of a query by the values of one or more
    attributes, using the ORDER BY-clause.

38
QUERY 15
  • Retrieve a list of employees and the projects
    they are working on, ordered by department and,
    within each department, ordered alphabetically by
    last name, first name.
  •  Q15
  • SELECT DNAME, LNAME, FNAME, PNAME   
  • FROM DEPARTMENT, EMPLOYEE, WORKS_ON, PROJECT
      
  • WHERE DNUMBERDNO AND SSNESSN AND
    PNOPNUMBER   
  • ORDER BY DNAME, LNAME, FNAME

39
Ordering
  • The default order is in ascending order of
    values.
  • We can specify the keyword DESC if we want a
    descending order of values.
  • The keyword ASC can be used to specify ascending
    order explicitly.
  • If we want descending order on DNAME and
    ascending order on LNAME, FNAME,
  • the ORDER BY-clause of Q15 becomes
  • ORDER BY   DNAME DESC, LNAME ASC, FNAME ASC

40
Nested Queries
  • Correlated Nested Queries
  • Whenever a condition in the WHERE-clause of a
    nested query references some attribute of a
    relation declared in the outer query, the two
    queries are said to be correlated.
  • We can understand a correlated query better by
    considering that the nested query is evaluated
    once for each tuple (or combination of tuples) in
    the outer query.

41
Q4A
  • SELECT DISTINCT PNUMBER   
  • FROM PROJECT   
  • WHERE PNUMBER IN
  • (SELECT PNUMBER     
  • FROM PROJECT, DEPARTMENT, EMPLOYEE     
  • WHERE DNUMDNUMBER AND MGRSSNSSN AND
    LNAMESmith)    
  • OR    PNUMBER IN
  • (SELECT PNO     
  • FROM WORKS_ON, EMPLOYEE     
  • WHERE ESSNSSN AND LNAMESmith)

42
IN Operator
  • The first nested query selects the project
    numbers of projects that have a Smith involved
    as manager, while the second selects the project
    numbers of projects that have a Smith involved
    as worker.
  • In the outer query, we select a PROJECT tuple if
    the PNUMBER value of that tuple is in the result
    of either nested query.
  • The comparison operator IN compares a value v
    with a set (or multiset) of values V and
    evaluates to TRUE if v is one of the elements in
    V.

43
In Operator
  • The IN operator can also compare a tuple of
    values in parentheses with a set or multiset of
    union-compatible tuples.
  • (Att1, Att2,..,Attn) IN (multiset-result of a
    query)

44
(Att1,Att2) IN multiset
  • Select the social security numbers of all
    employees who work the same (project, hours)
    combination on some project that employee John
    Smith (whose SSN 123456789) works on.
  • SELECT DISTINCT ESSN  
  • FROM WORKS_ON
  • WHERE (PNO, HOURS) IN
  • (SELECT PNO, HOURS
  • FROM WORKS_ON
  • WHERE SSN123456789)

45
Att lt ALL MultisetAtt lt ANY Multiset
  • In addition to the IN operator, a number of other
    comparison operators can be used to compare a
    single value v (typically an attribute name) to a
    set or multiset V (typically a nested query).
  • The ANY (or SOME) operator returns TRUE if
    the value v is equal to some value in the set V
    and is hence equivalent to IN.
  • The keywords ANY and SOME have the same meaning.
    Other operators that can be combined with ANY (or
    SOME) include gt, gt, lt, lt, and ltgt.
  • The keyword ALL can also be combined with each of
    these operators.
  • For example, the comparison condition (v gt ALL V)
    returns TRUE if the value v is greater than all
    the values in the set V.

46
Att gt ALL (multiset)
  • Query List the names of employees whose salary
    is greater than the salary of all the employees
    in department 5
  • SELECT LNAME, FNAME  
  • FROM EMPLOYEE  
  • WHERE SALARY gt ALL
  • (SELECT SALARY
  • FROM EMPLOYEE
  • WHERE DNO5)

47
Nested queries
  • In general, we can have several levels of nested
    queries.
  • Possible ambiguity among attribute names
  • Same attribure name appears in a relation in the
    FROM-clause of the outer query, and
  • in a relation in the FROM-clause of the nested
    query.
  • The rule is that a reference to an unqualified
    attribute refers to the relation declared in the
    innermost nested query.

48
QUERY 16
  • Retrieve the name of each employee who has a
    dependent with the same first name and same sex
    as the employee.
  • SELECT E.FNAME, E.LNAME   
  • FROM EMPLOYEE AS E   
  • WHERE E.SSN IN
  • (SELECT ESSN     
  • FROM DEPENDENT     
  • WHERE E.FNAMEDEPENDENT_NAME AND
    E.SEXSEX)

49
Q16
  • Q16 for each EMPLOYEE tuple, evaluate the nested
    query, which retrieves the ESSN values for all
    DEPENDENT tuples with the same sex and name as
    the EMPLOYEE tuple
  • if the SSN value of the EMPLOYEE tuple is in the
    result of the nested query, then select that
    EMPLOYEE tuple.

50
Q16
  • In general, a query written with nested SELECT .
    . . FROM . . . WHERE . . . blocks and using the
    or IN comparison operators can always be
    expressed as a single block query.
  • For example, Q16 may be written as in Q16A

51
Q16A
  • SELECT E.FNAME, E.LNAME   
  • FROM EMPLOYEE AS E, DEPENDENT AS D   
  • WHERE E.SSND.ESSN AND E.SEX D.SEX AND
    E.FNAME D.DEPENDENT_NAME

52
Contains
  • The original SQL implementation on SYSTEM R also
    had a CONTAINS comparison operator, which is used
    to compare two sets or multisets.
  • This operator was subsequently dropped from the
    language, possibly because of the difficulty in
    implementing it efficiently.
  • Most commercial implementations of SQL do not
    have this operator.
  • The CONTAINS operator compares two sets of values
    and returns TRUE if one set contains all values
    in the other set.
  • Query 3 illustrates the use of the CONTAINS
    operator.

53
QUERY 3
  • Q3 Retrieve the name of each employee who works
    on all the projects controlled by department
    number 5.
  • SELECT FNAME, LNAME   
  • FROM EMPLOYEE   
  • WHERE (
  • (SELECT PNO      the set of PNO where
  • FROM WORKS_ON      employee works on
  • WHERE SSNESSN)     
  • CONTAINS      
    contains ?
  • (SELECT PNUMBER     
  • FROM PROJECT     the set of
    PNO controlled
  •   WHERE DNUM5) ) by DNUM 5

54
Q3
  • In Q3, the second nested query (which is not
    correlated with the outer query) retrieves the
    project numbers of all projects controlled by
    department 5.
  • For each employee tuple, the first nested query
    (which is correlated) retrieves the project
    numbers on which the employee works if these
    contain all projects controlled by department 5,
    the employee tuple is selected and the name of
    that employee is retrieved.
  • CONTAINS comparison operator is similar in
    function to the DIVISION operation of the
    relational algebra.
Write a Comment
User Comments (0)
About PowerShow.com