query languages (basic operator)

Upload: mohanmarvadi

Post on 07-Apr-2018

228 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 Query Languages (basic operator)

    1/21

    Query Languages

    Language in which user requests information from the database.

    Categories of languages

    Procedural

    Non-procedural, or declarative

    Pure languages:

    Relational algebra

    Tuple relational calculus

    Domain relational calculus

  • 8/3/2019 Query Languages (basic operator)

    2/21

    Relational Algebra

    It is a Procedural language

    It consist of set of operation which takes one or two relation asinput and produce relation as output.

    Six basic operators

    select: - unary

    project: - unary

    rename: -unary

    union: - binary

    set difference: - binary

    Cartesian product: x - binary

    The operators take one or two relations as inputs and producea new relation as a result.

  • 8/3/2019 Query Languages (basic operator)

    3/21

    Additional Operations

    We define additional operations that do not add any power to the

    relational algebra, but that simplify common queries.

    Set intersection

    Natural join

    Division

    Assignment

  • 8/3/2019 Query Languages (basic operator)

    4/21

    Select Operation

    Notation: p(r)

    pis called the selection predicate Defined as:

    Each term is one of:

    op or

    where opis one of: =, , >, .

  • 8/3/2019 Query Languages (basic operator)

    5/21

    Select Operation Example

    Relation rA B C D

    1

    5

    12

    23

    7

    7

    3

    10

    A=B ^ D > 5(r)A B C D

    1

    23

    7

    10

  • 8/3/2019 Query Languages (basic operator)

    6/21

    Project Operation

    Notation:

    where A1, A2are attribute names and ris a relation name.

    The result is defined as the relation of kcolumns obtained byerasing the columns that are not listed

    Duplicate rows removed from result, since relations are sets

    Example: To eliminate the branch_nameattribute of account

    account_number, balance(account)

    )(,,, 21 rkAAA

  • 8/3/2019 Query Languages (basic operator)

    7/21

    Project Operation Example

    Relation r: A B C

    10

    20

    30

    40

    1

    1

    1

    2

    A C

    1

    1

    1

    2

    =

    A C

    1

    1

    2

    A,C (r)

  • 8/3/2019 Query Languages (basic operator)

    8/21

    Find those customers who live in xyz :

    customer_name(cutomer-city = xyz(account))

    Composition of Operations

  • 8/3/2019 Query Languages (basic operator)

    9/21

    Union Operation

    Notation: rs

    Defined as:

    r s= {t| tror ts}

    For rsto be valid.

    1. r,smust have the samearity (same number of attributes)

    2. The attribute domains must be compatible (example: 2nd

    column of rdeals with the same type of values as does the 2nd

    column of s)

    Example: to find all customers with either an account or a loan

    customer_name(depositor) customer_name(borrower)

  • 8/3/2019 Query Languages (basic operator)

    10/21

    Union Operation Example

    Relations r, s:

    r s:

    A B

    1

    2

    1

    A B

    2

    3

    rs

    A B

    1

    21

    3

  • 8/3/2019 Query Languages (basic operator)

    11/21

    Set Difference Operation

    Notation r s

    Defined as:

    r s = {t| trand t s}

    Set differences must be taken between compatiblerelations.

    rand smust have the same arity

    attribute domains of rand smust be compatible

  • 8/3/2019 Query Languages (basic operator)

    12/21

    Set Difference Operation Example

    Relations r, s:

    r s:

    A B

    1

    2

    1

    A B

    2

    3

    r

    s

    A B

    11

  • 8/3/2019 Query Languages (basic operator)

    13/21

    Cartesian-Product Operation

    Notation rx s

    Defined as:

    rx s= {t q| t rand qs}

    Assume that attributes of r(R) and s(S) are disjoint. (That is, R

    S= ). If attributes of r(R) and s(S) are not disjoint, then renaming must

    be used.

  • 8/3/2019 Query Languages (basic operator)

    14/21

    Cartesian-Product Operation Example

    Relations r, s:

    rx s:

    A B

    1

    2

    A B

    111

    12222

    C D

    101020

    1010102010

    E

    aab

    baabb

    C D

    10102010

    E

    aabbr

    s

  • 8/3/2019 Query Languages (basic operator)

    15/21

    Composition of Operations Can build expressions using multiple operations

    Example: A=C(r x s)

    r x s

    A=C(r x s)

    A B

    111

    12222

    C D

    101020

    1010102010

    E

    aab

    baabb

    A B C D E

    122

    101020

    aab

  • 8/3/2019 Query Languages (basic operator)

    16/21

    Rename Operation

    Allows us to name, and therefore to refer to, the results of

    relational-algebra expressions. Allows us to refer to a relation by more than one name.

    Example:

    x(E)

    returns the expression Eunder the name X

    If a relational-algebra expression Ehas arity n, then

    returns the result of expression Eunder the name X, and with the

    attributes renamed to A1 , A2, ., An.

    )(),...,,( 21E

    nAAAx

  • 8/3/2019 Query Languages (basic operator)

    17/21

    Banking Example

    branch (branch_name, branch_city, assets)

    customer (customer_name, customer_street, customer_city)

    account (account_number, branch_name, balance)

    loan (loan_number, branch_name, amount)

    depositor (customer_name, account_number)

    borrower(customer_name, loan_number)

  • 8/3/2019 Query Languages (basic operator)

    18/21

    Example Queries

    Find all loans of over $1200

    Find the loan number for each loan of an amount greater than

    $1200

    amount> 1200 (loan)

    loan_number(amount> 1200 (loan))

    Find the names of all customers who have a loan, an account, or both,

    from the bank

    customer_name(borrower) customer_name(depositor)

  • 8/3/2019 Query Languages (basic operator)

    19/21

    Example Queries

    Find the names of all customers who have a loan at the Perryridge branch.

    Query 1

    customer_name (branch_name = Perryridge(

    borrower.loan_number = loan.loan_number (borrower x loan)))

  • 8/3/2019 Query Languages (basic operator)

    20/21

    Example Queries

    Find the names of all customers who have a loan at the Perryridgebranch but do not have an account at any branch of the bank.

    customer-name(branch-name = Perryridge

    (borrower.loan-number = loan.loan-number(borrower x loan))) customer-name(depositor)

  • 8/3/2019 Query Languages (basic operator)

    21/21

    Formal Definition

    A basic expression in the relational algebra consists of either one of the

    following: A relation in the database

    A constant relation

    Let E1 and E2 be relational-algebra expressions; the following are all

    relational-algebra expressions:

    E1E2

    E1E2

    E1 x E2

    p(E1), Pis a predicate on attributes in E1

    s(E1), Sis a list consisting of some of the attributes in E1

    x(E1), x is the new name for the result of E1