You are on page 1of 74

Managing Your Database Using Oracle SQL Developer Page 1 of 74

Managing Your Database Using Oracle SQL Developer

Purpose

This tutorial describes how to use Oracle SQL Developer to manage your database objects.

NOTE: This tutorial is based on Oracle SQL Developer 1.2, available from June 2007. You can use
versions 1.0 or 1.1 to complete this tutorial, as the tasks discussed below remain the same for these
versions. Some of the screenshots will differ.

Time to Complete

Approximately 30 minutes

Topics

This tutorial covers the following topics:

Overview
Prerequisites
Creating a Database Connection
Adding a New Table Using the Create Table Dialog Box
Changing a Table Definition
Adding Table Constraints
Adding Data to a Table
Accessing Data
Creating Reports
Debugging and Executing PL/SQL
Summary

Viewing Screenshots

Place the cursor over this icon to load and view all the screenshots for this tutorial.
(Caution: This action loads all screenshots simultaneously, so response time may be slow
depending on your Internet connection.)

Note: Alternatively, you can place the cursor over an individual icon in the following steps to load and
view only the screenshot associated with that step. You can hide an individual screenshot by clicking it.

Overview

Oracle SQL Developer is a free graphical tool that enhances productivity and simplifies database
development tasks. Using SQL Developer, users can browse database objects, run SQL statements, edit
and debug PL/SQL statements and run reports, whether provided or created.

Developed in Java, SQL Developer runs on Windows, Linux and the Mac OS X. This is a great
advantage to the increasing numbers of developers using alternative platforms. Multiple platform
support also means that users can install SQL Developer on the Database Server and connect remotely

http://www.oracle.com/technology/obe/sqldev/sqldev.htm 8/7/2010

Managing Your Database Using Oracle SQL Developer Page 2 of 74

from their desktops, thus avoiding client server network traffic.

Default connectivity to the database is through the JDBC Thin driver, so no Oracle Home is required. To
install SQL Developer simply unzip the downloaded file. With SQL Developer users can connect to
Oracle Databases 9.2.0.1 and later, and all Oracle database editions including Express Edition.

Back to Topic List

Prerequisites

Before you perform this tutorial, you should:

1. Have access to an Oracle Database 10g instance or perform the Installing Oracle Database 10g
on Windows tutorial. Note: you can also perform this tutorial connecting to a non-windows
database.
2. Download Oracle SQL Developer from OTN and unzip it into any directory on your Windows
machine.
3. Download and unzip the file sqldev.zip into your working directory (e.g. c:\wkdir) This zip has 2
scripts that you use in this exercise.
4. Make sure the DEPENDENTS table doesn't exist. Open SQL*Plus and execute the following
commands:

connect hr/hr
drop table dependents;

Back to Topic List

Creating a Database Connection

The first step to managing database objects using Oracle SQL Developer is to create a database
connection. Perform the following steps:

1. Open Windows Explorer and double-click <your_path>\sqldeveloper\sqldeveloper.exe.

Note: you can also execute sqldeveloper in a non-windows environment.

2. In the Connections tab, right-click Connections and select New Database Connection.

http://www.oracle.com/technology/obe/sqldev/sqldev.htm 8/7/2010

Managing Your Database Using Oracle SQL Developer Page 3 of 74

3. Enter <your_schema_db_sid_name> for the Connection Name (or any other name that
identifies your connection), hr for the Username and Password, specify your <hostname> for the
Hostname and enter <your_db_sid> for the SID. Then click Test.

http://www.oracle.com/technology/obe/sqldev/sqldev.htm 8/7/2010

Click Save to save the connection. and then click Connect. The connection was not saved however.htm 8/7/2010 .com/technology/obe/sqldev/sqldev. http://www.Managing Your Database Using Oracle SQL Developer Page 4 of 74 4. The status of the connection was tested successfully.oracle.

Managing Your Database Using Oracle SQL Developer Page 5 of 74 5.htm 8/7/2010 .oracle. http://www. The connection was saved and you see the database in the list.com/technology/obe/sqldev/sqldev. Expand HR_ORCL.

com/technology/obe/sqldev/sqldev. 6. http://www.oracle. The SQL Worksheet allows you to execute SQL against the connection you just created. Expand Tables.Managing Your Database Using Oracle SQL Developer Page 6 of 74 Note: When a connection is opened. a SQL Worksheet is opened automatically.htm 8/7/2010 .

com/technology/obe/sqldev/sqldev.Managing Your Database Using Oracle SQL Developer Page 7 of 74 7.htm 8/7/2010 .oracle. http://www. Select the Table EMPLOYEES to view the table definition.

oracle. http://www. To see the data. click the Data tab.htm 8/7/2010 .com/technology/obe/sqldev/sqldev.Managing Your Database Using Oracle SQL Developer Page 8 of 74 8.

com/technology/obe/sqldev/sqldev. The data is shown. you create a new table and populate the table with data. http://www. In the next topic.Managing Your Database Using Oracle SQL Developer Page 9 of 74 9.htm 8/7/2010 .oracle.

Right-click Tables and select New TABLE..oracle.htm 8/7/2010 .com/technology/obe/sqldev/sqldev.. Perform the following steps: 1.Managing Your Database Using Oracle SQL Developer Page 10 of 74 Back to Topic List Adding a New Table Using the Create Table Dialog Box You create a new table called DEPENDENTS which has a foreign key with the EMPLOYEES table. http://www.

http://www.oracle.htm 8/7/2010 . Enter DEPENDENTS for the Table Name and click the Advanced check box.Managing Your Database Using Oracle SQL Developer Page 11 of 74 2.com/technology/obe/sqldev/sqldev.

Select the Cannot be NULL check box. Then click the Add Column icon. select NUMBER for the Data type and enter 6 for the Precision. Enter ID for the Name.htm 8/7/2010 .com/technology/obe/sqldev/sqldev.Managing Your Database Using Oracle SQL Developer Page 12 of 74 3. http://www.oracle.

enter 25 for the Size.com/technology/obe/sqldev/sqldev. Then click the Add Column icon. Then click the Add Column icon.oracle. http://www. Enter LAST_NAME for the Name. Select the Cannot be NULL check box. Enter FIRST_NAME for the Name. 5.Managing Your Database Using Oracle SQL Developer Page 13 of 74 4. and enter 20 for the Size.htm 8/7/2010 .

oracle.htm 8/7/2010 .Managing Your Database Using Oracle SQL Developer Page 14 of 74 6. Enter BIRTHDATE for the Name. Then click the Add Column icon. select DATE for the Data type.com/technology/obe/sqldev/sqldev. http://www.

Enter RELATION for the Name. and enter 25 for the Size.htm 8/7/2010 .oracle. Then click the Add Column icon. http://www.com/technology/obe/sqldev/sqldev.Managing Your Database Using Oracle SQL Developer Page 15 of 74 7.

and enter 1 for the Size. Enter GENDER for the Name. http://www.com/technology/obe/sqldev/sqldev. Then click the Add Column icon.oracle.Managing Your Database Using Oracle SQL Developer Page 16 of 74 8.htm 8/7/2010 .

Enter RELATIVE_ID for the Name.Managing Your Database Using Oracle SQL Developer Page 17 of 74 9. and enter 6 for the Precision. http://www.htm 8/7/2010 .oracle. Select the Cannot be NULL check box. Then click OK to create the table. select NUMBER for the Data type.com/technology/obe/sqldev/sqldev.

oracle. http://www.Managing Your Database Using Oracle SQL Developer Page 18 of 74 10. Select DEPENDENTS from the list. Your new table appears in the list of tables.htm 8/7/2010 .com/technology/obe/sqldev/sqldev.

Managing Your Database Using Oracle SQL Developer Page 19 of 74 Back to Topic List Changing a Table Definition Oracle SQL Developer makes it very easy to make changes to database objects. you delete a column in the DEPENDENTS table you just created. In this topic..htm 8/7/2010 . Click the Actions.oracle. Perform the following steps: 1..com/technology/obe/sqldev/sqldev. http://www. button.

Managing Your Database Using Oracle SQL Developer Page 20 of 74 2.oracle. Select Column then Drop. http://www.htm 8/7/2010 .com/technology/obe/sqldev/sqldev.

htm 8/7/2010 . http://www. Click OK. The column has been dropped. 4.Managing Your Database Using Oracle SQL Developer Page 21 of 74 3.oracle.com/technology/obe/sqldev/sqldev. Select the Column Name GENDER and click Apply.

Managing Your Database Using Oracle SQL Developer Page 22 of 74 5. 6. Expand HR_ORCL > Tables. http://www.htm 8/7/2010 .oracle. and select the DEPENDENTS table.com/technology/obe/sqldev/sqldev. Right-click the tables node for the HR_ORCL Database Connection and click Refresh to refresh the Database Connection.

htm 8/7/2010 .Managing Your Database Using Oracle SQL Developer Page 23 of 74 Back to Topic List Adding Table Constraints In this topic. http://www..oracle. Right-click DEPENDENTS table and select Edit. Perform the following steps: 1.com/technology/obe/sqldev/sqldev. you create the Primary and Foreign Key Constraints for the DEPENDENTS table..

oracle. Click the Primary Key node in the tree. http://www.com/technology/obe/sqldev/sqldev.htm 8/7/2010 .Managing Your Database Using Oracle SQL Developer Page 24 of 74 2.

Managing Your Database Using Oracle SQL Developer Page 25 of 74 3. Select the ID column and click > to shuttle the value to the Selected Columns window. Then click the Foreign Key node in the tree.oracle.htm 8/7/2010 .com/technology/obe/sqldev/sqldev. http://www.

oracle.Managing Your Database Using Oracle SQL Developer Page 26 of 74 4.htm 8/7/2010 .com/technology/obe/sqldev/sqldev. Click Add. http://www.

Managing Your Database Using Oracle SQL Developer Page 27 of 74 5. http://www.htm 8/7/2010 .oracle.com/technology/obe/sqldev/sqldev. Select EMPLOYEES for the Referenced Table and select RELATIVE_ID for the Local Column and click OK.

Managing Your Database Using Oracle SQL Developer Page 28 of 74 Back to Topic List Adding Data to a Table You can add data to a table by performing the following steps: 1.oracle.htm 8/7/2010 .com/technology/obe/sqldev/sqldev. http://www. Click the Data tab.

com/technology/obe/sqldev/sqldev. http://www.Managing Your Database Using Oracle SQL Developer Page 29 of 74 2.htm 8/7/2010 . Then click the Insert Row icon.oracle.

com/technology/obe/sqldev/sqldev.oracle. ID 209 FIRST_NAME Sue LAST_NAME Littlefield BIRTHDATE 01-JAN- 97 RELATION Daughter RELATIVE_ID 110 http://www.Managing Your Database Using Oracle SQL Developer Page 30 of 74 3. Enter the following data and click the Commit Changes icon.htm 8/7/2010 .

The row you just added was committed to the database.Managing Your Database Using Oracle SQL Developer Page 31 of 74 4. http://www.oracle.htm 8/7/2010 .com/technology/obe/sqldev/sqldev.

oracle.com/technology/obe/sqldev/sqldev. You can also load multiple rows at one time using a script.Managing Your Database Using Oracle SQL Developer Page 32 of 74 5. Click the SQL Worksheet HR_ORCL tab. http://www.htm 8/7/2010 .

htm 8/7/2010 . http://www.com/technology/obe/sqldev/sqldev. Right-click in the SQL Statement area and select Open File.Managing Your Database Using Oracle SQL Developer Page 33 of 74 6.oracle.

Managing Your Database Using Oracle SQL Developer Page 34 of 74

7. Navigate to your working directory (e.g. c:\wkdir) and select the load_dep.sql file and click
Open.

http://www.oracle.com/technology/obe/sqldev/sqldev.htm 8/7/2010

Managing Your Database Using Oracle SQL Developer Page 35 of 74

8. The SQL from the script is shown. Click the Run Script icon.

http://www.oracle.com/technology/obe/sqldev/sqldev.htm 8/7/2010

Managing Your Database Using Oracle SQL Developer Page 36 of 74

9. The data was inserted. To view the data, click the DEPENDENTS tab.

http://www.oracle.com/technology/obe/sqldev/sqldev.htm 8/7/2010

Managing Your Database Using Oracle SQL Developer Page 37 of 74 10. http://www.oracle. Click Refresh to show all the data.htm 8/7/2010 .com/technology/obe/sqldev/sqldev.

oracle.com/technology/obe/sqldev/sqldev.htm 8/7/2010 . All the data is displayed http://www.Managing Your Database Using Oracle SQL Developer Page 38 of 74 11.

for example.com/technology/obe/sqldev/sqldev. http://www. select Export and then one of the file types.Managing Your Database Using Oracle SQL Developer Page 39 of 74 12.htm 8/7/2010 . You can export the data so it can be used in another tool.oracle. Excel. Right-click on one of the values in any column.

Specify the directory and name of the file and click Apply.oracle.htm 8/7/2010 . http://www.Managing Your Database Using Oracle SQL Developer Page 40 of 74 13.com/technology/obe/sqldev/sqldev.

you see the following.CSV file. http://www. If you review the DEPENDENTS.htm 8/7/2010 .com/technology/obe/sqldev/sqldev.oracle.Managing Your Database Using Oracle SQL Developer Page 41 of 74 14.

htm 8/7/2010 . right-click and select Open SQL Worksheet.oracle.com/technology/obe/sqldev/sqldev. Select the HR_ORCL Database Connection.Managing Your Database Using Oracle SQL Developer Page 42 of 74 Back to Topic List Accessing Data One way to access DEPENDENTS data is to generate a SELECT statement on the DEPENDENTS table and add a WHERE clause. Perform the following steps: 1. http://www.

Managing Your Database Using Oracle SQL Developer Page 43 of 74 2.oracle. http://www.htm 8/7/2010 . Drag and Drop the DEPENDENTS table from the list of database objects to the SQL statement area.com/technology/obe/sqldev/sqldev.

. http://www. Right-click and select Format SQL. A SELECT statement is shown with all the columns contained in the DEPENDENTS table.oracle..htm 8/7/2010 .com/technology/obe/sqldev/sqldev.Managing Your Database Using Oracle SQL Developer Page 44 of 74 3.

Managing Your Database Using Oracle SQL Developer Page 45 of 74 4.oracle. Then click the Execute Statement icon. http://www.htm 8/7/2010 .com/technology/obe/sqldev/sqldev.'. Add the WHERE clause where relative_id > 110 to the end of the SELECT statement BEFORE the '.

Managing Your Database Using Oracle SQL Developer Page 46 of 74 5. http://www. The results are shown.oracle.htm 8/7/2010 .com/technology/obe/sqldev/sqldev.

Perform the following steps: 1. you can create a custom report based on the SQL. In addition.com/technology/obe/sqldev/sqldev.oracle. right-click and select Copy. you can run a report of your database data dictionary using bind variables. http://www.htm 8/7/2010 .Managing Your Database Using Oracle SQL Developer Page 47 of 74 Back to Topic List Creating Reports Since the SQL you just ran in the previous topic needs to be executed frequently. Select the SQL in the HR_ORCL SQL Worksheet that you executed.

htm 8/7/2010 . http://www.Managing Your Database Using Oracle SQL Developer Page 48 of 74 2.oracle. Click the Reports tab.com/technology/obe/sqldev/sqldev.

htm 8/7/2010 . http://www.Managing Your Database Using Oracle SQL Developer Page 49 of 74 3. Right-click User Defined Reports and select Add Folder.com/technology/obe/sqldev/sqldev.oracle.

Managing Your Database Using Oracle SQL Developer Page 50 of 74 4. 5.com/technology/obe/sqldev/sqldev. http://www. Click Apply. Right-click DEPENDENT Reports and select Add Report. Enter the Folder Name DEPENDENTS Reports. You can add a Description and Tooltip of your choice.oracle.htm 8/7/2010 .

Then click Apply.htm 8/7/2010 . Enter a Name for the report and click ctrl+v in the SQL area to paste the SQL you copied from the HR_ORCL SQL Worksheet.com/technology/obe/sqldev/sqldev. http://www.oracle.Managing Your Database Using Oracle SQL Developer Page 51 of 74 6.

Select the Report you just created. http://www.htm 8/7/2010 .oracle.Managing Your Database Using Oracle SQL Developer Page 52 of 74 7.com/technology/obe/sqldev/sqldev.

http://www. Select HR_ORCL from the drop list and click OK to connect to your database.htm 8/7/2010 .oracle.com/technology/obe/sqldev/sqldev.Managing Your Database Using Oracle SQL Developer Page 53 of 74 8.

htm 8/7/2010 . 10. Expand Data Dictionary Reports > Data Dictionary.oracle. Then select Dictionary Views. http://www. The results of your report are shown.Managing Your Database Using Oracle SQL Developer Page 54 of 74 9. You can also run a Data Dictionary report.com/technology/obe/sqldev/sqldev.

All the Data Dictionary views that contain 'col' in its name are displayed.oracle. Enter col for the Value and click Apply.Managing Your Database Using Oracle SQL Developer Page 55 of 74 11.com/technology/obe/sqldev/sqldev. 12.htm 8/7/2010 . http://www.

http://www.com/technology/obe/sqldev/sqldev. you create a Package Spec and Package Body that adjusts an employees salary.oracle.Managing Your Database Using Oracle SQL Developer Page 56 of 74 Back to Topic List Debugging and Executing PL/SQL Oracle SQL Developer contains extensive PL/SQL debugging capabilities.htm 8/7/2010 . Select File > Open using the main menu. In this topic. Perform the following steps: 1.

sql and click Open.com/technology/obe/sqldev/sqldev.\wkdir) select createHRpack. From your working directory (c.oracle. http://www.htm 8/7/2010 .Managing Your Database Using Oracle SQL Developer Page 57 of 74 2.

http://www.htm 8/7/2010 .oracle. Select the HR_ORCL database connection from the the drop list on the right.com/technology/obe/sqldev/sqldev.Managing Your Database Using Oracle SQL Developer Page 58 of 74 3.

Click the Run Script icon.htm 8/7/2010 .com/technology/obe/sqldev/sqldev.oracle. http://www.Managing Your Database Using Oracle SQL Developer Page 59 of 74 4.

htm 8/7/2010 .com/technology/obe/sqldev/sqldev. http://www. Right-click the Packages node and select Refresh.oracle. The script ran successfully and the package and package body were created.Managing Your Database Using Oracle SQL Developer Page 60 of 74 5.

http://www. Expand HR_ORCL > Packages > HR_PACK and select HR_PACK to view the package definition.oracle.Managing Your Database Using Oracle SQL Developer Page 61 of 74 6.com/technology/obe/sqldev/sqldev.htm 8/7/2010 .

Select HR_PACK BODY to view the package body definition.oracle.htm 8/7/2010 .Managing Your Database Using Oracle SQL Developer Page 62 of 74 7. http://www.com/technology/obe/sqldev/sqldev.

htm 8/7/2010 .oracle. To make any changes to the Package Body.Managing Your Database Using Oracle SQL Developer Page 63 of 74 8. click the Edit icon.com/technology/obe/sqldev/sqldev. http://www.

Click on any one of the .to collapse the code or press + to expand the code.oracle.Managing Your Database Using Oracle SQL Developer Page 64 of 74 9.htm 8/7/2010 .com/technology/obe/sqldev/sqldev. http://www.

http://www. If your line numbers do not appear.htm 8/7/2010 . This is useful for debugging purposes.oracle.com/technology/obe/sqldev/sqldev. you can right-click in the line number area and click Toggle Line Numbers to turn them on.Managing Your Database Using Oracle SQL Developer Page 65 of 74 10.

com/technology/obe/sqldev/sqldev.oracle.htm 8/7/2010 . select Packages > HR_PACK. In the Connections Navigator. http://www. right-click and select Run.Managing Your Database Using Oracle SQL Developer Page 66 of 74 11.

Managing Your Database Using Oracle SQL Developer Page 67 of 74 12.htm 8/7/2010 . A parameter window appears. You need to set the input parameters here.com/technology/obe/sqldev/sqldev.oracle. http://www.

their salary is increased by 20%.2. Click OK. The current SALARY for EMPLOYEE_ID 102 is 17000. http://www.com/technology/obe/sqldev/sqldev.htm 8/7/2010 . What this means is that the Employee who has the ID 102.Managing Your Database Using Oracle SQL Developer Page 68 of 74 Set the P_ID to 102 and P_INCREMENT to 1.oracle.

oracle. http://www.com/technology/obe/sqldev/sqldev. The value returned is 20400.htm 8/7/2010 .Managing Your Database Using Oracle SQL Developer Page 69 of 74 13.

http://www.htm 8/7/2010 .com/technology/obe/sqldev/sqldev. right-click on HR_PACK in the navigator and select Run.oracle.Managing Your Database Using Oracle SQL Developer Page 70 of 74 14. To test the Exception Handling.

htm 8/7/2010 .oracle. change the P_INCREMENT value to 5 and click OK. http://www. This time.Managing Your Database Using Oracle SQL Developer Page 71 of 74 15.com/technology/obe/sqldev/sqldev.

com/technology/obe/sqldev/sqldev. http://www.oracle.Managing Your Database Using Oracle SQL Developer Page 72 of 74 16. In this case.htm 8/7/2010 . an exception was raised with "Invalid increment value" because the P_INCREMENT value was greater than 1.5.

you learned how to: Create a database connection Add a new table using the Table Dialog Box Change a table definition Add data to a table Access data Generate a report Debug and execute PL/SQL Back to Topic List http://www.Managing Your Database Using Oracle SQL Developer Page 73 of 74 Back to Topic List Summary In this tutorial.oracle.htm 8/7/2010 .com/technology/obe/sqldev/sqldev.

oracle.com/technology/obe/sqldev/sqldev. http://www.htm 8/7/2010 .Managing Your Database Using Oracle SQL Developer Page 74 of 74 Move your mouse over this icon to hide all screenshots.