Oracle_out.doc

download Oracle_out.doc

of 14

Transcript of Oracle_out.doc

  • 8/10/2019 Oracle_out.doc

    1/14

    Business DataBase Systems Name: __________________Professor Chen

    ENHANCED GUIDE TO ORACLE

    Chapter Creatin! an" #o"ifyin! Data$ase Ta$%es

    A& Names an" PropertiesI. Naming Conventions (Table and Field names)

    1. from 1 to 30 characters. onl! al"han#meric characters$ and s"ecial characters (%$ _ $&)3. m#st begin 'ith a letter and can not contain blan s"aces or h!"hense.g.$ re the follo'ing names valid* +h!*c#stomer orderc#stomer,order&order

    II. -ata T!"es/2 commands are N4T case sensitive (b#t /#er! 'ithin the single 5#otation are case sensitive).

    There are three t!"es of datat!"e in 4racle: b#ilit,in$ librar!$ or #ser,defined. 6#ilt,in are standard$ #ser,defined are s"eciali7ed t!"es b#ilt b! #sers$ and librar! t!"es are t!"es b#ilt b! a third "art! and s#""liedto the #ser. The follo'ings are most commonl! #sed b#ilt,in data t!"es:

    1. 89C9a) variable length character #" to ;< =$000 charactersb) m#st s"ecif! ma>im#m 'idth allo'edc) trailing s"aces are N4T added ( tr#ncated)(e.g.) s_name 89C9(0)?

    an instance: @A4N CBN. C9

    a) fi>ed length (;

  • 8/10/2019 Oracle_out.doc

    2/14

    a.) C9: CII code re"resents each character #" to E,digit (1 b!te)$ binar! val#e and canre"resent a total of G different characters.b.) NC9: imilar to C9 data t!"e b#t it s#""orts #" to 1G,digit ( b!tes) binar!character codes for other co#ntr!s characters.

    =. ND6B9 ("recision$ scale)

    10 ,130;< ;es$ vie's$ etc.) in the database. -D commands'or 'ith datain the database.

    B& INTEGRIT+ an" ,ALUE CONSTRAINTS

    I. Naming convention for constraints

    ;tablenameO_;fieldnameO_;constraint idO

    Chen: Bnhanced #ide to 4racle page-2

  • 8/10/2019 Oracle_out.doc

    3/14

    +here ;constraint idO is:1. p- 29ID9L PBL. f- 9BFB9BNCB ;tablenameO (")3 )) CBCP ;condition to be checedO (note that cc stands for CHECK CONDITION)=. nn N4T N

    . u- NI/B

    1 and stated above are integrityconstraints 'hereas 3$=$ are valueconstraints.

    For e>am"les: Inte!rity Constraints1. f_id ND6B9() C4NT9INT fac#lt!_f_id_" 29ID9L PBL loc_id ND6B9() C4NT9INT fa#lt!_loc_id_f 9BFB9BNCB location (loc_id)

    FCTL f

    F_I- Q 4C_I- Q

    4CTI4N

    4C_I-

    ,a%ue Constraints

    3. a) credits ND6B9() C4NT9INT co#rse_credits_cc

    CBCP ((creditsO) N- (credits ;

  • 8/10/2019 Oracle_out.doc

    4/14

    (f_id ND6B9() CONSTRAINTfac#lt!_f_id_" PRI#AR+ .E+$f_last 89C9(30) CONSTRAINTfac#lt!_f_last_nn NOT NULL$f_first 89C9(30) CONSTRAINT fac#lt!_f_first_nn NOT NULL$f_mi C9(1)$loc_id ND6B9() C4NT9INT fac#lt!_loc_id_f RE(ERENCESlocation(loc_id)$f_"hone 89C9(10)$

    f_ran C9(=) C4NT9INT fac#lt!_f_ran_ccCHEC.((f_ran < R4R) 49 (f_ran < RFR)49 (f_ran < RTR) 49 (f_ran < RINTR))$f_"in ND6B9(=))?

    C& STARTING S*L/P%us

    I. dding Fields to B>isting Tables

    TB9 T6B ;tablenameO-- ;fieldnameO ;data declarationOC4NT9INT ;integrit! constraintsO

    C4NT9INT ;val#e constraintsO?

    Note thata) ;QO is something that the #ser t!"e inb) ;data declarationO ma! be: ;data t!"e (si7e)O or ;data t!"e (si7e) -BFT ;defa#lt val#eOOe.g.$1. TB9 T6B fac#lt! -- (startdate -TB)?. integrity constraintTB9 T6B enrollment

    -- C4NT9INT enrollment_csecid_f F49BIN PBL (csecid)

    9BFB9BNCB co#rse_section(csecid)?3. value constraintTB9 T6B st#dent

    -- C4NT9INT st#dent_s_class_ccCBCP ((s_class < RF9R) 49 (s_class < R4R)49 (s_class < RA9R) 49 (s_class < R9R)49 (s_class < R9R))?

    II. Dodif!ing B>iting Fields

    TB9 T6B ;tablenameOD4-IFL ;fieldnameO ;ne' data declarationO

    C4NT9INT ;integrit! constraintsOC4NT9INT ;val#e constraintsO?

    D4-IFL cla#se can be #sed onl! to changea. data t!"eb. data si7e$c. defa#lt val#es$ andd. the N4T N chec conditions

    Chen: Bnhanced #ide to 4racle page-4

  • 8/10/2019 Oracle_out.doc

    5/14

    other chec condition modifications$ !o# m#sta. -942 the e>isting col#mn constraint$ and thenb. se the -- cla#se to define the ne' constraint.

    III. -ro""ing B>isting Constraints

    TB9 T6B ;tablenameO-942 C4NT9INT ;integrit! constraintsO?

    orTB9 T6B ;tablenameO-942 C4NT9INT ;val#e constraintsO?

    #mmar! of #sing the TB9 T6B commands:

    Use ALTER TABLE0ADD 1hen2 Use ALTER TABLE0#ODI(+ 1hen2

    dding a ne' col#mn Dodif!ing a col#mns data t!"e or si7e

    dding a ne' foreign e! or chec conditionconstraint

    Dodif!ing a col#mn to add a N4T Nconstraint

    Dodif!ing a col#mn to add a defa#lt val#e

    I8. D4T F9B/BNTL B- / Commands

    C9BTB T6B ;tablenameO(;fieldname1O ;data declarationO

    C4NT9INT ;integrit! constraint declarationOC4NT9INT ;fieldname1 8B C4NT9INT declarationO

    ;fieldnameO ;data declarationOC4NT9INT ;integrit! constraint declarationO

    C4NT9INT ;fieldname 8B C4NT9INT declarationOQQ )?

    BBCT T6B_NDBF94D B9_T6B?

    BBCT C4NT9INT_NDBF94D B9_C4NT9INT?

    BBCT C4NT9INT_NDBF94D B9_C4NT9INT+B9B T6B_NDB < @;tablenameO?

    -BC9I6B ;tablenameO?

    ,, mae s#re follo' the order of deleting tables,, see em"t!n#.s5l or north'oo.s5l

    -942 T6B ;tablenameO?9BNDB ;oldtablenameO T4 ;ne'tablenameO?

    Chen: Bnhanced #ide to 4racle page-5

    CB BNITITL

  • 8/10/2019 Oracle_out.doc

    6/14

    Chen: Bnhanced #ide to 4racle page-

    (i!ure2 Ta$%es 1ith Referentia% Inte!rity Constraints

    Therefore$ the order of entering data (or creating tables) is:

    4CTI4N ,,,,O FCTL ,,,,O T-BNT

    STUDENT

    p- f-

    s'i" s'%ast 3 s'pin f'i"

    100 Diller EE1 1Q Q

    10F Connol! 1EE 3

    (ACULT+p- f-

    f'i" f'%ast 3 %o)'i" 3 f'ima!e

    1 Co> F3 co>.M"g

    Q Q Q

    F 6ro'n FK bro'n.M"g

    LOCATION

    p-

    %o)'i" $%"!')o"e room )apa)ity=F CR 454 465

    3 3

    FK I6 1

    TABLE74 TABLE7

    ((ACULT+) 8STUDENT)

    RULES2

    1. Lo# canCt a""a record to TABLE7

    (or the table 'ith f-9e.g.$

    T3-BNT) #nless there is a corres"onding record in TABLE74 (orthe table 'ith p-).

    . Lo# cant "e%etea record in TABLE74 (or the table 'ith p-$ e.g.$

    FC30TL) if there is a record in TABLE7

    (or the table 'ith f-).

    4rder of enterin!data into the database: 04CTI4NFC30TL

    T3-BNT

    4rder of "e%etin!data from the database:T3-BNTFC30TL

    04CTI4N

  • 8/10/2019 Oracle_out.doc

    7/14

  • 8/10/2019 Oracle_out.doc

    8/14

    II& Up"atin! E=istin! Re)or"s in a Ta$%e

    2-TB ;tablenameO BT ;col#mnO < ;ne' data val#eO +B9B ;search conditionO?

    e.g.$ 2-TB st#dent BT s_class < @9 +B9B sid < 10?

    -BBTB F94D ;tablenameO +B9B ;search conditionO? e.g.$

    -BBTB F94D st#dent? +B9B sid < 10?

    o' man! records 'ill be deleted from the ne>t command*

    -BBTB F94D ;tablenameO?

    B& Usin! the SELECT Comman" to Retrie>;

    >& SELECT CO##ANDS

    ,, 9etrieving -ata from a ingle TableBBCT ;col#mn 1$ col#mn $ QOF94D ;o'nername.tablenameO+B9B ;search conditionO?

    BBCT ;col#mn 1$ col#mn $ QO

    F94D ;o'nername.table1name$ o'nername.tablename$ QO+B9B ;search conditionO?

    BBCT ;col#mn 1$ col#mn $ QOF94D ;o'nername.table1name$ o'nername.tablename$ QO+B9B ;search conditionOANDQORQ?

    BBCT ;col#mn 1$ col#mn $ QOF94D ;o'nername.table1name$ o'nername.tablename$ QO

    +B9B NOT;search conditionO?

    BBCT F94D ;tablenameO

    BBCT -ITINCT ;fieldnameOF94D ;tablenameO?

    Chen: Bnhanced #ide to 4racle page-$

    Case issensitive'ithin the search condition

  • 8/10/2019 Oracle_out.doc

    9/14

    ,, orting the /#er! 4#t"#t,, scending is the defa#ltBBCT ;col#mn 1$ col#mn $ QOF94D ;o'nername.table1name$ o'nername.tablename$ QO+B9B ;search conditionOORDER B+;fieldnameO ;-BCO?

    BBCT ;col#mn 1$ col#mn $ QOF94D ;o'nername.table1name$ o'nername.tablename$ QO+B9B ;search conditionO49-B9 6L ;fieldname1O ;fieldnameO?

    BBCT L-TBF94D -?

    Note that - is a U#tilit!V table "rovided b! 4racle. It has e>actl! one col#mn$ called-DDL$ and one ro'. - is #sed to do things s#ch as:

    W rithmeticW 4btain s!stem information s#ch as the dateW 9et#rn literal val#es.

    Lo# can do this 'ith other tables$ b#t the res#lts 'o#ld be ret#rned once for each ro' in thetable.

    & Usin! Ca%)u%ations in a *uery

    1. N#mber F#nctions

    6(n) e.g.$ BBCT 6(,1) F94D -? 9es#lt: 1

    24+B9(n$ "o'er) e.g.$ BBCT 24+B9(3$) F94D -? 9es#lt: 94N-(n$ "recision) e.g.$ BBCT 94N-(13.=KG$ 1) F94D -? 9es#lt: 13.T9NC(n$ "recision) e.g.$ BBCT T9NC(13.=KG$ 1) F94D -? 9es#lt: 13.=e.g.$BBCT sid$ slname$ T9NC((L-TB J sdob)X3G.) ageF94D st#dent$ -

    . sing -ate rithmetic"ecif! the shi"ments are in the "eriod from EX11X001 thro#gh EX1KX001.

    BBCT shi"id$ date_e>"ected$ 5#antit!_e>"ected

    F94D shi""ing+B9B date_e>"ected O T4_-TB(@0EX10X001$ @ DDX--XLLLL)N- date_e>"ected ;< T4_-TB(@0EX10X001$ @ DDX--XLLLL) H K?

    --_D4NT(;no'n dateO$ ;co#ntO)

    e.g.$ --_D4NT(T4_-TB(@Q$ @;masO)$ n)? n (co#nt) ma! be negative

    D4NT_6BT+BBN(;date1O$ ;dateO)

    Chen: Bnhanced #ide to 4racle page-%

    Format mas

  • 8/10/2019 Oracle_out.doc

    10/14

    3. sing ro#" F#nctions

    8(fieldname)C4NT(fieldname)DY(fieldname)

    DIN(fieldname)D(fieldname)

    e.g.$BBCT bldg_code$ room$ D(ca"acit!)$8(ca"acit!)$ DY(ca"acit!)F94D location+B9B ca"acit! Oenrl$ c#rrenrl$ (ma>enrl J c#rrenrl) o"enF94D co#rse_section

    C. sing /#eries to (INNB9) Aoin D#lti"le Tables

    BBCT ;col#mn,name1$ col#mn,name$ QOF94D ;tablename1O$ ;tablenameO$ Q+B9B ;tablename1.col#mn,name < talbename.col#mn,nameO?

    Note that the col#mn,name in the &HE'Econdition sho#ld be the same (either " or f). If a

    col#mn,name is on m#lti"le tables$ it sho#ld be follo'ed b! a tablename (e.g.$ fac#lt!.loc_id)

    BBCT ;col#mn,name1$ col#mn,name$ QOF94D tablename1$ tablename$ tablename3$ tablename=$ Q+B9B tablename1.col#mn,name < talbename.col#mn,nameN- tablename3.col#mn,name < talbename=.col#mn,nameQN-Q ?

    Usin! Set Operators to Com$ine *uery Resu%ts

    NI4N: ret#rns all ro's from both 5#eries$ b#t 4NL dis"la!s d#"licate ro's onceNI4N : ret#rns all 8"up%i)ate;ro's from both 5#eries$ and dis"la!s d#"licate ro'sINTB9BCT: ret#rns all ro's that are ret#rned b! both 5#eriesDIN: ret#rns all ro's ret#rned b! the first 5#er! min#s the matching ro's ret#rned b! thesecond 5#er!

    Chen: Bnhanced #ide to 4racle page-()

  • 8/10/2019 Oracle_out.doc

    11/14

    The! all re5#ire that both 5#eries have the same n#mber of dis"la! col#mns in theBBCT statement$ and that each col#mn in the first 5#re! has the same data t!"e as thecorres"onding col#mn in the second 5#er!.

    Data$ase ,ie1s

    database vie' is a logical (virt#al) table based on a 5#er!. It does not store data$ b#t "resents it ina format different from the one in 'hich it is stored in the #nderl!ing tables.

    C9BTB 8IB+ ;vie'_nameO ;vie' 5#er! s"ecification.?

    e.g.$

    C9BTB 8IB+ fa)u%ty'

  • 8/10/2019 Oracle_out.doc

    12/14

    RE,O.E;"rivilege1$ "rivilege$ QO4N ;table_nameOF94D ;#ser1$ #ser$ QO?

    Se?uen)es

    4racle se5#ences are se5#ential lists of n#mbers that are generated a#tomaticall! b! the database.e5#ences are #sef#l for creating #ni5#e s#rrogate e!s val#es for "rimar! e! fields 'hen no fielde>ists to #se as the "rimar! e!.

    C9BTB B/BNCB ;se5#ence_nameOT9T +IT ;nODY8B ;nO

    N4DY8B N4CCB

    N449-B9?

    INB9T INT4 ;table nameO

    8B ;se5_nameO.NBYT8$ @fie%"4 "ata

  • 8/10/2019 Oracle_out.doc

    13/14

    /#er! 6#ilder also generates / commands (code generator). ee te>t boo for more details.

    Optimiin! *uery Pro)essin!

    Inde>es ma! be #sed to im"rove the efficienc! of data searches to meet "artic#lar search criteria after thetable has been in #se for some time. Therefore$ the abilit! to create inde>es 5#icl! and efficientl! at an!

    time is im"ortant. / inde>es can be created on the basis of an! selected attrib#tes. For e>am"le$C9BTB IN-BY 5oh_inde>

    4N inventor! (5oh)?

    Note that this statement defines an inde> called *o+,inde for the 5oh col#mn in the inventor!table. This inde> ens#res that in the ne>t e>am"le / onl! needs to loo at ro' in the databasethat satisf! the +B9B condition$ and is$ therefore$ 5#icer to "rod#ce an ans'er.

    BBCT F94D inventor!+B9B 5oh O that "revents !o# from #sing a val#e that has been #sed before. #cha feat#re is es"eciall! #sef#l 'hen the inde> field (attrib#te) is a "rimar! e! 'hose val#es m#st notbe d#"licated:

    C9BTB NI/B IN-BY ;inde>_fieldO 4N ;tablenameO (the e! field)?

    Therefore$

    the inde>es are defined so as to o"timi7e the "rocessing of BBCT statements. ?

    an inde> is never e>"licitl! referenced in a BBCT statement? the s!nta> of / does not

    allo' this?

    d#ring the "rocessing of a statement$ / itself determines 'hether an e>isting inde> 'ill be

    #sed?

    an inde> ma! be created or deleted at an! time?

    'hen #"dating$ inserting or deleting ro's$ / also maintains the inde>es on the tables

    concerned. This means that$ on the one hand$ the "rocessing time for BBCT statements isred#ced$ 'hile$ on the other hand$ the "rocessing time for #"date statements (s#ch as INB9T$2-TB and -BBTB) can increase.

    BBCT inde>_name from #ser_inde>es?

    Chen: Bnhanced #ide to 4racle page-(3

  • 8/10/2019 Oracle_out.doc

    14/14

    Ora)%es (un)tion .eys

    (un)tions .eyscce"t6loc Den#CancelCancel a /#er!Clear 6locClear FieldXItemClear FormClear 9ecordCo#nt Datching 9ecords-elete 6ac'ard-elete 9ecord-is"la! Brror-o'n-o'n-#"licate FieldXItem

    -#"licate 9ecordBditBnter /#er!B>ec#te /#er!B>itel"eftist of 8al#es (48s)Ne' 9ecordNe>t 6locNe>t FieldXItemNe>t FieldXItem

    Ne>t 2rimar!Ne>t 9ecordNe>t et of 9ecords2revio#s 6loc2revio#s FieldXItem2revio#s FieldXItem2revio#s 9ecord2rint9et#rn9ight9#n a Formcroll -o'n

    croll "ho' Pe!s""

    F10FBscCtrlH/hiftHFCtrlH#hiftHFKhiftHF=hfitHF6acs"acehiftHFGhiftHF1CtrlHl-o'nF3

    F=CtrlHeFKFECtrlH5F1eftFFGCtrlH2age -o'nTabhiftHTab

    hiftHF3hiftH-o'nCtrlHOCtrlH2age "hiftHCtrlHTabhiftHTabhiftH"hiftHFEBnter9ightCtrlHr2age -o'n

    2age "CtrlHF1"CtrlH"

    Chen: Bnhanced #ide to 4racle page-(4