Cornea Madalina - Tema2

Embed Size (px)

Citation preview

  • 8/12/2019 Cornea Madalina - Tema2

    1/14

    Section 2 Lesson 1: Using Variables in PL/SQL

    Vocabulary

    Identify the vocabulary word for each definition below:Use variables-Used for storage of data and manipulation of stored values.

    Parameters- values passed to a program by a user or by another program to customize the

    program.

    Try It / Solve It

    1. Fill in the blanks.

    A. ariables can be assigned to the output of aPL/SQL subprogram

    !. ariables can be assigned values in the declarative section of a "#$% block

    '. ariables can be passed asparameters to PL/SQLto subprograms.

    (. Identify valid and invalid variable declaration and initialization

    number)of)copies "#%)I*+,-,/

    printer)name '0*%+A*+ A'A(2134/

    deliver)to A'A(2134:56ohnson/

    by)when 7A+,:5 %8%7A+,91/

    . ,;amine the following anonymous block and choose the appropriate statement.

    7,'#A,

    fname A'A(2(34/

    lname A'A(21%)0U+"U+."U+)#I*,2 F*A>, ??= = ??lname4/

    ,*7/

    A. +he block will e;ecute successfully and print @fernandez.

    !. +he block will give an error because the fname variable is used without initializing.

    '. +he block will e;ecute successfully and print @null fernandez .

    7. +he block will give an error because you cannot use the 7,FAU#+ keyword to initialize a variable

    of the A'A( type.

    ,. +he block will give an error because the F*A>, variable is not declared.

  • 8/12/2019 Cornea Madalina - Tema2

    2/14

    B. In Application ,;press:

    A. 'reate the following function:

    ',A+, FU*'+I0* num)characters 2p)string I* A'A(4,+U* I*+,-, A%

    v)num)characters I*+,-,/

    !,-I*

    %,#,'+ #,*-+2p)string4 I*+0 v)num)characters

    F0> dual/

    ,+U* v)num)characters/

    ,*7/

    !. 'reate and e;ecute the following anonymous block:

    7,'#A,

    v)length)of)string I*+,-,/

    !,-I*

    v)length)of)string :5 num)characters2=0racle 'orporation=4/

    7!>%)0U+"U+."U+)#I*,2v)length)of)string4/

    ,*7/

  • 8/12/2019 Cornea Madalina - Tema2

    3/14

    Section 2 Lesson 2: Recogniing PL/SQL Le!ical Units

    VocabularyIdentify the vocabulary word for each definition below:

    LiteralE An e;plicit numericD character stringD dateD or !oolean value that is not represented by

    an identifier

    "elimitersE %ymbols that have special meaning to an 0racle database.

    Reserve# $or#sE Cords that have special meaning to an 0racle database and cannot be used

    as identifiers.

    %ommentsE 7escribe the purpose and use of each code segment and are ignored by "#$%.

    Le!ical unitsE !uilding blocks of any "#$% block and are seuences of characters including

    lettersD digitsD tabsD returnsD and symbols.

    I#enti&iers E A nameD up to 3 characters in lengthD given to a "#$% obGect.

    Try It / Solve It Questions

    1. Fill in the blanks.

    A. An identifieris the name given to a "#$% obGect.

    !. A reserved word is a word that has special meaning to the 0racle database.

    '. A delimiters is a symbol that has special meaning to the 0racle database.

    7. A literal is an e;plicit numericD character stringD dateD or !oolean value that is not

    represented by an identifier.

    ,. A comments e;plains what a piece of code is trying to achieve.

    (. Identify each of the following identifiers as valid or invalid. If invalidD specify why.

    Identifier alid 2H4 Invalid2H4 Chy Invalid

    +oday ;

    #ast name ; 'ontine un spatiu

    +odays)date H

    number)of)days)in)february)this) year ; >ai mult de 3 de caractere

    IsleapJyear H

    Knumber H

    *U>!,K H

    *umber1toL H

  • 8/12/2019 Cornea Madalina - Tema2

    4/14

    . Identify the reserved words in the following list.

    Cord eserved

    8$*

    create 7a

    make *u

    table 7a

    seat *u

    alter 7a

    rename 7a

    row 7a

    number 7a

    web *u

    B. Chat kind of le;ical unit 2for e;ample eserved wordD 7elimiterD #iteralD 'omment4 is each of the

    following

    alue #e;ical Unit

    %,#,'+ eserved word

    :5 'ompound 7elimiter

    =+,%+= 'ommentFA#%, !oolean #iteral

    EE new process 'omment

    F0> eserved word

    $Mselect the country with the highest elevation M$ 'omment

    )test

    B.3N *umeric comment

  • 8/12/2019 Cornea Madalina - Tema2

    5/14

    Section 2 Lesson ': Recogniing "ata Ty(es

    Vocabulary

    Identify the vocabulary word for each definition below:

    Large ob)ectE old valuesD called locatorsD that specify the location of large obGects 2such as

    graphic images4 that are stored out of line.

    Scalar E old a single value with no internal components.

    %om(ositeE 'ontain internal elements that are either scalar 2record4 or composite 2record and

    table4

    Re&erenceE old valuesD called pointersD that point to a storage location.

    *b)ect E A schema obGect with a nameD attributesD and methods.

    Try It / Solve It

    1. In your own wordsD describe what a data type is and e;plain why it is important.

    Rezolvare :specificO ce tip i ce dimensiune de date poate fi utilizatD precum i modul Pn care

    acesta este stocat

  • 8/12/2019 Cornea Madalina - Tema2

    6/14

    Section 2 Lesson 4: Using Scalar Data Types

    Vocabulary

    Identify the vocabulary word for each definition below:

    +ooleanEA datatype that stores one of the three possible values used for logicalcalculations: TRUE, FALE, or !ULL"

    ,TP. -Attribute used to declare a variable according to another previouslydeclared variable or database colu#n"

    Try It / Solve It

    1. $eclarations:A" %hich of the following variable declarations are valid&

    Declaration Valid or

    Invalid

    anu#ber'of'students (L'I!TE)ER* Valid

    b TU$E!T'!A+E AR-.AR/012345ohnson*Invalid

    c stu'per'class -6!TA!T !U+7ER*Invalid

    d to#orrow $ATE :4 8$ATE91* Valid

    !. For those declarations in 1"A" that are invalid, describe why they are invalid"

    Rezolvare :c" constanta nu are nicio valoare atribuita

    '. %rite an anony#ous bloc in which you declare and print each of the variables in 1"A,correcting the invalid declarations"

    Rezolvare:

    7,'#A,number)of)students "#%)I*+,-,/%+U7,*+)*A>, A'A(2134:56ohnson/

    stu)per)class '0*%+A*+ *U>!,2D(4 :5Q.(%)0U+"U+."U+)#I*,2= number)of)students 5 =?? number)of)students4/7!>%)0U+"U+."U+)#I*,2= %+U7,*+)*A>, 5 =?? %+U7,*+)*A>,4/

    7!>%)0U+"U+."U+)#I*,2= stu)per)class 5 =?? stu)per)class4/7!>%)0U+"U+."U+)#I*,2= tomorrow 5 =?? tomorrow4/

    ,*7/

    (. Evaluate the variables in the following code" Answer the following ;uestions about eachvariable" Is it na#ed well& %hy or why not& If it is not na#ed well, what would be a betterna#e and why&

    $E-LARE

    country'na#e AR-.AR/ 0

  • 8/12/2019 Cornea Madalina - Tema2

    7/14

    7E)I!

    ELE-T country'na#e, #edian'age I!T6 country'na#e, #edian'ageFR6+ wf'countries

    %.ERE country'na#e 4 >United tates of A#erica>*$7+'6UT(UT"(UT'LI!E0> The #edian age in >??country'na#e??> is

    >??#edian'age??>">3*E!$*

    Rezolvare:variabilele country'na#e si #edian'age sunt folosite ca identificatori, desidesfinesc si nu#ele a doua ca#puri din tabela wf'countries

    . E@a#ine the declarations in ;uestion /" -hange the declarations so that they use theT8(E attribute"

    Rezolvare:

    $E-LARE

    v)country)name wf)countries.country)nameR+8",/

    v)median)age wf)countries.median)ageR+8",:

    B. In your own words, describe why using the T8(E attribute is better than hardBcoding data types" -an you e@plain how you could run into proble#s in the future byhardBcoding the data types of the country'na#e and #edian'age variables in;uestion /&

    Rezolvare:Este folosit pentru a oferi Cn #od auto#at o variabilD cu acelea i datetipu i di#ensiuneca defini iea coloanei de bae de date,o altD variabilD declaratD

    .ello %orld>3*E!$*

    A" Add a declarative section to this (LGL bloc" In the declarative section, declarethe following variables:

    S A variable na#ed T6$A8 of datatype $ATE" Initialie T6$A8 with 8$ATE"

    S A variable na#ed T6+6RR6% with the sa#e datatype as T6$A8" Use theT8(E attribute to declare this variable"

    Rezolvare:

    $E-LARET6$A8 $ATE:48$ATE*T6+6RR6% T6$A8T8(E*

    ! " In the e@ecutable section, initialie the T6+6RR6% variable with an e@pressionthat calculates to#orrowHs date 0add 1 to the value in T6$A83" (rint the value of T6$A8and T6+6RR6% after printing .ello %orldH"

    Rezolvare:

    7E)I!T6+6RR6% :4 T6$A8 91*$7+'6UT(UT"(UT'LI!E0>.ello %orld>??> >??T6$A8??> >??T6+6RR6%3*

  • 8/12/2019 Cornea Madalina - Tema2

    8/14

    E!$*

    Hello World 03/Mar/2014 04/Mar/2014

    Statement processed.

    Section 2 Lesson 5: Writing L/S!L "#ecutable State$ents

  • 8/12/2019 Cornea Madalina - Tema2

    9/14

    Vocabulary

    Identify the vocabulary word for each definition below:.!(licit %onversions- 'onverts values from one data type to another by using builtEin

    functions.Im(licit conversionsE 'onverts data types dynamically if they are mi;ed in a

    statement.

    Try it/solve it

    1" E@a#ine the following code and then answer the ;uestions"

    $E-LARE

    @ AR-.AR/0/23*7E)I!

    @:4 >1/J> 9 >K *

    $7+'6UT(UT"(UT'LI!E0@3*E!$*

    3 A" %hat do you thin the output will be when you run the above code&Rezolvare:

  • 8/12/2019 Cornea Madalina - Tema2

    10/14

    +onth dd, yyyyH" tore the date in a $ATE variable called #y'date" -reate anothervariable of the $ATE type called v'last'day" Assign the last day of this #onth tov'last'day" $isplay the value of v'last'day"

    Rezolvare:

    $E-LARE#y'date $ATE:48$ATE*v'last'day $ATE :4 LAT'$A80#y'date3*

    7E)I!$7+'6UT(UT"(UT'LI!E0T6'-.AR0#y'date,>+onth dd,8888>33*$7+'6UT(UT"(UT'LI!E0'LAT'$A83*

    E!$*

    March 03, 2014

    31/Mar/2014

    Statement processed.

    B. +odify the progra# created in ;uestion J to add K< days to todayHs date and thencalculate and display the nu#ber of #onths between the two dates"

    Rezolvare:

    $E-LARE#y'date $ATE:48$ATE*v'last'day $ATE :4 #y'date9K

  • 8/12/2019 Cornea Madalina - Tema2

    11/14

    $E-LARE

    v'nu#ber !U+7ER*v'boolean 766LEA!*

    7E)I!v'nu#ber :4 /

  • 8/12/2019 Cornea Madalina - Tema2

    12/14

    Identify the vocabulary word for each definition below.QULI3I.R-A label given to a block.Variable Sco(eE'onsists of all the blocks in which the variable is either local 2the declaring

    block4 or global 2nested blocks within the declaring block4 .Variable VisibilityE+he portion of the program where the variable can be accessed without

    using a ualifier.

    Try It / Solve It

    1. ,valuate the "#$% block below and determine the value of each of the followingvariables according to the rules of scoping.

    7,'#A,weight *U>!,24 :5 Q33/

    message A'A(2( employeesC,, employee)id 5 133/

  • 8/12/2019 Cornea Madalina - Tema2

    13/14

    7,'#A,

    v)employee)id employees.employee)idR+8",/v)Gob employees.Gob)idR+8",/

    !,-I*

    %,#,'+ employee)idD Gob)id I*+0 v)employee)idD v)GobF0> employees

    C,, employee)id 5 13/7!>%)0U+"U+."U+)#I*,2v)employee)id?? = is a =??v)Gob4/

    ,*7/

    7!>%)0U+"U+."U+)#I*,2v)employee)id?? = is a =??v)Gob4/,*7/

    A. Chy does the inner block display the Gob)id of employee 13D not employee 1333 Rezolvare: pentru ca se afiseaza in blocul unde se e;trage valare 13

    !. Chy does the outer block display the Gob)id of employee 133D not employee 13Rezolvare:pentru ca valoarea variabilei este cea din blocul e;tern

    '. >odify the code to display the details of employee 133 in the inner block. Use block labels.TTouter

    Section 2 Lesson %: &ood rogra$$ing ractices

    Try It / Solve It

    1"Enter and run the following (LGL bloc" It will e@ecute correctly if you have entered it

  • 8/12/2019 Cornea Madalina - Tema2

    14/14

    correctly, but it contains so#e e@a#ples of bad progra##ing practice"

    $E-LARE

    #yvar1 AR-.AR/0/23* #yvar/nu#ber0K3*7E)I!ELE-T country'na#e I!T6 #yvar1

    FR6+ wf'countries %.ERE country'id 4 1/K=*#yvar/ :4>1/JK>*

    +8AR/ :4 #yvar/ N /*$7+'6UT(UT"(UT'LI!E0#yvar13* End*B

    < A" +odify the bloc to use good progra##ing practice, and reBrun the bloc"

    Q 7" 8our #odified bloc should contain e@a#ples of the following good progra##ingpractices: e@plicit data type conversions, #eaningful and consistent variable na#es, use ofT8(E, upper and lowercase conventions, single and #ultiBline co##ents, and clearindentation"

    $E-LARE

    #yvar1 AR-.AR/0/23*#yvar/ !U+7ER0K3*

    7E)I!ELE-T country'na#e I!T6 #yvar1

    FR6+ wf'countries %.ERE country'id 4 1/K=*

    #yvar/ :4 >1/JK>*+8AR/ :4 #yvar/ N /*$7+'6UT(UT"(UT'LI!E0#yvar13*

    E!$*