ENCh08

Embed Size (px)

Citation preview

  • 8/9/2019 ENCh08

    1/76

    Copyright © 2004 Pearson Education, Inc.

    Chapter 8SQL-99: Schema

    Definition, BasicConstraints, and Queries

  • 8/9/2019 ENCh08

    2/76

    Slide 8-2Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    Data Definition, Constraints,

    and Schema Changes%sed to CRE&'E, (R)P, and &*'ER the

    descriptions o+ the tales relations o+ a

    dataaseSynta/

    CRE&'E (&'&1&SE dataasenae 3

    CRE&'E '&1*E talenae 

    colunnae datatype, 

    colunnae2 datatype, ....... 

  • 8/9/2019 ENCh08

    3/76

    Slide 8-5Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    CREATE TABLE

    Speci+ies a ne6 ase relation y gi$ing it a nae,and speci+ying each o+ its attriutes and their datatypes I#'E7ER, *)&', (ECI9&*i,:,C;&Rn,

  • 8/9/2019 ENCh08

    4/76

    Slide 8-4Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    CREATE TABLE: Data Type

  • 8/9/2019 ENCh08

    5/76

    Slide 8-=Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    CREATE TABLE

    In S>*2, can use the CRE&'E '&1*E coand +orspeci+ying the priary "ey attriutes, secondary "eys, and

    re+erential integrity constraints +oreign "eys. ?ey attriutes can e speci+ied $ia the PRI9&R@ ?E@ and

    %#I>%E phrases

    CREATE TABLE DEPT

    ( DNAME VARCHAR(10) NOT NULL,DNUMBER INTEGER NOT NULL,

    MGRSSN CHAR(9),MGRSTARTDATE CHAR(9),PRIMARY KEY (DNUMBER),UNIQUE (DNAME),FOREIGN KEY (MGRSSN) REFERENCES EMP );

  • 8/9/2019 ENCh08

    6/76

    Slide 8-AElmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    DROP TABLE

    %sed to reo$e a relation ase tale and

    its definition

    'he relation can no longer e used in

    Bueries, updates, or any other coands

    since its description no longer e/ists

    E/aple

    DROP TABLE DEPENDENT;

  • 8/9/2019 ENCh08

    7/76Slide 8-Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

     ALTER TABLE

    %sed to add an attriute to one o+ the ase relations

    'he ne6 attriute 6ill ha$e #%**s in all the tuples o+ the

    relation right a+ter the coand is e/ecuted3 hence, the

     #)' #%** constraint is not allowed   +or such an attriute

    E/aple

    ALTER TABLE EMPLOYEE ADD JOB

    VARCHAR(1);

    'he dataase users ust still enter a $alue +or the ne6

    attriute D)1 +or each E9P*)@EE tuple. 'his can e

    done using the %P(&'E coand.

  • 8/9/2019 ENCh08

    8/76Slide 8-8Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    Featres Added in S!L" and

    S!L#$$CREATE SCHEMAREFERENTIAL INTEGRITY

    OPTIONS

  • 8/9/2019 ENCh08

    9/76Slide 8-Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    CREATE SC%E&A

    the stateent can include all the eleentsFde+initions. G Schea eleents include tales, constraints, $ie6s,

    doains, and other constructs such as authori!ation

    grants&lternati$ely, the schea can e assigned a

    nae and authori!ation identi+ier. 'he eleentscan e de+ined later.

    EH schea o6ned y the user 6ith DS9I'; G CRE&'E SC;E9& copany &%';)RI&'I)#

    DS9I';3

  • 8/9/2019 ENCh08

    10/76Slide 8-0Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

     Attri'tes Constraints

    Speci+y constraint on attriute

     G (#%91ER I#' #)' #%** CHECK  

    (#%91ER J 0 ( (#%91ER K 2

    &lternati$ely

     G CREATE DOMAIN (#%9 &S I#'E7ER

    C;EC? (#%9J0 ( (#%9K2

     G (#%91ER (#%9

  • 8/9/2019 ENCh08

    11/76Slide 8-Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

     Additiona( Data Types in

    S!L" and S!L#$$;as (&'E, 'I9E, and 'I9ES'&9P data typesDATE!

     G  9ade up o+ year-onth-day in the +orat yyyy--dd

    TIME! G  9ade up o+ hourinutesecond in the +orat hhss

    TIME(")! G  9ade up o+ hourinutesecond plus i additional digits

    speci+ying +ractions o+ a second G  +orat is hhssii...i

    TIMESTAMP! G  ;as oth (&'E and 'I9E coponents

  • 8/9/2019 ENCh08

    12/76Slide 8-2Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

     Additiona( Data Types in

    S!L" and S!L#$$ )cont*+INTERVAL!

     G  Speci+ies a relati$e $alue rather than an asolute $alue

     G  Can e (&@L'I9E inter$als or @E&RL9)#';

    inter$als G  Can e positi$e or negati$e 6hen added to or

    sutracted +ro an asolute $alue, the result is anasolute $alue

  • 8/9/2019 ENCh08

    13/76Slide 8-5Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    Referentia( ntegrity )R+

    RI constraints can e $iolated 6hen tuples

    are inserted or deleted, or 6hen a +oreign

    "ey or priary "ey is odi+ied

    (e+ault action

     G Re:ect the update operation that 6ill cause the

    $iolation

    R#$#%#&'" '%"**#%#+ '"-&. 

     G SE' #%**, SE' (E&%*', C&SC&(E

     G Mhen )# (E*E'E or )# %P(&'E

  • 8/9/2019 ENCh08

    14/76

  • 8/9/2019 ENCh08

    15/76Slide 8-=Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    REFERE-TAL -TE.RT/OPTO-S )contined+

    CREATE TABLE EMP( ENAME VARCHAR(/0) NOT NULL,

    ESSN CHAR(9) %"%2 3#2,

    BDATE DATE,DNO INTEGER DEFAULT 1 4#3 (+&-50 &+ +&-69)

    SUPERSSN CHAR(9),7 PRIMARY KEY (ESSN),FOREIGN KEY (DNO) REFERENCES DEPT

      ON DELETE SET DEFAULT ON UPDATE CASCADE,FOREIGN KEY (SUPERSSN) REFERENCES EMP

      ON DELETE SET NULL ON UPDATE CASCADE );

    I+ the tuples +or a super$ising eployee is deleted, the $alue o+

    S%PERSS# is set to #%**.

    I+ SS# is updated, the ne6 $alue is cascaded to S%PERSS#.

  • 8/9/2019 ENCh08

    16/76Slide 8-AElmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    Retrie0a( !eries in S!L

    S>* has one asic stateent +or retrie$ing in+oration

    +ro a dataase3 the SE*EC' stateent

    'his is not the same as  the SE*EC' operation o+ the

    relational algera

    Iportant distinction et6een S>* and the +oral

    relational odel3 S>* allo6s a tale relation to ha$e t6o

    or ore tuples that are identical in all their attriute $alues

    ;ence, an S>* relation tale is a multi-set   soeties

    called a ag o+ tuples3 it is not   a set o+ tuplesS>* relations can e constrained to e sets y speci+ying

    PRI9&R@ ?E@ or %#I>%E attriutes, or y using the(IS'I#C' option in a Buery

  • 8/9/2019 ENCh08

    17/76Slide 8-Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    Retrie0a( !eries in S!L

    )cont*+1asic +or o+ the S>* SE*EC' stateent is called amapping   or a SELECT-FROM-WHERE blo! 

    SELECT  Kattriute listJ

    FROM  Ktale listJ

    8HERE KconditionJ

     G  Kattriute listJ is a list o+ attriute naes 6hose $alues are to eretrie$ed y the Buery

     G  Ktale listJ is a list o+ the relation naes reBuired to process theBuery

     G  KconditionJ is a conditional 1oolean e/pression that identi+iesthe tuples to e retrie$ed y the Buery

  • 8/9/2019 ENCh08

    18/76Slide 8-8Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    Re(ationa( Data'ase Schema##Figre 1*1 httpLL6e.ntnu.edu.t6LNsatsengL

  • 8/9/2019 ENCh08

    19/76Slide 8-Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    Pop(ated

    Data'ase##Fig*1*2

  • 8/9/2019 ENCh08

    20/76Slide 8-20

    Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth EditionCopyright © 2004 Rae! Elasri and Sha"ant #a$athe

    Simp(e S!L !eries

    1asic S>* Bueries correspond to using the SE*EC', PR)DEC', andD)I# operations o+ the relational algera

    &ll suseBuent e/aples use the C)9P@ dataase

    E/aple o+ a siple Buery on one  relation

    >uery 0 Retrie$e the irthdate and address o+ the eployee 6hose

    nae is ODohn 1. SithO.

    Q0! SELECT BDATE, ADDRESSFROM EMPLOYEE8HERE FNAME:J-4&: AND MINIT:B

      AND LNAME:S"'4

     G  Siilar to a SE*EC'-PR)DEC' pair o+ relational algera operations3 theSE*EC'-clause speci+ies the p"o#etion att"ibutes and the M;ERE-clausespeci+ies the seletion ondition

     G  ;o6e$er, the result o+ the Buery ma$ ontain  duplicate tuples

  • 8/9/2019 ENCh08

    21/76Slide 8-2

    Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth EditionCopyright © 2004 Rae! Elasri and Sha"ant #a$athe

    Simp(e S!L !eries )cont*+

    >uery Retrie$e the nae and address o+ all eployees 6ho 6or"+or the OResearchO departent.

    Q1! SELECT FNAME, LNAME, ADDRESSFROM EMPLOYEE, DEPARTMENT

    8HERE DNAME:R#.#%4: ANDDNUMBERDNO

     G  Siilar to a SE*EC'-PR)DEC'-D)I# seBuence o+ relationalalgera operations

     G (#&9EOResearchO is a seletion ondition  corresponds to aSE*EC' operation in relational algera

     G  (#%91ER(#) is a #oin ondition corresponds to a D)I#operation in relational algera

  • 8/9/2019 ENCh08

    22/76Slide 8-22

    Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth EditionCopyright © 2004 Rae! Elasri and Sha"ant #a$athe

    Simp(e S!L !eries )cont*+

    >uery 2 or e$ery pro:ect located in OSta++ordO, list the pro:ect nuer, thecontrolling departent nuer, and the departent anagerOs last nae,address, and irthdate.

    Q! SELECT PNUMBER, DNUM, LNAME, BDATE, ADDRESS

    FROM PROJECT, DEPARTMENT, EMPLOYEE8HERE DNUMDNUMBER AND MGRSSNSSNAND PLOCATION:S'$$-%+:

     G  In >2, there are two  :oin conditions G  'he :oin condition (#%9(#%91ER relates a pro:ect to its controlling

    departent

     G  'he :oin condition 97RSS#SS# relates the controlling departent tothe eployee 6ho anages that departent

  • 8/9/2019 ENCh08

    23/76Slide 8-25

    Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth EditionCopyright © 2004 Rae! Elasri and Sha"ant #a$athe

     A(iases, 3 and DST-CT,

    Empty 4%ERE#c(ase In S>*, 6e can use the sae nae +or t6o or oreattriutes as long as the attriutes are in diffe"ent "elations

    & Buery that re+ers to t6o or ore attriutes 6ith the sae

    nae ust %ualif$  the attriute nae 6ith the relationnae y p"efi&ing   the relation nae to the attriute nae

    E/aple

    E9P*)@EE.*#&9E, (EP&R'9E#'.(#&9E

  • 8/9/2019 ENCh08

    24/76Slide 8-24

    Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth EditionCopyright © 2004 Rae! Elasri and Sha"ant #a$athe

     ALASES

    Soe Bueries need to re+er to the sae relation t6ice

    In this case, aliases  are gi$en to the relation nae

    >uery 8 or each eployee, retrie$e the eployeeOs nae, and the naeo+ his or her iediate super$isor.

    Q

    FROM EMPLOYEE E S8HERE E=SUPERSSNS=SSN

     G  In >8, the alternate relation naes E and S are called aliases  or tuple'a"iables +or the E9P*)@EE relation

     G  Me can thin" o+ E and S as t6o diffe"ent opies  o+ E9P*)@EE3 Erepresents eployees in role o+ supe"'isees  and S representseployees in role o+ supe"'iso"s

  • 8/9/2019 ENCh08

    25/76

    Slide 8-2=Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

     ALASES )cont*+

     G  &liasing can also e used in any S>* Buery +or con$enienceCan also use the &S "ey6ord to speci+y aliases

    Q

    FROM EMPLOYEE AS E, EMPLOYEE AS S8HERE E=SUPERSSNS=SSN

  • 8/9/2019 ENCh08

    26/76

    Slide 8-2AElmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    5-SPECFED

    4%ERE#c(ase& missing WHERE-lause  indicates no condition3 hence,all tuples  o+ the relations in the R)9-clause are selected

    'his is eBui$alent to the condition M;ERE 'R%E

    >uery Retrie$e the SS# $alues +or all eployees.

    Q9! SELECT SSN

    FROM EMPLOYEE

    I+ ore than one relation is speci+ied in the R)9-clause

    and   there is &- >-"& -&+"'"-&, '4#& '4# CARTESIAN

     PRODUCT -$ '?#. ". .##'#+

  • 8/9/2019 ENCh08

    27/76

    Slide 8-2Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    5-SPECFED

    4%ERE#c(ase )cont*+E/aple

    Q10! SELECT SSN, DNAME

    FROM EMPLOYEE, DEPARTMENT

     G  It is e/treely iportant not to o$erloo" speci+ying any selection and :oin conditions in the M;ERE-clause3 other6ise, incorrect and $erylarge relations ay result

  • 8/9/2019 ENCh08

    28/76

    Slide 8-28Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    Simp(e E6amp(escreate dataase coursesstudents3

    use coursesstudents3

    Q 'ale de+initioncreate tale courses

      cid int0 #)' #%** autoincreent,

      cnae $archar50 #)' #%**,

      credit tinyint2 #)' #%**,

      croo $archar0,

      priary "ey cid3

    create tale students

      sid int0 unsigned #)' #%**autoincreent,

      snae $archar50 #)' #%** de+ault OO,

      syear tinyint2 unsigned,

      priary "ey sid3

    create tale selected

      cid int0 unsigned,

      sid int0 unsigned

    3

    Q (atainsert into courses $alues , O(19SO, 5, O50O3

    insert into courses $alues 2, O)SO, 5, O40AO3insert into courses $alues 5, O&lgorithO, 5, O50O3

    insert into students $alues , OSaO, 43

    insert into students $alues 2, ODoeO, 53

    insert into students $alues 5, O9aryO, 53

    insert into students $alues 4, ODohnO, 53

    insert into selected $alues , 3

    insert into selected $alues , 23

    insert into selected $alues , 43

    insert into selected $alues 2, 3

    insert into selected $alues 2, 53

    insert into selected $alues 2, 43

    insert into selected $alues 2, 23

    insert into selected $alues 5, 23

    insert into selected $alues 5, 43

  • 8/9/2019 ENCh08

    29/76

    Slide 8-2Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    7oin Operations

    select selected.cid, selected.sid, snae+ro selected, students

    @4#%# .##'#+=."+ .'?+#&'.=."+;

    -------------------

    cid sid snae

    ------------------- 2 Doe

    4 Dohn

    Sa

    2 2 Doe

    2 5 9ary

    2 4 Dohn

    2 Sa

    5 4 Dohn

    5 2 Doe

    -------------------

    select T +ro

    students3

    -------------------

    sid snae syear

    -------------------

    Sa 4

    2 Doe 5

    5 9ary 5 4 Dohn 5

    -------------------

    select T +ro selected3

    ------------ cid sid

    ------------

    2

    4

    2

    2 5

    2 4

    2 2

    5 2

    5 4 ------------

    I+ oitted,

    5A ro6s6ill result.

  • 8/9/2019 ENCh08

    30/76

    Slide 8-50Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    5SE OF 3

    'o retrie$e all the attriute $alues o+ the selected tuples, a T is

    used, 6hich stands +or all the att"ibutes

    E/aples

    Q1C! SELECT

    FROM EMPLOYEE

    8HERE DNO

    Q1D! SELECT FROM EMPLOYEE, DEPARTMENT

    8HERE DNAME:R#.#%4: AND

    DNODNUMBER 

  • 8/9/2019 ENCh08

    31/76

    Slide 8-5Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    5SE OF DST-CT

    S>* does not treat a relation as a set3 dupliate tuples an

    appea" 

    'o eliinate duplicate tuples in a Buery result, the

    "ey6ord DISTINCT is used

    or e/aple, the result o+ > ay ha$e duplicate

    S&*&R@ $alues 6hereas >& does not ha$e any

    duplicate $alues

    Q11! SELECT SALARYFROM EMPLOYEE

    Q11A! SELECT DISTINCT SALARY

    FROM EMPLOYEE

  • 8/9/2019 ENCh08

    32/76

  • 8/9/2019 ENCh08

    33/76

    Slide 8-55Elmasri and Navathe, undamentals of Data!ase S"stems,

    Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    SET OPERATO-S )cont*+ 

    >uery 4 9a"e a list o+ all pro:ect nuers +or pro:ects that in$ol$e aneployee 6hose last nae is OSithO as a 6or"er or as a anager o+

    the departent that controls the pro:ect.

    Q! (SELECT PNAME

    FROM PROJECT, DEPARTMENT, EMPLOYEE8HERE DNUMDNUMBER AND MGRSSNSSN

    AND LNAME:S"'4:)

    UNION (SELECT PNAME

    FROM PROJECT, 8ORKSON, EMPLOYEE

    8HERE PNUMBERPNO AND ESSNSSN ANDLNAME:S"'4:)

  • 8/9/2019 ENCh08

    34/76

    Slide 8-54Elmasri and Navathe, undamentals of Data!ase S"stems,

    Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    -EST-. OF !5ERES

    & coplete SE*EC' Buery, called a nested %ue"$ , can e speci+ied6ithin the M;ERE-clause o+ another Buery, called the oute" %ue"$

    9any o+ the pre$ious Bueries can e speci+ied in an alternati$e +or

    using nesting

    >uery Retrie$e the nae and address o+ all eployees 6ho 6or"

    +or the OResearchO departent.

    Q1! SELECT FNAME, LNAME, ADDRESS

    FROM EMPLOYEE

    8HERE DNO IN (SELECT DNUMBER  

    FROM DEPARTMENT8HERE DNAME:R#.#%4: )

  • 8/9/2019 ENCh08

    35/76

    Slide 8-5=Elmasri and Navathe, undamentals of Data!ase S"stems,

    Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    -EST-. OF !5ERES

    )cont*+

    'he nested Buery selects the nuer o+ the OResearchO departent

    'he outer Buery select an E9P*)@EE tuple i+ its (#) $alue is in theresult o+ either nested Buery

    'he coparison operator IN copares a $alue $ 6ith a set or ulti-seto+ $alues

  • 8/9/2019 ENCh08

    36/76

  • 8/9/2019 ENCh08

    37/76

    Slide 8-5Elmasri and Navathe, undamentals of Data!ase S"stems,

    Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    CORRELATED -ESTED

    !5ERES )cont*+ G  In >2, the nested Buery has a di++erent result fo" eah tuple  in the outer

    Buery

     G  & Buery 6ritten 6ith nested SE*EC'... R)9... M;ERE... loc"s andusing the or I# coparison operators can always  e e/pressed as asingle loc" Buery. or e/aple, >2 ay e 6ritten as in >2&

    Q1A! SELECT E=FNAME, E=LNAMEFROM EMPLOYEE E, DEPENDENT D8HERE E=SSND=ESSN AND

    E=FNAMED=DEPENDENTNAME

     G  'he original S>* as speci+ied +or S@S'E9 R also had a CONTAINS coparison operator, 6hich is used in con:unction 6ith nested correlatedBueries

     G  'his operator 6as dropped +ro the language, possily ecause o+ thedi++iculty in ipleenting it e++iciently

  • 8/9/2019 ENCh08

    38/76

    Slide 8-58Elmasri and Navathe, undamentals of Data!ase S"stems,

    Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    CORRELATED -ESTED

    !5ERES )cont*+

     G  9ost ipleentations o+ S>* do not   ha$e this operator 

     G  'he C)#'&I#S operator copares t6o sets of 'alues , and returns 'R%Ei+ one set contains all $alues in the other set  reiniscent o+ the di'ision operation o+ algera.

    >uery 5 Retrie$e the nae o+ each eployee 6ho 6or"s onall   the pro:ectscontrolled y departent nuer =.

    Q/! SELECT FNAME, LNAMEFROM EMPLOYEE8HERE ( (SELECT PNO

      FROM 8ORKSON

      8HERE SSNESSN)  CONTAINS  (SELECT PNUMBER    FROM PROJECT  8HERE DNUM) )

  • 8/9/2019 ENCh08

    39/76

    Slide 8-5Elmasri and Navathe, undamentals of Data!ase S"stems,

    Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    CORRELATED -ESTED

    !5ERES )cont*+

     G  In >5, the second nested Buery, 6hich is not correlated6ith the outer Buery, retrie$es the pro:ect nuers o+ all

     pro:ects controlled y departent =

     G  'he +irst nested Buery, 6hich is correlated, retrie$es the

     pro:ect nuers on 6hich the eployee 6or"s, 6hich isdi++erent fo" eah emplo$ee tuple  ecause o+ thecorrelation

  • 8/9/2019 ENCh08

    40/76

    Slide 8-40Elmasri and Navathe, undamentals of Data!ase S"stems,

    Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    T%E E8STS F5-CTO-

    EHIS'S is used to chec" 6hether the result

    o+ a correlated nested Buery is epty

    contains no tuples or not

    Me can +orulate >uery 2 in an

    alternati$e +or that uses EHIS'S as >21

     elo6

  • 8/9/2019 ENCh08

    41/76

  • 8/9/2019 ENCh08

    42/76

  • 8/9/2019 ENCh08

    43/76

    Slide 8-45Elmasri and Navathe, undamentals of Data!ase S"stems,

    Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    E8PLCT SETS

    It is also possile to use an #""' (#&?#%'#+) .#' -$

    ?#. in the M;ERE-clause rather than a nested Buery

    >uery 5 Retrie$e the social security nuers o+ all

    eployees 6ho 6or" on pro:ect nuer , 2, or 5.

    Q1/! SELECT DISTINCT ESSN

    FROM 8ORKSON

    8HERE PNO IN (1, , /)

  • 8/9/2019 ENCh08

    44/76

    Slide 8-44Elmasri and Navathe, undamentals of Data!ase S"stems,

    Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    -5LLS - S!L !5ERES

    S>* allo6s Bueries that chec" i+ a $alue is #%** issingor unde+ined or not applicale

    S>* uses IS or IS NOT to copare #%**s ecause it

    considers each #%** $alue distinct +ro other #%**

    $alues, so eBuality coparison is not appropriate .>uery 4 Retrie$e the naes o+ all eployees 6ho do not

    ha$e super$isors.

    Q1! SELECT FNAME, LNAME

    FROM EMPLOYEE8HERE SUPERSSN IS NULL

     #ote I+ a :oin condition is speci+ied, tuples 6ith #%**

    $alues +or the :oin attriutes are not included in the result

  • 8/9/2019 ENCh08

    45/76

    Slide 8-4=Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    7oined Re(ations Featre

    in S!L"Can speci+y a U:oined relationU in the R)9-clause

    *oo"s li"e any other relation ut is the result o+ a :oin

    &llo6s the user to speci+y di++erent types o+ :oins regular

    UthetaU D)I#, #&'%R&* D)I#, *E' )%'ER D)I#,RI7;' )%'ER D)I#, CR)SS D)I#, etc

  • 8/9/2019 ENCh08

    46/76

    Slide 8-4AElmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    7oined Re(ations Featre

    in S!L" )cont*+ E/aples

    Q

    can e 6ritten as #ote there is one ro6 in di++erence

    Q

    ON E=SUPERSSNS=SSN)

    Q1! SELECT FNAME, LNAME, ADDRESSFROM EMPLOYEE, DEPARTMENT8HERE DNAME:R#.#%4: AND DNUMBERDNO

  • 8/9/2019 ENCh08

    47/76

    Slide 8-4Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    7oined Re(ations Featre

    in S!L" )cont*+ could e 6ritten as

    Q1! SELECT FNAME, LNAME, ADDRESSFROM (EMPLOYEE JOIN DEPARTMENT

     ON DNUMBERDNO)8HERE DNAME:R#.#%4

    or as

    Q1! SELECT FNAME, LNAME, ADDRESSFROM (EMPLOYEE NATURAL JOIN DEPARTMENT

     AS DEPT(DNAME, DNO, MSSN, MSDATE)8HERE DNAME:R#.#%4

  • 8/9/2019 ENCh08

    48/76

    Slide 8-48Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    7oined Re(ations Featre

    in S!L" )cont*+&nother E/aple3

     G  >2 could e 6ritten as +ollo6s3 this illustrates ultiple :oins in the :oined tales

    Q! SELECT PNUMBER, DNUM,LNAME, BDATE,ADDRESS

    FROM ((PROJECT JOINDEPARTMENT ON

    DNUMDNUMBER) JOINEMPLOYEE ONMGRSSNSSN)

    8HERE PLOCATION:S'$$-%+

  • 8/9/2019 ENCh08

    49/76

    Slide 8-4Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

     A..RE.ATE F5-CTO-S

    Include COUNT, SUM, MA, MIN, and AVG

    >uery = ind the a/iu salary, the iniu salary, and

    the a$erage salary aong all eployees.

    Q1! SELECT MA(SALARY),

    MIN(SALARY), AVG(SALARY)

    FROM EMPLOYEE

     G  Soe S>* ipleentations ma$ not allow mo"e than one funtion  in the SE*EC'-clause

  • 8/9/2019 ENCh08

    50/76

    Slide 8-=0Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

     A..RE.ATE F5-CTO-S

    )cont*+>uery A ind the a/iu salary, the iniu salary,

    and the a$erage salary aong eployees 6ho 6or" +or theOResearchO departent.

    Q1! SELECT MA(SALARY), MIN(SALARY),AVG(SALARY)

    FROM EMPLOYEE, DEPARTMENT8HERE DNODNUMBER AND

    DNAME:R#.#%4:

  • 8/9/2019 ENCh08

    51/76

    Slide 8-=Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

     A..RE.ATE F5-CTO-S

    )cont*+>ueries and 8 Retrie$e the total nuer o+ eployees

    in the copany >, and the nuer o+ eployees in theOResearchO departent >8.

    Q1! SELECT COUNT ()FROM EMPLOYEE

    Q1

    DEPARTMENT8HERE DNODNUMBER AND

    DNAME:R#.#%4

  • 8/9/2019 ENCh08

    52/76

    Slide 8-=2Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    .RO5P-.

    In any cases, 6e 6ant to apply the aggregate+unctions to subg"oups of tuples in a "elation

    Each sugroup o+ tuples consists o+ the set o+tuples that ha$e the same 'alue  +or the g"oupingatt"ibute(s)

    'he +unction is applied to each sugroupindependently

    S>* has a GROUP BY-clause +or speci+ying thegrouping attriutes, 6hich must also appea" in theSELECT-lause

  • 8/9/2019 ENCh08

    53/76

  • 8/9/2019 ENCh08

    54/76

  • 8/9/2019 ENCh08

    55/76

    Slide 8-==Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    T%E %A9-.#CLA5SE

    Soeties 6e 6ant to retrie$e the $alues

    o+ these +unctions +or only those g"oups that

     satisf$ e"tain onditions

    'he ;&

  • 8/9/2019 ENCh08

    56/76

    S S . CO SO

  • 8/9/2019 ENCh08

    57/76

    Slide 8-=Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    S5BSTR-. CO&PARSO-

    'he LIKE coparison operator is used to

    copare partial strings

    '6o reser$ed characters are used OVO or OTO

    in soe ipleentations replaces anaritrary nuer o+ characters, and OO

    replaces a single aritrary character 

  • 8/9/2019 ENCh08

    58/76

    S5BSTR-. CO&PARSO-

  • 8/9/2019 ENCh08

    59/76

    Slide 8-=Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    S5BSTR-. CO&PARSO-

    )cont*+>uery 2A Retrie$e all eployees 6ho 6ere orn during the

    =0s. ;ere, O=O ust e the 8th character o+ the stringaccording to our +orat +or date, so the 1(&'E $alue isO=O, 6ith each underscore as a place holder +or a

    single aritrary character.

    Q! SELECT FNAME, LNAMEFROM EMPLOYEE8HERE BDATE LIKE :

    'he *I?E operator allo6s us to get around the +act that each$alue is considered atoic and indi$isile3 hence, in S>*,character string attriute $alues are not atoic

    ART%&ETC OPERATO-S

  • 8/9/2019 ENCh08

    60/76

    Slide 8-A0Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

     ART%&ETC OPERATO-S

    'he standard arithetic operators OO, O-O. OTO, and OLO +or addition,

    sutraction, ultiplication, and di$ision, respecti$ely can e

    applied to nueric $alues in an S>* Buery result

    >uery 2 Sho6 the e++ect o+ gi$ing all eployees 6ho 6or"

    on the OProductHO pro:ect a 0V raise.

    Q!SELECT FNAME, LNAME, 1=1SALARY

    FROM EMPLOYEE, 8ORKSON, PROJECT

    8HERE SSNESSN AND PNOPNUMBER AND

    PNAME:P%-+?'

    ORDER B/

  • 8/9/2019 ENCh08

    61/76

    Slide 8-AElmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    ORDER B/

    'he ORDER BY clause is used to sort the tuples in aBuery result ased on the $alues o+ soe attriutes

    >uery 28 Retrie$e a list o+ eployees and the

     pro:ects each 6or"s in, ordered y the eployeeOsdepartent, and 6ithin each departent orderedalphaetically y eployee last nae.

    Q

      FROM DEPARTMENT, EMPLOYEE,8ORKSON, PROJECT

    8HERE DNUMBERDNO AND SSNESSNAND PNOPNUMBER  ORDER BY DNAME, LNAME

  • 8/9/2019 ENCh08

    62/76

    S f S!L ! i

  • 8/9/2019 ENCh08

    63/76

    Slide 8-A5Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    Smmary of S!L !eries

    & Buery in S>* can consist o+ up to si/ clauses, ut onlythe +irst t6o, SE*EC' and R)9, are andatory. 'he

    clauses are speci+ied in the +ollo6ing order

    SELECT 6''%"?'# ".'5FROM 6'# ".'5

    8HERE 6-&+"'"-&5

    GROUP BY 6*%-?"&* ''%"?'#(.)5

    HAVING 6*%-? -&+"'"-&5

    ORDER BY 6''%"?'# ".'5

  • 8/9/2019 ENCh08

    64/76

    S if i 5 d t i S!L

  • 8/9/2019 ENCh08

    65/76

    Slide 8-A=Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    Specifying 5pdates in S!L

    'here are three S>* coands to odi+ythe dataase3 I#SER', (E*E'E, and

    %P(&'E

    -SERT

  • 8/9/2019 ENCh08

    66/76

    Slide 8-AAElmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    -SERT

    In its siplest +or, it is used to add one orore tuples to a relation

    &ttriute $alues should e listed in the

    sae order as the attriutes 6ere speci+iedin the CRE&'E '&1*E coand

    -SERT ) t +

  • 8/9/2019 ENCh08

    67/76

    Slide 8-AElmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    -SERT )cont*+

    E/aple

    U1! INSERT INTO EMPLOYEEVALUES (:R"4%+:,:K:,:M%"&":, :/9

  • 8/9/2019 ENCh08

    68/76

    Slide 8-A8Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    -SERT )cont*+

    Iportant #ote )nly the constraints speci+ied inthe ((* coands are autoatically en+orced ythe (19S 6hen updates are applied to thedataase

    &nother $ariation o+ I#SER' allo6s insertion o+multiple tuples  resulting +ro a Buery into arelation

    -SERT ) t +

  • 8/9/2019 ENCh08

    69/76

    Slide 8-AElmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    -SERT )cont*+

     G  E/aple Suppose 6e 6ant to create a teporary tale that has the nae,nuer o+ eployees, and total salaries +or each departent. & tale(EP'SI#) is created y %5&, and is loaded 6ith the suaryin+oration retrie$ed +ro the dataase y the Buery in %51.

    U/A! CREATE TABLE DEPTSINFO(DEPTNAME VARCHAR(10), NOOFEMPS INTEGER, TOTALSAL INTEGER);

    U/B! INSERT INTO DEPTSINFO (DEPTNAME,NOOFEMPS, TOTALSAL)

    SELECT DNAME, COUNT (), SUM (SALARY)

    FROM DEPARTMENT, EMPLOYEE8HERE DNUMBERDNOGROUP BY DNAME ;

    -SERT )cont +

  • 8/9/2019 ENCh08

    70/76

    Slide 8-0Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    -SERT )cont*+

     #ote 'he (EP'SI#) tale ay not e up-to-date i+ 6echange the tuples in either the (EP&R'9E#' or theE9P*)@EE relations afte"   issuing %51. Me ha$e tocreate a $ie6 see later to "eep such a tale up to date.

    DELETE

  • 8/9/2019 ENCh08

    71/76

    Slide 8-Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    DELETE

    Reo$es tuples +ro a relation Includes a M;ERE-clause to select the tuples to e deleted

    'uples are deleted +ro only one table  at a tie unlessC&SC&(E is speci+ied on a re+erential integrity

    constraint& issing M;ERE-clause speci+ies that all tuples  in the

    relation are to e deleted3 the tale then ecoes an eptytale

    'he nuer o+ tuples deleted depends on the nuer o+tuples in the relation that satis+y the M;ERE-clause

    Re+erential integrity should e en+orced

    DELETE )cont +

  • 8/9/2019 ENCh08

    72/76

    Slide 8-2Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    DELETE )cont*+

    E/aplesUA! DELETE FROM EMPLOYEE

    8HERE LNAME:B%-@&

    UB! DELETE FROM EMPLOYEE

    8HERE SSN:1/

  • 8/9/2019 ENCh08

    73/76

    Slide 8-5Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    5PDATE

    %sed to odi+y attriute $alues o+ one or oreselected tuples

    & M;ERE-clause selects the tuples to e

    odi+ied&n additional SE'-clause speci+ies the attriutes

    to e odi+ied and their ne6 $alues

    Each coand odi+ies tuples in the same

    "elation

    Re+erential integrity should e en+orced

    5PDATE )cont +

  • 8/9/2019 ENCh08

    74/76

    Slide 8-4Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    5PDATE )cont*+

    E/aple Change the location and controlling departentnuer o+ pro:ect nuer 0 to O1ellaireO and =,respecti$ely.

    U! UPDATE PROJECTSET PLOCATION :B#"%#:, DNUM 8HERE PNUMBER10

    5PDATE )cont +

  • 8/9/2019 ENCh08

    75/76

    Slide 8-=Elmasri and Navathe, undamentals of Data!ase S"stems, Fourth Edition

    Copyright © 2004 Rae! Elasri and Sha"ant #a$athe

    5PDATE )cont*+

    E/aple 7i$e all eployees in the OResearchO departent a 0V raisein salary.

    U! UPDATE EMPLOYEESET SALARY SALARY 1=1

    8HERE DNO IN (SELECT DNUMBER    FROM DEPARTMENT  8HERE DNAME:R#.#%4:)

    In this reBuest, the odi+ied S&*&R@ $alue depends on the originalS&*&R@ $alue in each tuple

    'he re+erence to the S&*&R@ attriute on the right o+ re+ers to the oldS&*&R@ $alue e+ore odi+ication

    'he re+erence to the S&*&R@ attriute on the le+t o+ re+ers to the ne6S&*&R@ $alue a+ter odi+ication

    %ome 4or

  • 8/9/2019 ENCh08

    76/76

    %ome 4or

    8.hand in 9yS>*-e/ecutale coands

    8.0 hand in 9yS>*-e/ecutale coands

    8. hand in 9yS>*-e/ecutale coands

    8.4 hand in 9yS>*-e/ecutale coands