MELJUN CORTES Manual RDBMS Database Csci30

download MELJUN CORTES Manual RDBMS Database Csci30

of 24

Transcript of MELJUN CORTES Manual RDBMS Database Csci30

  • 8/13/2019 MELJUN CORTES Manual RDBMS Database Csci30

    1/24

    CCSI30 RDBMS Development Tools

    DATABASES

    A database in Microsoft SQL Server 2000 consists of a collection of

    tables wit !ata" an! oter ob#ects" s$c as views" in!e%es" store! proce!$res"

    an! tri&&ers" tat are !efine! to s$pport te activities performe! wit te !ata'

    Before ob#ects witin te !atabase can be create!" (o$ m$st create te !atabase

    an! $n!erstan! ow to can&e te settin&s an! confi&$ration of te !atabase'

    Tis incl$!es tas)s s$c as e%pan!in& or srin)in& te !atabase" or specif(in&

    te files $se! to create te !atabase'

    DATABASE MANAGEMENT SYSTEM (DBMS)

    Database Management System is a collection of interrelate! !ata an! a

    set of pro&rams to access tose !ata' Te( are !esi&ne! to a!minister ver(

    lar&e information wic involves te !efinition of str$ct$res on ow te

    information will be store! an! te provision of te means to operate on te

    information'

    Advantages of using a Database Management System

    *revents !ata re!$n!anc( an! inconsistenc(

    Allows nee!e! !ata to be retrieve! in a convenient an! efficient

    approac

    Collects !ata in one stora&e me!i$m an! format

    Data inte&rit( is maintaine!

    Prepared by: ELJUN P. CORTES + , +

  • 8/13/2019 MELJUN CORTES Manual RDBMS Database Csci30

    2/24

    CCSI30 RDBMS Development Tools

    Atomicit( of processes is $pel!" i'e'" processes tat nee! to complete

    a tas) to&eter m$st appen in its entiret( or not at all or a fail$re in

    one process so$l! restore te !ate to te consistent state tat e%iste!

    before te fail$re occ$rre!

    Conc$rrent $ser+access to !ata is a$tomaticall( s$pervise!

    Sec$rit( can be enforce!

    STRUCTURE ! A RE"AT#NA" DATABASE

    Te data mode$is te $n!erl(in& str$ct$re of a !atabase an! is !efine!

    as a collection of concept$al tools for !escribin& !ata" !ata relationsips" !ata

    semantics" an! consistenc( constraints' Simpl(" it a wa( of or&ani-in& !ata in a

    !atabase'

    Microsoft SQL Server is a relational !atabase'

    Te relational mo!el represents !ata an! te relationsip amon& tose

    !ata b( or&ani-in& a series of two+!imensional tables' Tese tables"

    s$bse.$entl( referre! to as fiel!s" relate to one anoter base! on col$mns in te

    tables'

    A %eyi!entifies an! !efines a specific set of one or more col$mns in a

    table' A )e( allows $s to ma)e a !istinction between tin&s or ob#ects in te real

    worl! wen associate! to recor!s in a !atabase'

    Prepared by: ELJUN P. CORTES + 2 +

  • 8/13/2019 MELJUN CORTES Manual RDBMS Database Csci30

    3/24

    CCSI30 RDBMS Development Tools

    T&o Ty'es of eys

    ,' Primary Key + one or a combination of more tan one col$mn in a

    table tat $ni.$el( i!entifies ow recor!s are store! in tat table' Tis

    $ni.$e i!entit( of recor!s ens$res tat no !$plicate recor!s are store!

    in te !atabase

    2' Foreign Key + one or more col$mns in a table wose val$es in tose

    col$mns matc te val$es in te primar( )e( of anoter table' /orei&n

    )e( ens$res wat we call as referential integrityin a !atabase'

    ERAT#NS N A RE"AT#NA" DATABASE

    A*ue+y $anguage is a lan&$a&e in wic a $ser re.$ests information

    from te !atabase'

    Te +e$ationa$ a$geb+a is a .$er( lan&$a&e tat consists of a set of

    operations tat ta)e one or two relations as inp$t an! pro!$ce a new relation as

    teir res$lt'

    !undamenta$ 'e+ations of Re$ationa$ A$geb+a

    1. Select + !enote! b( te lowercase ree) letter si&ma 1" selects

    recor!s tat satisf( a &iven pre!icate' Te pre!icate is written as a

    s$bscript of wile te ar&$ment relation is witin a parentesis after

    te ' Comparisons $sin& " " 4" 4" 5" an! 5 are allowe! an! b(

    $sin& and1 an! or1" several pre!icates can be combine! to form

    more comple% select operations'

    Prepared by: ELJUN P. CORTES + 3 +

  • 8/13/2019 MELJUN CORTES Manual RDBMS Database Csci30

    4/24

    CCSI30 RDBMS Development Tools

    2' Project + !enote! b( te ree) letter pi 16" pro#ects onl( te specifie!

    attrib$tes of te recor!s written as a s$bscript to 6' Te ar&$ment

    relation bein& in te parentesis tat follows' Since te relation is a

    set" !$plicate recor!s are remove! from te res$lt'

    3. Union + !enote! b( te set teor( $nion 1" combine te res$lt of

    two compatible relations' Compatible" meanin&" te same n$mber of

    attrib$tes of te same t(pe'

    4. Set-Difference + !enote! b( " li)e in te e%pression rs" ret$rns

    recor!s tat are in one relation 1r b$t are not in anoter 1s'

    5. Cartesian Product + !enote! b( a cross 1" li)e in te e%pression

    r,r2" ret$rns eac possible pairs of recor!s7 one from te r, relation

    an! one from te r2 relation' To !istin&$is attrib$tes from !ifferent

    relations tat ave te same name" we attac te name of te relation

    from wic te attrib$te ori&inall( came'

    6. Rename + !enote! b( te lowercase ree) letter ro 1" ret$rns a

    relation $n!er a !ifferent name !enote! b( te s$bscript of ' Tis

    wo$l! be ver( $sef$l in &ettin& te Cartesian pro!$ct of a relation to

    itself wito$t avin& an ambi&$o$s reference to eac of te relations8

    attrib$tes'

    Prepared by: ELJUN P. CORTES + 9 +

  • 8/13/2019 MELJUN CORTES Manual RDBMS Database Csci30

    5/24

    CCSI30 RDBMS Development Tools

    DATA DE!#N#T#NS

    Database C+eation

    Syntax:

    Te s(nta% above is te simplest wa( of creatin& a !atabase wic

    places te !efa$lt files in SQL Server8s installation fol!ers'

    If we nee! to specif( te location of te primar( !atabase file an!

    its lo& file" we iss$e te followin& comman! tat contains te p(sical file

    name 1or te name of te file as it is written on te !is) an! also

    specif(in& te lo&ical name of te !ata 1or te name tat is $se! to refer to

    te ob#ect as part of te !atabase''

    Syntax:

    Data Ty'es

    Before we are able to create tables in o$r !atabase" we nee! to

    )now first ow ever( col$mn in ever( table will be store!' :ac m$st be

    !eclare! as a certain t(pe of !ata" )nown as a data type'

    bi&int Inte&er 1wole n$mber !ata from +2;3 1+?9==?>0> tro$& 2;3+ ,

    Prepared by: ELJUN P. CORTES + ? +

    CR:AT: DATABAS: 4DATABAS:@AM:5

    CR:AT: DATABAS: 4DATABAS:@AM:5

    1AM: DATA /IL: AM: /IL:AM: 4LCATI / 'MD/5L

    1AM: L /IL: AM:/IL:AM: 4LCATI / 'LD/5

  • 8/13/2019 MELJUN CORTES Manual RDBMS Database Csci30

    6/24

    CCSI30 RDBMS Development Tools

    1?9==?>0=

    Int Inte&er 1wole n$mber !ata from +23,1+2,9=9>3;9>tro$& 23,+ , 12,9=9>3;9=

    Smallint Inte&er !ata from 2,?1+32=;> tro$& 2,?+ , 132=;=

    Tin(int Inte&er !ata from 0 tro$& 2??Bit Inte&er !ata wit eiter a , or 0 val$e

    !ecimal /i%e! precision an! scale n$meric !ata from +,03>E ,tro$& ,03>+ ,

    n$meric /$nctionall( e.$ivalent to de,ima$

    Mone( Monetar( !ata val$es from +2;31+?9=='?>0> tro$& 2;3+ ,1?9=='?>0=

    smallmone( Monetar( !ata val$es from F2,9=9>'3;9> tro$&2,9=9>'3;9=" wit acc$rac( to a ten+to$san!t of amonetar( $nit

    /loat /loatin& precision n$mber !ata from +,'=tro$&,'=

    Real /loatin& precision n$mber !ata from +3'903>tro$&3'903>

    !atetime Date an! time !ata from Gan$ar( ," ,=?3" tro$&December 3," 000 caracters

    Te%t ariable+len&t non+Hnico!e !ata wit a ma%im$mlen&t of 23,+ , 12,9=9>3;9= caracters

    car /i%e!+len&t Hnico!e !ata wit a ma%im$m len&t of9000 caracters' Hnico!e !ata stores eac caracteras 2 b(tes as compare! to ASCII wic is store! onl(in , b(te

    nvarcar ariable+len&t Hnico!e !ata wit a ma%im$m len&tof 9000 caracters' sysnameis a s(stem+s$pplie!$ser+!efine! !ata t(pe tat is f$nctionall( e.$ivalentto nva+,-a+ (./0) an! is $se! to reference !atabaseob#ect names'

    te%t ariable+len&t Hnico!e !ata wit a ma%im$m len&tof 230+ , 1,0=3=9,>23 caracters

    Binar( /i%e!+len&t binar( !ata wit a ma%im$m len&t of

    Prepared by: ELJUN P. CORTES + ; +

  • 8/13/2019 MELJUN CORTES Manual RDBMS Database Csci30

    7/24

    CCSI30 RDBMS Development Tools

    >000 b(tes

    varbinar( ariable+len&t binar( !ata wit a ma%im$m len&t of>000 b(tes

    Ima&e ariable+len&t binar( !ata wit a ma%im$m len&t of

    23,

    + , 12,9=9>3;9= b(tesC$rsor A reference to a c$rsor

    s.l@variant A !ata t(pe tat stores val$es of vario$s SQL Server+s$pporte! !ata t(pes" e%cept te1t" nte1t" timestam'"an! s*$2va+iant

    Table A special !ata t(pe $se! to store a res$lt set for laterprocessin&

    timestamp A !atabase+wi!e $ni.$e n$mber tat &ets $p!ate!ever( time a row &ets $p!ate!'

    $ni.$ei!entifier A &loball( $ni.$e i!entifier 1HID

    Nu$$s

    al$es tat are in!eterminate are sai! to be HLL' NU"" !ata is

    $se! partic$larl( at recor!s werein no entr( e%ists in one of its col$mns'

    Tis simpl( means no !ata is present in tat partic$lar entr(' HLL !ata

    is ver( m$c !ifferent from a te%t !ata wit 0 caracters 1J8" as HLL

    means notin& or no entr( at all wile J8 is a te%t entr( tat #$st appen to

    ave no caracters at all'

    In creatin& tables" we can specif( weter a partic$lar col$mn m$st

    accept a val$e or ma( not accept a val$e in some sit$ations' Tis is !one

    b( specif(in& a col$mn to be HLL or T HLL'

    Syntax:

    Prepared by: ELJUN P. CORTES + = +

    CR:AT: TABL: 4TBLM:51 4/LDM:5 4DATATK*:514L:T5 *RIMARK :K" 4/LDM:5 4DATATK*:514L:T5 4HLLNT HLL5O

  • 8/13/2019 MELJUN CORTES Manual RDBMS Database Csci30

    8/24

    CCSI30 RDBMS Development Tools

    Note3 A primar( )e( col$mn so$l! alwa(s ave an entr( an!

    terefore" it is alwa(s set to T HLL wile col$mns oter tan

    te primar( )e( !efa$lts to HLL'

    T-e INSERTCommand

    Syntax:

    Te IT )e(wor! is #$st tere to ma)e te statement more

    rea!able an! can basicall( be omitte!' Te col$mn@list ma( be omitte!

    onl( if te or!er of te list of !ata val$es inserte! matc te or!er of te

    col$mns of te table an! all col$mns m$st ave a !ata val$e to be

    inserte!' Te ALH:S )e(wor! is a list of te val$es to be inserte!

    matcin& te or!er an! amo$nt of col$mns liste! in te col$mn@list' It

    ma( not alwa(s be te case tat all col$mns will ave a val$e wen

    insertin& a row in a table an! terefore" oter entries ma( be consi!ere!

    as HLL !ata'

    S#NG"E TAB"E 4UER#ES

    An SQL statement tat accesses !ata from a table or #oine! tables is

    calle! a .$er(' Accessin& or retrievin& !ata from te !atabase server wo$l!

    consist of te followin& process7 1, Te client will form$late a .$er( in SQL an!

    pass it to te !atabase serverO 12 Te !atabase server wo$l! cec) te

    correctness of te .$er( an! if correct retrieves te !ata !escribe! in te .$er(O

    Prepared by: ELJUN P. CORTES + > +

    IS:RT PIT 4table5 P1col$mn@list ALH:S 1!ata@val$es

  • 8/13/2019 MELJUN CORTES Manual RDBMS Database Csci30

    9/24

    CCSI30 RDBMS Development Tools

    13 Te !atabase server ret$rns te res$lt set in table form or in rows an!

    col$mns'

    Syntax: (Simple S!C" statement#

    Te first line is te Jselect8 cla$se' Te Jselect8 cla$se specifies te

    col$mns to be incl$!e! in te res$lt set' Te secon! line is te Jfrom8 cla$se'

    Te Jfrom 8 cla$se specifies te so$rce of !ata' Te so$rce of !ata can be a

    table" #oine! tables" view or combination of views an! tables'

    Sim'$e Ret+ieva$s

    ACC:SSI ALL CLHMS AD ALL RS

    Syntax:

    /RMATTI I T: S:L:CT CLAHS:

    Te select cla$se is not onl( limite! to specif(in& col$mns to be

    retrieve!' Matematical operations" strin& concatenations" an! application

    of f$nctions can also be !one in te select cla$se'

    Te 567 s(mbol is an overloa!e! operator' I/ two operan!s are

    strin&s" te JE8 wo$l! mean concatenation' If te two operan!s are

    inte&ers or an( n$meric !ata t(pe" te JE8 wo$l! mean a!!ition'

    Te 5as7cla$se is $se! to name te res$ltin& col$mn' ito$t te

    Jas8 cla$se" te res$ltin& fiel! will ave no name'

    Prepared by: ELJUN P. CORTES + < +

    S:L:CT C," C2" C3" '' Cfrom !ata@so$rcewere *

    S:L:CT /RM 4table5

  • 8/13/2019 MELJUN CORTES Manual RDBMS Database Csci30

    10/24

    CCSI30 RDBMS Development Tools

    Matematical operations can also be carrie! o$t in te Jselect8

    cla$se' Te( are not limite! to n$meric !ata' Caracter !ata t(pes tat

    as n$meric val$es co$l! also be $se! in matematical operations' Te(

    are a$tomaticall( converte! b( te server'

    /ILT:RI R:SHLTS IT :R: CLAHS:

    Lo&ical connectives Jan!8" Jor8 an! Jnot8 can be $se! in te Jwere8

    cla$se' Comparison operators li)e 4 1less tan" 4 1less tan or e.$al" 5

    1&reater tan" 5 1&reater tan or e.$al" 1e.$al" an! 45 1not e.$al

    can also be $se!' Te res$lt of lo&ical an! comparison operators are

    eiter tr$e or false'

    T:R SQL CSTRHCTS

    ne of te most commonl( $se! operation on strin&s is pattern

    matcin& $sin& te operator Jli)e8' Te operator Jli)e8 !etermines weter

    or not a &iven caracter strin& matces a specifie! pattern' A pattern can

    incl$!e re&$lar caracters an! wil!car! caracters'

    Prepared by: ELJUN P. CORTES + ,0 +

  • 8/13/2019 MELJUN CORTES Manual RDBMS Database Csci30

    11/24

    CCSI30 RDBMS Development Tools

    ILDCARDCARACT:R

    D:SCRI*TI :UAM*L:

    V Matces an( s$bstrin&

    incl$!in& empt(s$bstrin&'

    :R: Title LI:

    JVmana&erV8 fin!s allemplo(ees avin& tewor! Jmana&er8an(were in teir title'

    @ 1$n!erscore Matces a sin&lecaracter'

    :R: firstnameLI: J@ill8 fin!s all fo$r+letter last names taten! wit ill 1Bill" Gill"an! so on'

    P Matces an( sin&lecaracter witin te

    specifie! ran&e 1Pa+for set 1Pabc!ef'

    :R: firstnameLI: JPB+Gill8 fin!s first

    names en!in& wit illan! be&innin& wit an(sin&le caracterbetween B an! G" fore%ample Bill" ill" Gillan! so on'

    PW Matces an( sin&lecaracter not witin tespecifie! ran&e 1PWa+for set 1PWabc!ef'

    :R: firstnameLI: JPWB+Gill8 fin!s firstnames en!in& wit an(sin&le caracterbetween B an! G'

    HSI I

    Te I operator !etermines if a &iven val$e matces an( val$e in

    te list' Te list can eiter be a plain list separate! b( commas or a res$lt

    of a sin&le col$mn s$b+.$er(

    Prepared by: ELJUN P. CORTES + ,, +

  • 8/13/2019 MELJUN CORTES Manual RDBMS Database Csci30

    12/24

    CCSI30 RDBMS Development Tools

    So+ting

    HS: / RD:R BK

    Te $se of RD:R BK cla$se witin a .$er( ca$ses te rows of

    te res$lt to appear in a sorte! or!er' Te RD:R BK cla$se is almost

    alwa(s fo$n! at te en! of a S:L:CT statement since sortin& will be te

    last tin& to be !one before !ispla(in& te res$lts'

    Syntax:

    Note3 r!er is ASC:DI b( !efa$lt'

    SRTI IT MHLTI*L: :KS" D:SC:DI RD:R

    In cases werein sortin& on onl( one col$mn is not eno$&

    beca$se m$ltiple entries ma( ave te same val$es" a secon!ar( or even

    tertiar( sortin& )e( is nee!e! to arran&e te res$lt of te .$er('

    M$ltiple col$mns wit a combination of ascen!in& an! !escen!in&

    sort or!er can also be $se! on a .$er( b( simpl( appen!in& te !esire!

    or!er ne%t to te col$mn name'

    Prepared by: ELJUN P. CORTES + ,2 +

    S:L:CT 4fiel!name5" 4fiel!name5"/RM 4tablename5:R: 4con!ition5RD:R BK 4fiel!name5

  • 8/13/2019 MELJUN CORTES Manual RDBMS Database Csci30

    13/24

    CCSI30 RDBMS Development Tools

    Bui$t8in !un,tions

    HS: / C$U%"

    Te CHT a&&re&ate f$nction literall( co$nts te rows in a .$er('

    Te res$lt is sown in a fiel! tat as no col$mn name'

    Syntax:

    HS: / SU&

    Te SHM a&&re&ate f$nction comp$tes for te s$m of some

    collection of n$mbers' nl( col$mns tat are n$meric in nat$re can be

    $se! as parameter for te SHM f$nction'

    Syntax:

    HS: / D'S"'%C"

    To &et ri! of !$plicate items in a .$er(" te DISTICT pre!icate is

    $se! before te name of te col$mn'

    Syntax:

    Prepared by: ELJUN P. CORTES + ,3 +

    S:L:CT C$U%"(#/RM 4tablename5:R: 4con!ittion5

    S:L:CT SU&()fieldname*+,)fieldname+,.#/RM 4tablename5:R: 4con!ition5

    S:L:CT DISTICT 4fiel!name5/RM 4tablename5:R: 4con!ition5

  • 8/13/2019 MELJUN CORTES Manual RDBMS Database Csci30

    14/24

    CCSI30 RDBMS Development Tools

    G+ou'ing

    :STI QH:RI:S

    A s$b.$er( is a normal SQL .$er( tat is neste! insi!e anoter

    .$er( 1b( $se of parenteses create! wen (o$ ave a S:L:CT

    statement tat serves as te basis for eiter part of te !ata or con!ition in

    anoter .$er('

    Syntax:

    HSI /R$UP 01

    Te RH* BK cla$se is $se! to a&&re&ate or combine

    information'

    Syntax:

    HSI 234'%/

    Te AI cla$se can onl( be $se! wen tere is a RH* BK

    cla$se' It places con!itions on ow te &ro$pin& will loo) li)e'

    Syntax:

    Prepared by: ELJUN P. CORTES + ,9 +

    S:L:CT 4fiel!name,5"4fiel!name25"/RM 4tablename5:R: 4fiel!name5 I N T I

    1S:L:CT 4fiel!name5/RM 4tablename5

    S:L:CT 4fiel!name,5" 4fiel!name25" /RM 4tablename5:R: 4con!ition5RH* BK 4fiel!name5

    S:L:CT 4fiel!name,5" 4fiel!name25" /RM 4tablename5RH* BK 4fiel!name5AI 4con!ition5

  • 8/13/2019 MELJUN CORTES Manual RDBMS Database Csci30

    15/24

    CCSI30 RDBMS Development Tools

    234'%/vs' 52R

    Te AI cla$se filters res$lts #$st li)e te :R: cla$se' Te

    !ifference lies on te point of time te filterin& will be !one' Since

    AI can onl( be $se! in con#$nction wit te RH* BK cla$se"

    $sin& bot :R: an! AI in a .$er( means te rows will be

    filtere! first b( te :R: cla$se before te res$ltin& set is &ro$pe!

    $sin& te RH* BK ten onl( after will te rows be filtere! a&ain b( te

    AI cla$se'

    HLLS

    HLL val$es are basicall( still consi!ere! in a RH* BK cla$se'

    T$s" wen rows are &ro$pe! accor!in& to some specifie! col$mns" tose

    col$mns wic contain HLL val$es are treate! #$st te same an! HLLS

    are &ro$pe! to&eter #$st li)e oter val$es tat are e.$al'

    Prepared by: ELJUN P. CORTES + ,? +

  • 8/13/2019 MELJUN CORTES Manual RDBMS Database Csci30

    16/24

    CCSI30 RDBMS Development Tools

    MU"T#"E8TAB"E 4UER#ES

    9oining T&o Tab$es Toget-e+

    In normali-in& a !atabase" tables are bro)en to prevent re!$n!anc(

    of !ata' ormali-in& a !atabase means !ecomposin& tables' B$t tat

    wo$l! not mean tat te information or !ata in te !ecompose! table is

    not nee!e! or important' Te information mi&t still be nee!e! in te !e+

    normali-e! table'

    B( #oinin& tables" !ata from two or more tables or .$eries base! on

    lo&ical relationsips amon& te tables can be retrieve!'

    Syntax:

    I:R GI

    Te simplest form of a #oin is te I:R GI' Te col$mn or

    col$mns in one table tat matces te col$mns in te secon! table m$st

    be specifie!'

    Prepared by: ELJUN P. CORTES + ,; +

    S:L:CT 4tbl,nme'fl!nme5" 4tbl2nme'fl!nme5" '/RM 4tbl,nme5 4alias5 GI X L:/T GI X

    RIT GI 4tbl2nme5 4alias5 4tbl,nme'fl!nme5 4tbl2nme'fl!nme5:R: 4con!ition5''''

  • 8/13/2019 MELJUN CORTES Manual RDBMS Database Csci30

    17/24

    CCSI30 RDBMS Development Tools

    L:/T GI

    Te secon! t(pe of #oin is te L:/T GI' L:/T GI ret$rns all

    te rows from te first table" even if te #oin con!ition is not satisfie!' If

    tere are rows in te first table tat !o not ave matces in te secon!

    table" tose rows will also be liste!' Te col$mns !erive! from te secon!

    table will ave a n$ll val$e'

    RIT GI

    Te RIT GI ret$rns all te rows from te secon! table" even if

    te #oin con!ition is not satisfie!' If tere a! been an( rows in te

    secon! table tat !i! not ave an( matc in te first table" tose rows also

    wo$l! ave been liste!' Te col$mns !erive! from te first table will ave

    a n$ll val$e'

    Sub*ue+ies Using E:#STS; #N; ANY; SME; and A""

    Syntax:

    Prepared by: ELJUN P. CORTES + ,= +

    S:L:CT 4fl!,nme5" 4fl!2nme5" /RM 4tblnme5:R: 4fl!nme5 T I X I X T :UISTS X

    :UISTS X 1s$b.$er( X list

  • 8/13/2019 MELJUN CORTES Manual RDBMS Database Csci30

    18/24

    CCSI30 RDBMS Development Tools

    I

    Te JI8 operator !etermines if a &iven val$e matces an( val$e in

    te list' Te list can eiter be a plain list separate! b( commas or a res$lt

    of a sin&le col$mn s$b.$er('

    :UISTS

    :UISTS tests weter a s$b.$er( as an( t$ples in its res$lt' It

    ret$rns tr$e if te s$b.$er( is non+empt('

    ALL" SM: an! AK

    To compare a scalar val$e wit a sin&le+col$mn set of val$es $se"

    Jall8" Jsome8" an! Jan(8' Te &eneral s(nta% for tese constr$cts is7

    scalar@e%pression Y X 45 X Z X 5 X 5 X Z5 X 4 X 4 X Z4 [

    all X some X an( 1s$b.$er(

    Hsin& tese tree constr$cts is almost te same $sin& te Jin8

    constr$ct' Te onl( !ifference is tat te e%pression is compare! to te

    list'

    Prepared by: ELJUN P. CORTES + ,> +

  • 8/13/2019 MELJUN CORTES Manual RDBMS Database Csci30

    19/24

    CCSI30 RDBMS Development Tools

    S:T *:RATIS

    Te JHI8 operator combines te res$lt of two or more .$eries

    into a sin&le res$lt set' Te onl( pre+re.$isite is te tat te table #oine!

    m$st ave te same n$mber of col$mns an! te !ata t(pes m$st be

    compatible'

    Syntax:

    UDATES

    C-anging E1isting Data

    CAI :UISTI DATA I T: DATABAS:

    Te H*DAT: statement prett( m$c $p!ates e%istin& !ata' Its

    str$ct$re is prett( m$c te same as te S:L:CT statement b$t wit an

    a!!e! S:T cla$se an! a sli&tl( !ifferent /RM cla$se'

    Syntax:

    Prepared by: ELJUN P. CORTES + ,< +

    S:L:CT 4fl!,nme5" 4fl!2nme5" /RM 4tbl,nme5HIS:L:CT 4fl!,nme5" 4fl!2nme5" /RM 4tbl2nme5

    H*DAT: 4table@name5S:T 4col$mn5 4val$e5 P" 4col$mn5 4val$e5P/RM 4so$rce@table1s5

    P:R: 4restrictive@con!ition5

  • 8/13/2019 MELJUN CORTES Manual RDBMS Database Csci30

    20/24

    CCSI30 RDBMS Development Tools

    HSI A CM*HD CDITI

    Similar in nat$re to te S:L:CT statement" te H*DAT: statement

    can be $se! to can&e !ata to specific rows b( combinin& con!itions in

    te :R: cla$se' B( $sin& te )e(wor!s AD an! R" $p!ate can be

    !one on more specific rows'

    Hp!ates can also be !one $sin& con!itions forme! from a

    s$b.$er('

    Adding Ne& Data

    To a!! new !ata to tables" we $se te IS:RT statement' To a!!

    m$ltiple !ata sim$ltaneo$sl( to a table $sin& te IS:RT statement"

    instea! of $sin& te ALH:S cla$se" a s$b.$er( is iss$e! in te IS:RT

    statement'

    Syntax:

    De$eting Data

    To !elete !ata in tables" we $se te D:L:T: statement' Te first

    tin& to !o is to )now wic !ata is to be !elete!' After ens$rin& te ri&t

    !ata" te D:L:T: statement is iss$e! base! on te .$er('

    Syntax:

    Prepared by: ELJUN P. CORTES + 20 +

    IS:RT 4table@name5 P1col$mn@list 4!erive!@table@name5

    D:L:T: 4table@name5P:R: 4con!ition5

  • 8/13/2019 MELJUN CORTES Manual RDBMS Database Csci30

    21/24

    CCSI30 RDBMS Development Tools

    C+eating a Ne& Tab$e f+om an E1isting Tab$e

    it te $se of te IT cla$se in a select statement" a new table

    can be create! tat contains te res$lt of a .$er('

    Syntax:

    Setting a Co$umn to Nu$$

    A col$mn can be set to HLL $sin& an $p!ate statement' Simpl(

    set te val$e of te col$mn to be e.$al to te HLL !ata'

    Syntax:

    C-anging Database St+u,tu+e

    ALT:R

    After tables in a !atabase ave been create!" te str$ct$re can still

    be can&es b( a!!in& or removin& col$mns or b( can&in& te !ata t(pe

    of te col$mns'

    Syntax:

    Prepared by: ELJUN P. CORTES + 2, +

    S:L:CT IT 4new@table@name5/RM 4e%istin&@table@name5:R: 4con!tion5

    H*DAT: 4table@name5 S:T 4fiel!@name5 HLL

    ALT:R TABL: 4table@name5PADD 4col$mn@name5 4!ata@t(pe5 41len&t5"PDR* 4col$mn@name5

    ALT:R CLHM 4col$mn@name54new@!ata@t(pe5 4new@len&t5

  • 8/13/2019 MELJUN CORTES Manual RDBMS Database Csci30

    22/24

    CCSI30 RDBMS Development Tools

    DI//ICHLT CA:S

    To alter te str$ct$re of a !atabase b( a!!in& a forei&n )e( col$mn

    to a table" tat is" a col$mn referencin& a primar( )e( col$mn from anoter

    table" we $se te CSTRAIT F R:/:R:C:S cla$se'

    Syntax:

    DR*

    To remove a table !efinition an! all !ata" constraints" an! all oter

    ob#ects tat !efine te table" DR* TABL: comman! is $se!'

    Syntax:

    To !elete te wole !atabase itself" DR* DATABAS:

    4!atabase@name5 is $se!'

    DATABASE ADM#N#STRAT#N

  • 8/13/2019 MELJUN CORTES Manual RDBMS Database Csci30

    23/24

    CCSI30 RDBMS Development Tools

    Syntax:

    Hsin& views offer te followin& a!vanta&es7

    Q$eries are optimi-e! since te view is compile!

    Sections of lon& an! comple% .$eries can be ma!e into

    views terefore re!$cin& co!e an! improvin& rea!abilit('

    Const+aints

    R#MARY EY

    A primar( )e( is te $ni.$e i!entifier for eac row' A table can onl(

    ave one primar( )e(' Te primar( )e( cannot ave a HLL val$e'

    Syntax:

    Prepared by: ELJUN P. CORTES + 23 +

    CR:AT: I: 4view@name5AS.$er(

    CSTRAIT 4constraint@name5*RIMARK :K 4col$mn@name5

  • 8/13/2019 MELJUN CORTES Manual RDBMS Database Csci30

    24/24

    CCSI30 RDBMS Development Tools

    !RE#GN EY

    A forei&n )e( is a col$mn or combination of col$mns tat is a

    primar( )e( at anoter table' Te forei&n )e( serves as a lin) between

    two tables'

    Syntax:

    CSTRAIT 4constraint@name5/R:I :K 1col$mn@nameR:/:R:C:S 4reference!@table@name5

    1reference!@col$mn