January 25, 2017 | Author: samramon95 | Category: N/A
Infosys Technologies Limited
TOAD Exercises for Hands-on
© Infosys Technologies Ltd No. 350, Hebbal Electronics City, Hootagalli Mysore – 571186
Author(s) Authorized by Creation Date Version
ER/COPR/CRS/DB46SC/019
N.Vijayakalyani, Rituza Khare Dr. M.P. Ravindra March 2007 1.0
VER. NO : 2.0
COPYRIGHT NOTICE All ideas and information contained in this document are the intellectual property of Education and Research Department, Infosys Technologies Limited. This document is not for general distribution and is meant for use only for the person they are specifically issued to. This document shall not be loaned to anyone, within or outside Infosys, including its customers. Copying or unauthorized distribution of this document, in any form or means including electronic, mechanical, photocopying or otherwise is illegal. Education and Research Department Infosys Technologies Limited Electronic City Hosur Road Bangalore - 561 229, India. Tel: 91 80 852 0261-270 Fax: 91 80 852 0362 www.infy.com mailto:
[email protected]
Document Revision History Version 1.0
Date 21-May-2007
Author(s) N.Vijayakalyani, Rituza Khare
Reviewer(s) Ajit Ravindran Nair
2.0
12-Jul-2007
Nihar Bhatt, Pragati Sheel
N.Vijayakalyani
Comments Initial Draft
ORACLE – Exercises for Hands on
Background This document contains the assignments to be completed as part of the hands on for the subject ORACLE (Course code: ). Note: All assignments in this document must be completed in the sequence in this document in order to complete the course.
Assignment 1 Objective: To learn how to connect to TOAD Background: Although on your machine TOAD is pre configured but please follow these steps to get a feel of how actually this has been configured. Step 1: Reach TOAD via : Start->All programs->Quest Software->TOAD for Oracle->Toad for Oracle 9.0
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
4 of 85
ORACLE – Exercises for Hands on
Step 2: Getting connected to Oracle through TOAD supplying your Userid ,Password and Hoststring
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
5 of 85
ORACLE – Exercises for Hands on
Step 3: After getting connected to Oracle through TOAD the following screen would appear.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
6 of 85
ORACLE – Exercises for Hands on
Summary: You have learnt how to get connected to Oracle using TOAD Note: ENRTOAD must be showing your Oracle ID.(eg: TJan07LC1Comp55)
Assignment 2 Objective: To learn how to create tables, apply constraints and generate ER Diagram using TOAD
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
7 of 85
ORACLE – Exercises for Hands on Create the following tables EMP Column name Data type
Description
EMPNO ENAME SAL MGR DEPTNO PROJNO
Number(5) Varchar2(15) Number(8,2) Number(5) Number(3) Number(3)
Employee number Employee name Basic Salary Manager‟s Emp. number Department Number Project Number
DEPT Column name
Data type
Description
DEPTNO DNAME LOC
Number(3) Varchar2(15) Varchar2(15)
Department number Department name Location of department
PROJECT Column name
Data type
Description
PROJNO PNAME TEAMSIZE
Number(3) Varchar2(15) Number(3)
Project number Project name Size of the project team
Step 1: Goto Database >>Create >>Table You will get the following screen.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
8 of 85
ORACLE – Exercises for Hands on
Step 2: After adding all the columns to the EMP table the following screen would appear. It shows all the 6 columns of the EMP table with their sizes as well.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
9 of 85
ORACLE – Exercises for Hands on
Step 3: To add constraints to the already existing columns click on the “Constraints” tab of the same form. Click on “Add Constraint” tab of the form. The following screen will appear.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
10 of 85
ORACLE – Exercises for Hands on
Step 4: To add the Primary key constraint select the column on which you want to set the property of primary key. The selected table column will appear in the Constraint columns. Note: You should have selected only the required columns for the desired constraint. Constraint name will be generated by the tool. The primary key option would have been set to “Yes”.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
11 of 85
ORACLE – Exercises for Hands on
Step 3: To add check constraint to the Sal column click on the “Constraints” tab in the window and choose “Check constraint”. It will open a “Constraint text” window to write the logic of check constraint. The check constraint counter will start reflecting the count 1.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
12 of 85
ORACLE – Exercises for Hands on
Step 4: To check all the applied constraints on the columns go to the “database” menu and choose “Schema Browser” or the same icon will be available in the tool bar.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
13 of 85
ORACLE – Exercises for Hands on
Step 5: The following screen would appear. The left frame shows the created tables list. As EMP is the only table that is created so far, so only it will appear in the list.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
14 of 85
ORACLE – Exercises for Hands on
Step 6: After the creation of all the three tables the following screen would be seen with the left frame having in list of all the 3 tables. Notice the “Table” tab in the left frame is selected.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
15 of 85
ORACLE – Exercises for Hands on
Step 7: After the creation of a table the constraints on its columns can be checked by clicking on the “Constraints” tab on the left frame. Then select on any constraint name, the right frame will reflect its details.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
16 of 85
ORACLE – Exercises for Hands on
Step 8: To check for the ER diagram of the relational schema, click on the Database >> Report >> ER Diagram.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
17 of 85
ORACLE – Exercises for Hands on
Step 9: Click on the new ER Diagram(the first icon which is shown tabbed).
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
18 of 85
ORACLE – Exercises for Hands on
Step 10: Select your UserID in the schema and EMP/EMPLOYEE table name in the table list. Let all the check boxes be ticked. Click on ok.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
19 of 85
ORACLE – Exercises for Hands on
Step 11: The following ER Diagram must appear. It shows the relationship of EMPLOYEE table with all the other tables in the database via foreign keys.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
20 of 85
ORACLE – Exercises for Hands on
Step 12: Save the ERD of the schema by clicking on the save icon at the top of the frame. It will ask for the name ,save it as bitmap file with the extension “.bmp”.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
21 of 85
ORACLE – Exercises for Hands on
Assignment 3 Objective: To learn how to create sequence, view and trigger using TOAD Step 1: Go to Database>>Create>>Sequence option. ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
22 of 85
ORACLE – Exercises for Hands on The following window will appear.
Step 2: Supply the name of the sequence, the start value and the increment value.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
23 of 85
ORACLE – Exercises for Hands on
Step 3: To check for the existing Sequences got to Databse>>Schema Browser or you can click on its icon in the tool bar.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
24 of 85
ORACLE – Exercises for Hands on
Step 4: Click on the “Sequence” tab in the left frame. On the right frame it will reflect all the details of the selected sequence.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
25 of 85
ORACLE – Exercises for Hands on
Step 1: Select View option from Database >> Create menu. You will see the following window.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
26 of 85
ORACLE – Exercises for Hands on
Step 2: When it is prompting for View Name, type the view name and type the following query in the text window provided. SELECT deptno, COUNT (empno) as NoofEmp FROM emp GROUP BY deptno
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
27 of 85
ORACLE – Exercises for Hands on
Step 3: Click on Validate Query, and then you will see Query parsed OK, if the query runs without any errors.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
28 of 85
ORACLE – Exercises for Hands on
Step 4: Then click on OK button.
Objective: To create a trigger using TOAD. Step 1: Select the Trigger option from Database >> Create menu, as shown in the snap shot below.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
29 of 85
ORACLE – Exercises for Hands on
Step 2: Provide the Trigger name, select the Schema name and Table name on which the trigger is to be created.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
30 of 85
ORACLE – Exercises for Hands on
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
31 of 85
ORACLE – Exercises for Hands on
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
32 of 85
ORACLE – Exercises for Hands on
Step 3: Also click on the Fire when, Fire on and For each options as given below
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
33 of 85
ORACLE – Exercises for Hands on
Step 4: Select the When Clause window and provide the condition for the when clause as given below. NEW.job = „SALESMAN‟
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
34 of 85
ORACLE – Exercises for Hands on
Step 5: Provide the trigger body as given below. BEGIN IF INSERTING THEN :NEW.comm := 0; ELSIF :OLD.comm IS NULL THEN :NEW.comm := 0; ELSE :NEW.comm := :OLD.comm + 0.12; END IF; And click on OK button.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
35 of 85
ORACLE – Exercises for Hands on
Step 6: Finally you can see the trigger created from Schema browser.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
36 of 85
ORACLE – Exercises for Hands on
Assignment 4 Objective: To learn how to create a Store Procedure, SP template and how to call a SP from an anonymous block using TOAD.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
37 of 85
ORACLE – Exercises for Hands on Step 1: Go to Database>>Schema Browser option. The following window will appear. Select the Treeview from the Browser Style.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
38 of 85
ORACLE – Exercises for Hands on The following window will appear.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
39 of 85
ORACLE – Exercises for Hands on Step 2: Expand My Schema tab
Step 3: To see the existing Stored Procedures expand the tab of Procedure.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
40 of 85
ORACLE – Exercises for Hands on
Step 4: Right click on the Procedure Tab and go for View/Edit package save option
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
41 of 85
ORACLE – Exercises for Hands on
Step 5: Go for the first procedure tab file path
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
42 of 85
ORACLE – Exercises for Hands on
Step 6: Click on edit file and the file opens With the following code CREATE OR REPLACE PROCEDURE %YourObjectname% IS tmpVar NUMBER; /****************************************************************************** NAME: %YourObjectName% PURPOSE: REVISIONS: Ver Date Author Description --------- ---------- --------------- -----------------------------------1.0 %SYSDATE% %USERNAME% 1. Created this procedure. NOTES: Automatically available Auto Replace Keywords: Object Name: %YourObjectName% Sysdate: %SYSDATE% Date and Time: %DATE%, %TIME%, and %DATETIME% ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
43 of 85
ORACLE – Exercises for Hands on Username: Table Name:
%USERNAME% (set in TOAD Options, Procedure Editor) %TableName% (set in the "New PL/SQL Object" dialog)
******************************************************************************/ BEGIN tmpVar := 0; EXCEPTION WHEN NO_DATA_FOUND THEN NULL; WHEN OTHERS THEN -- Consider logging the error and then re-raise RAISE; END %YourObjectName%; Step 6: Let the file remain open Then click on add to add a new SP template
Step 8: Fill in the following details
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
44 of 85
ORACLE – Exercises for Hands on Path will be the same as old Procedure template path and name of file will be the name of the new SP template.
Step 9: Try editing the new file. The following screen appears. Click on “yes” to save the file, as it does not exist it will create a new file with this name and save it.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
45 of 85
ORACLE – Exercises for Hands on
Step 10: Copy the contents of the old Procedure template in this new file with the following changes highlighted in blue color CREATE OR REPLACE PROCEDURE %YourObjectname% ( pNum1 In number, pNum2 IN Number, pRes Out Number) IS tmpVar NUMBER; /****************************************************************************** NAME: %YourObjectName% PURPOSE: REVISIONS: Ver Date Author Description --------- ---------- --------------- -----------------------------------1.0 %SYSDATE% %USERNAME% 1. Created this procedure. NOTES:
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
46 of 85
ORACLE – Exercises for Hands on Automatically available Auto Replace Keywords: Object Name: %YourObjectName% Sysdate: %SYSDATE% Date and Time: %DATE%, %TIME%, and %DATETIME% Username: %USERNAME% (set in TOAD Options, Procedure Editor) Table Name: %TableName% (set in the "New PL/SQL Object" dialog) ******************************************************************************/ BEGIN tmpVar := 0; EXCEPTION WHEN NO_DATA_FOUND THEN NULL; WHEN OTHERS THEN -- Consider logging the error and then re-raise RAISE; END %YourObjectName%; Step 11: Apply the changes and say ok. Now this will be the new Procedure template in addition to the old procedure template. Step 12: Now Come back to the schema browser and right click on the procedure tab to create a new SP.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
47 of 85
ORACLE – Exercises for Hands on
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
48 of 85
ORACLE – Exercises for Hands on
Step 13: Fill in the following details and choose the new proc template as directed below.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
49 of 85
ORACLE – Exercises for Hands on
Step 14: Save it. After this the following screen would appear.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
50 of 85
ORACLE – Exercises for Hands on
Complete the procedure body by adding the following stmt in it. pRes:= pNum1 + pNum2;
Now execute the procedure by pressing F11 Step 15: After the Procedure is created, create an anonymous block for calling it. declare Result number; begin TryProc(&a,&b,Result); dbms_output.put_line('The result is'||Result); end;
Step 16: Now execute this SP. Set serveroutput on. It will ask for 2 inputs at the run time Supply the following data
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
51 of 85
ORACLE – Exercises for Hands on
Step 17: After execution the following output will appear in the output window.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
52 of 85
ORACLE – Exercises for Hands on
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
53 of 85
ORACLE – Exercises for Hands on
Assignment 5 Objective: To learn how to use Debug options of TOAD Step 1: In TOAD Select Database Schema Browser Select Procedures from Schema Browser
Step 2: Select the Procedure to be debugged.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
54 of 85
ORACLE – Exercises for Hands on
Step 3: As shown in the figure below, load the procedure in the Procedure editor.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
55 of 85
ORACLE – Exercises for Hands on
Once it is loaded, you can see a new window for the procedure editor.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
56 of 85
ORACLE – Exercises for Hands on
Step 4: Before using any debug options, we need to set the values for input parameters of the procedure selected. In Debug menu select Set Parameters options.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
57 of 85
ORACLE – Exercises for Hands on
Step 5 : Now you can see a new window opened and the parameters of the procedure are listed. One by one we need to set the values for all IN and INOUT parameters. The following pictures illustrate this method for two input parameters (pNum1 and pNum2).
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
58 of 85
ORACLE – Exercises for Hands on
Step 6: Once the parameters are set with the values, the procedure is ready for debug operation. Here we will see how to set a break point. Go to Debug menu in Procedure editor, and select Set Breakpoint option after placing the cursor in the appropriate line. Debug Set Breakpoint.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
59 of 85
ORACLE – Exercises for Hands on
Once the Breakpoint is set, it will be highlighted with a red line, as shown below.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
60 of 85
ORACLE – Exercises for Hands on
Step 7: We can add watches too. Go to Debug menu and select Watches option.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
61 of 85
ORACLE – Exercises for Hands on
The Watch window will be opened as shown below. Here, select the add watch option available in the watch window. (See the below snap shot)
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
62 of 85
ORACLE – Exercises for Hands on
Once you click on Add watch option, a window will be popped and it will prompt for expression to be watched. Type an expression, or variable to be watched. The following snap shots illustrate, to add watches for variables pNum1 and pNum2.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
63 of 85
ORACLE – Exercises for Hands on
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
64 of 85
ORACLE – Exercises for Hands on
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
65 of 85
ORACLE – Exercises for Hands on
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
66 of 85
ORACLE – Exercises for Hands on
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
67 of 85
ORACLE – Exercises for Hands on
Please find below, the snap shot to display the added watch variables in the watch window. (It shows process not accessible because we have not yet started the debug/ trace operation)
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
68 of 85
ORACLE – Exercises for Hands on
Step 8: Now we can start tracing the execution of the procedure. Go to Debug menu and select Trace Into option. Debug Trace Into.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
69 of 85
ORACLE – Exercises for Hands on
It will prompt with the following message “Do you want to compile referenced objects with Debug information? This process can take several seconds”. Click on Yes.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
70 of 85
ORACLE – Exercises for Hands on
The Tracing is started and it will show the tracing line with highlighted color.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
71 of 85
ORACLE – Exercises for Hands on
Every time press shift+F7 or select the Trace Into option from Debug menu.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
72 of 85
ORACLE – Exercises for Hands on
While tracing the procedure execution, we can watch the variables added for watch option in the watch window.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
73 of 85
ORACLE – Exercises for Hands on
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
74 of 85
ORACLE – Exercises for Hands on
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
75 of 85
ORACLE – Exercises for Hands on
Assignment 6 Objective: To learn how to use the DBMS output window of SQL Editor. The following demo is to show the use of DBMS Output window available in SQL Editor of TOAD. Step 1: Type the following code in SQL Editor, as given in the snap shot. Click on DBMS Output window, and click on the clear window button.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
76 of 85
ORACLE – Exercises for Hands on
Step 2: Click on the Turn Output On button as shown in the below snap shot.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
77 of 85
ORACLE – Exercises for Hands on
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
78 of 85
ORACLE – Exercises for Hands on
Step 3: Go to Debug menu and select the Run option.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
79 of 85
ORACLE – Exercises for Hands on
Once you Run the script, the DBMS Output window will show the output.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
80 of 85
ORACLE – Exercises for Hands on
Assignment 7 Objective: To learn how to use the CodeXpert option of SQL Editor. Step 1: Type the following query in SQL Editor. Select emp.empno, emp.deptno, dept.dname from emp, dept;
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
81 of 85
ORACLE – Exercises for Hands on
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
82 of 85
ORACLE – Exercises for Hands on
Step2 : Select CodeXpert window and click on
ER/COPR/CRS/DB46SC/019
button available in CodeXpert window.
Education & Research Department © Infosys Technologies Limited
83 of 85
ORACLE – Exercises for Hands on
Now we can see the suggestions given by CodeXpert in the bottom window.
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
84 of 85
ORACLE – Exercises for Hands on
ER/COPR/CRS/DB46SC/019
Education & Research Department © Infosys Technologies Limited
85 of 85