Pl lab solution

  • View
    8.225

  • Download
    4

Embed Size (px)

DESCRIPTION

 

Text of Pl lab solution

  • 1. ______________APractice Solutions______________

2. Oracle Database 11g: PL/SQL Fundamentals A - 2General Notes: Save all your lab files at the following location: D:labsPLSFlabs. Enter your SQL statements in a SQL Worksheet. To save a script in SQL Developer, fromthe File menu, select Save As or right-click in the SQL Worksheet and select Save file tosave your SQL statement as a lab__.sql script. To modify an existingscript, use File > Open to open the script file, make changes and then make sure you useSave As to save it with a different filename. To run the query, click the Execute Statement icon (or press F9) in the SQL Worksheet. ForDML and DDL statements, use the Run Script icon (or press F5). After you have executed a saved script, make sure that you do not enter your next queryin the same worksheet. Open a new worksheet. 3. Oracle Database 11g: PL/SQL Fundamentals A - 3Practice IThis is the first of many practices in this course. The solutions (if you require them) can be foundin Appendix A. Practices are intended to cover most of the topics that are presented in thecorresponding lesson.1. Start up SQL Developer using the user ID and password that are provided to you by theinstructor such as oraxx where xx is the number assigned to your PC.Click the SQL Developer icon on your desktop.2. Create a database connection using the following information:a. Connection Name: MyDBConnection.b. Username: oraxx where xx is the number assigned to your PC by the instructor.c. Password: oraxx where xx is the number assigned to your PC by the instructor.d. Hostname: Enter the host name for your PC.e. Port: 1521f. SID: ORCLRight-click the Connections icon on the Connections tabbed page, and then selectthe New Database Connection option from the shortcut menu. The New/SelectDatabase Connection window is displayed. Use the preceding information providedto create the new database connection.Note: To display the properties of the newly created connection, right-click theconnection name, and then select Properties from the shortcut menu. Substitute theusername, password, host name, and service name with the appropriate informationas provided by your instructor. The following is a sample of the newly createddatabase connection for student ora41: 4. Oracle Database 11g: PL/SQL Fundamentals A - 43. Test the new connection. If the Status is Success, connect to the database using this newconnection.a. Double-click the MyDBConnection icon on the Connections tabbed page.b. Click the Test button in the New/Select Database Connection window. If the status isSuccess, click the Connect button.4. Browse the structure of the EMPLOYEES table and display its data. 5. Oracle Database 11g: PL/SQL Fundamentals A - 5a. Expand the MyDBConnection connection by clicking the plus sign next to it.b. Expand the Tables icon by clicking the plus sign next to it.c. Display the structure of the EMPLOYEES table.Double-click the EMPLOYEES table. The Columns tab displays the columns in theEMPLOYEES table as follows:5. Browse the EMPLOYEES table and display its data.To display the employees data, click the Data tab. The EMPLOYEES table data isdisplayed as follows: 6. Oracle Database 11g: PL/SQL Fundamentals A - 66. Use the SQL Worksheet to select the last names and salaries of all employees whose annualsalary is greater than $10,000. Use both the Execute Statement (F9) and the Run Script icon(F5) icons to execute the SELECT statement. Review the results of both methods ofexecuting the SELECT statements in the appropriate tabs.Note: Take a few minutes to familiarize yourself with the data, or consult Appendix B,which provides the description and data for all tables in the HR schema that you will use inthis course.Display the SQL Worksheet using any of the following two methods:a. Select Tools > SQL Worksheet or click the Open SQL Worksheet icon. TheSelect Connection window is displayed. Select the new MyDBConnection fromthe Connection drop-down list (if not already selected), and then click OK.b. Click the Open SQL Worksheet icon on the toolbar. 7. Oracle Database 11g: PL/SQL Fundamentals A - 77. In the SQL Developer menu, navigate to Tools > Preferences. The Preferences window isdisplayed. 8. Oracle Database 11g: PL/SQL Fundamentals A - 88. Click the Worksheet Parameters option under the Database option. In the Select default pathto look for scripts text box, specify the D:labsPLSF folder. This folder contains thesolutions scripts, code examples scripts, and any labs or demos used in this course. Click OKwhen done to accept the new setting. 9. Oracle Database 11g: PL/SQL Fundamentals A - 99. Familiarize yourself with the labs folder on the D: drive:a. Right-click the SQL Worksheet area, and then select Open File from the shortcut menu.The Open window is displayed.b. Ensure that the path that you set in a previous step is the default path that is displayed inthe Open window.c. How many subfolders do you see in the labs folder?d. Navigate through the folders, and open a script file without executing the code.e. Clear the displayed code in the SQL Worksheet area.No formal solutions. 10. Oracle Database 11g: PL/SQL Fundamentals A - 10Practice 1: Introduction to PL/SQLThe labs folder is the working directory where you save your scripts. Ask your instructor forhelp in locating the labs folder for this course. The solutions for all practices are in the solnfolder.1. Which of the following PL/SQL blocks execute successfully?a. BEGINEND;b. DECLAREamount INTEGER(10);END;c. DECLAREBEGINEND;d. DECLAREamount INTEGER(10);BEGINDBMS_OUTPUT.PUT_LINE(amount);END;The block in a does not execute because the executable section does not have any statements.The block in b does not have the mandatory executable section that begins with the BEGINkeyword.The block in c has all the necessary parts but the executable section does not have anystatements.2. Create and execute a simple anonymous block that outputs Hello World. Execute and savethis script as lab_01_02_soln.sql.a. Start SQL Developer. The instructor will provide the necessary information.b. Enable output in SQL Developer by clicking the Enable DBMS Output button on theDBMS Output tab.Enable DBMSOutputDBMS OutputTab 11. Oracle Database 11g: PL/SQL Fundamentals A - 11c. Enter the following code in the workspace.BEGINDBMS_OUTPUT.PUT_LINE( Hello World );END;d. Click the Run Script button.e. You should see the following output in the Script Output tab:f. Click the Save button. Select the folder in which you want to save the file. Enterlab_01_02_soln.sql as the file name and click the Save button. 12. Oracle Database 11g: PL/SQL Fundamentals A - 12Practice 2: Declaring PL/SQL Variables1. Identify valid and invalid identifiers:a. today Validb. last_name Validc. todays_date Invalid character not allowedd. Number_of_days_in_February_this_year Invalid Too longe. Isleap$year Validf. #number Invalid Cannot start with #g. NUMBER# Validh. number1to7 Valid2. Identify valid and invalid variable declaration and initialization:a. number_of_copies PLS_INTEGER; Validb. PRINTER_NAME constant VARCHAR2(10); Invalidc. deliver_to VARCHAR2(10):=Johnson; Invalidd. by_when DATE:= CURRENT_DATE+1; ValidThe declaration in b is invalid because constant variables must be initialized duringdeclaration.The declaration in c is invalid because string literals should be enclosed within singlequotation marks.3. Examine the following anonymous block and choose the appropriate statement.DECLAREv_fname VARCHAR2(20);v_lname VARCHAR2(15) DEFAULT fernandez;BEGINDBMS_OUTPUT.PUT_LINE(v_fname ||||v_lname);END;a. The block executes successfully and prints fernandez.b. The block produces an error because the fname variable is used without initializing.c. The block executes successfully and prints null fernandez.d. The block produces an error because you cannot use the DEFAULT keyword toinitialize a variable of type VARCHAR2.e. The block produces an error because the v_fname variable is not declared.a. The block will execute successfully and print fernandez4. Create an anonymous block. In SQL Plus, load the lab_01_02_soln.sql script, whichyou created in exercise 2 of practice 1 by following these instructions:Select File > Open.Browse to select the lab_01_02_soln.sql file. Click the Open button. Yourworkspace will now have the code in the .sql file. 13. Oracle Database 11g: PL/SQL Fundamentals A - 13a. Add declarative section to this PL/SQL block. In the declarative section, declare thefollowing variables:1. Variable today of type DATE. Initialize today with SYSDATE.DECLAREv_today DATE:=SYSDATE;2. Variable tomorrow of type today. Use the %TYPE attribute to declare thisvariable.v_tomorrow v_today%TYPE;b. In the executable section, initialize the tomorrow variable with an expression,which calculates tomorrows date (add one to the value in today). Print the value oftoday and tomorrow after printing Hello World.BEGINv_tomorrow:=v_today +1;DBMS_OUTPUT.PUT_LINE( Hello World );DBMS_OUTPUT.PUT_LINE(TODAY IS : || v_today);DBMS_OUTPUT.PUT_LINE(TOMORROW IS :|| v_tomorrow);END;c. Execute and save your script as lab_02_04_soln.sql. Follow the instructions instep 2 f) of practice 1 to save the file. Sample output is as follows (the values of todayand tomorrow will be different to reflect your current todays and tomorrows date):anonymous block completedHello WorldTODAY IS : 09-MAY-07TOMORROW IS : 10-MAY-07 14. Oracle Database 11g: PL/SQL Fundamentals A - 145. Edit the lab_02_04_soln.sql script.a. Add code to create two bind variables.Create bind variables basic_percent and pf_percent of type NUMBER.VARIABLE b_basic_percent NUMBERVARIABLE b_pf_percent NUMBERb. In the executable section of the PL/SQL block, assign the values 45 and 12 tobasic_percent and pf_percent, respectively.:b_basic_percent:=45;:b_pf_percent:=12;c. Terminate the PL/SQL block with / and display the value of the bind variables byusing the PRINT command