Escolar Documentos
Profissional Documentos
Cultura Documentos
TOAD
Exercises for Hands-on
Author(s)
Authorized by
Creation Date
Version
ER/COPR/CRS/DB46SC/019
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:E&R@infy.com
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
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
4 of 85
Step 2:
Getting connected to Oracle through TOAD supplying your Userid ,Password and
Hoststring
ER/COPR/CRS/DB46SC/019
5 of 85
Step 3:
After getting connected to Oracle through TOAD the following screen would appear.
ER/COPR/CRS/DB46SC/019
6 of 85
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
7 of 85
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
Managers 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
8 of 85
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
9 of 85
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
10 of 85
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
11 of 85
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
12 of 85
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
13 of 85
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
14 of 85
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
15 of 85
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
16 of 85
Step 8:
To check for the ER diagram of the relational schema, click on the Database >> Report >> ER
Diagram.
ER/COPR/CRS/DB46SC/019
17 of 85
Step 9:
Click on the new ER Diagram(the first icon which is shown tabbed).
ER/COPR/CRS/DB46SC/019
18 of 85
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
19 of 85
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
20 of 85
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
21 of 85
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
22 of 85
Step 2:
Supply the name of the sequence, the start value and the increment value.
ER/COPR/CRS/DB46SC/019
23 of 85
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
24 of 85
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
25 of 85
Step 1: Select View option from Database >> Create menu. You will see the following
window.
ER/COPR/CRS/DB46SC/019
26 of 85
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
27 of 85
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
28 of 85
ER/COPR/CRS/DB46SC/019
29 of 85
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
30 of 85
ER/COPR/CRS/DB46SC/019
31 of 85
ER/COPR/CRS/DB46SC/019
32 of 85
Step 3: Also click on the Fire when, Fire on and For each options as given below
ER/COPR/CRS/DB46SC/019
33 of 85
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
34 of 85
ER/COPR/CRS/DB46SC/019
35 of 85
Step 6: Finally you can see the trigger created from Schema browser.
ER/COPR/CRS/DB46SC/019
36 of 85
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
37 of 85
ER/COPR/CRS/DB46SC/019
38 of 85
ER/COPR/CRS/DB46SC/019
39 of 85
Step 3:
To see the existing Stored Procedures expand the tab of Procedure.
ER/COPR/CRS/DB46SC/019
40 of 85
Step 4:
Right click on the Procedure Tab and go for View/Edit package save option
ER/COPR/CRS/DB46SC/019
41 of 85
Step 5:
Go for the first procedure tab file path
ER/COPR/CRS/DB46SC/019
42 of 85
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
43 of 85
******************************************************************************/
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
44 of 85
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
45 of 85
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
46 of 85
ER/COPR/CRS/DB46SC/019
47 of 85
ER/COPR/CRS/DB46SC/019
48 of 85
Step 13:
Fill in the following details and choose the new proc template as directed below.
ER/COPR/CRS/DB46SC/019
49 of 85
Step 14:
Save it.
After this the following screen would appear.
ER/COPR/CRS/DB46SC/019
50 of 85
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
51 of 85
Step 17:
After execution the following output will appear in the output window.
ER/COPR/CRS/DB46SC/019
52 of 85
ER/COPR/CRS/DB46SC/019
53 of 85
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
54 of 85
Step 3:
As shown in the figure below, load the procedure in the Procedure editor.
ER/COPR/CRS/DB46SC/019
55 of 85
Once it is loaded, you can see a new window for the procedure editor.
ER/COPR/CRS/DB46SC/019
56 of 85
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
57 of 85
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
58 of 85
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
59 of 85
Once the Breakpoint is set, it will be highlighted with a red line, as shown below.
ER/COPR/CRS/DB46SC/019
60 of 85
Step 7:
We can add watches too. Go to Debug menu and select Watches option.
ER/COPR/CRS/DB46SC/019
61 of 85
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
62 of 85
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
63 of 85
ER/COPR/CRS/DB46SC/019
64 of 85
ER/COPR/CRS/DB46SC/019
65 of 85
ER/COPR/CRS/DB46SC/019
66 of 85
ER/COPR/CRS/DB46SC/019
67 of 85
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
68 of 85
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
69 of 85
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
70 of 85
The Tracing is started and it will show the tracing line with highlighted color.
ER/COPR/CRS/DB46SC/019
71 of 85
Every time press shift+F7 or select the Trace Into option from Debug menu.
ER/COPR/CRS/DB46SC/019
72 of 85
While tracing the procedure execution, we can watch the variables added for watch option in
the watch window.
ER/COPR/CRS/DB46SC/019
73 of 85
ER/COPR/CRS/DB46SC/019
74 of 85
ER/COPR/CRS/DB46SC/019
75 of 85
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
76 of 85
Step 2:
Click on the Turn Output On button as shown in the below snap shot.
ER/COPR/CRS/DB46SC/019
77 of 85
ER/COPR/CRS/DB46SC/019
78 of 85
Step 3:
Go to Debug menu and select the Run option.
ER/COPR/CRS/DB46SC/019
79 of 85
Once you Run the script, the DBMS Output window will show the output.
ER/COPR/CRS/DB46SC/019
80 of 85
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
81 of 85
ER/COPR/CRS/DB46SC/019
82 of 85
Step2 :
Select CodeXpert window and click on
ER/COPR/CRS/DB46SC/019
83 of 85
Now we can see the suggestions given by CodeXpert in the bottom window.
ER/COPR/CRS/DB46SC/019
84 of 85
ER/COPR/CRS/DB46SC/019
85 of 85