<Do not delete this text because it is a placeholder for the generated list of "main" topics when run in a browser>
This tutorial introduces Oracle SQL Developer 3.0 and shows you how to manage your database objects.
Approximately 50 minutes
Oracle SQL Developer is a free graphical tool that enhances productivity and simplifies database development tasks. Using SQL Developer, you 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 number of developers using alternative platforms. Multiple platform support also means that users can install SQL Developer on the Database Server and connect remotely 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 you can connect to any supported Oracle Database.
Before starting this tutorial, you should:
. |
Install Oracle SQL Developer 3.0 from OTN. Follow the readme instructions here. |
---|---|
. |
Install Oracle Database 11g with the Sample schema. |
. |
Unlock the HR user. Login to SQL Developer as the SYS
user and execute the following command: Note: This tutorial is developed using Oracle SQL Developer 3.0. |
---|
. |
Download and unzip the files.zip to a local folder on your file system. In this tutorial, we use the C:\sqldev3.0 folder. |
---|
The first step to managing database objects using Oracle SQL Developer 3.0 is to create a database connection. Perform the following steps:
. |
If you installed the SQL Developer icon on your desktop, click the icon to start your SQL Developer and move to Step 4. If you do not have the icon located on your desktop, perform the following steps to create a shortcut to launch SQL Developer 3.0 directly from your desktop. Open the directory where the SQL Developer 3.0 is located, right-click sqldeveloper.exe (on Windows) or sqldeveloper.sh (on Linux) and select Send to > Desktop (create shortcut).
|
---|---|
. |
On the desktop, you will find an icon named Shortcut to sqldeveloper.exe. Double-click the icon to open SQL Developer 3.0. Note: To rename it, select the icon and then press F2 and enter a new name.
|
. |
Your Oracle SQL Developer opens.
|
. |
In the Connections navigator, right-click Connections and select New Connection.
|
. |
The New / Select Database Connection dialog opens. Enter the connection details as follows and click Test. Connection Name: HR_ORCLUser Name: hr Password: <your_password> (Select the Save Password checkbox) Hostname: localhost SID: <your_own_SID>
|
. |
Check for the status of the connection on the left-bottom side (above the Help button). It should read Success. Click Connect. Then click Save.
|
. |
The connection was saved and you see the newly created connection in the Connections list.
|
. |
Expand HR_ORCL. Note: When a connection is opened, a SQL Worksheet is opened automatically. The SQL Worksheet allows you to execute SQL against the connection you have opened.
|
. |
Expand Tables.
|
. |
Select the EMPLOYEES table to view the table definition. Then click the Data tab.
|
. |
The data in the table is shown. In the next topic, you create a new table and populate the table with data.
|
. |
Click the DEPARTMENTS table in the Connections navigator.
|
. |
There are a number of constraints for the DEPARTMENTS table. To view the various constraints on the table, click the Constraints tab.
|
. |
Note that the DEPARTMENTS table has 4 constraints. Each constraint has a unique name. The CONSTRAINT_TYPE identifies the type of constraint on the table. Click Edit .
|
. |
The Edit Table dialog has a number of fields. Select Foreign Keys beneath the Search field.
|
. |
Review the Foreign Keys constraints. Note that the DEPARTMENTS table has two foreign keys, one named DEPT_LOC_FK and the other named DEPT_MGR_FK. When you select one of the names in the Foreign Keys field, the details about that foreign key appear. Click OK to leave the Edit Table dialog.
|
---|
In this section, you create a new table called DEPENDENTS. You create columns in the DEPENDENTS table. Your table will have the columns ID, FIRST_NAME, LAST_NAME, BIRTHDATE, and RELATION.
Later, you create a foreign key on the DEPENDENTS table to associate the column's values with the EMPLOYEES table. Perform the following steps:
. |
Right-click Tables and select New TABLE...
|
---|---|
. |
Enter DEPENDENTS for the Table Name and select the Advanced check box.
|
. |
For the first column in your table, enter ID for the Name, select NUMBER for the Datatype and enter 6 for the Precision (length of the number). Select Cannot be NULL, then click Add Column . Note: By checking Cannot be NULL, you are adding a constraint on the table that specifies the column must hold values.
|
. |
For the next column, enter FIRST_NAME for the Name, leave the type as VARCHAR2 and enter 20 for the Size to specify the length of the VARCHAR2. Then click Add Column .
|
. |
For the next column, enter LAST_NAME for the Name, leave the type as VARCHAR2 and enter 25 for the Size. Select the Cannot be NULL check box. Then click Add Column .
|
. |
For the next column, enter BIRTHDATE for the Name, select DATE for the type. Then click Add Column .
|
. |
For the last column, enter RELATION for the Name, leave type as VARCHAR2 and enter 25 for the Size. Click OK to create the table.
|
. |
Your new table appears in the list of tables.
|
Oracle SQL Developer makes it very easy to make changes to database objects. In this topic, you add a column called RELATIVE_ID to the DEPENDENTS table you just created. Perform the following steps:
. |
In the Connections navigator, select the DEPENDENTS table.
|
---|---|
. |
Right-click, select Column then Add...
|
. |
Enter RELATIVE_ID, select NUMBER for the Data Type (from the drop list), set the Precision to 6 and Scale to 0. Click Apply. Note: If you want to see the SQL that is generated for you, click the SQL tab.
|
. |
The confirmation verifies that a column has been added. Click OK.
|
. |
Expand the DEPENDENTS table to review the updates. The RELATIVE_ID column is added to the table definition.
|
In this topic, you create the Primary and Foreign Key Constraints for the DEPENDENTS table. Constraints are a way of validating data. A Primary Key constraint ensures that a column's values are unique and present (not null). A Foreign Key constraints maintains data integrity between two different columns (one of which may be in a separate table).
Perform the following steps:
. |
Right-click DEPENDENTS table and select Edit...
|
---|---|
. |
Click the Primary Key node in the tree.
|
. |
Select the ID column and click to shuttle the value to the Selected Columns window. This identifies that the ID column in the DEPENDENTS table has a Primary Key constraint. All of the values in this column must be unique and must be present.
|
. |
Next, you add a Foreign Key constraint. Select the Foreign Keys node in the tree and click Add.
|
. |
You will link the ID RELATIVE_ID column in the DEPENDENTS table to the ID column in the EMPLOYEES table. This will ensure that any value added in the the RELATIVE_ID column must match an existing value in the ID column of the EMPLOYEES table. Select EMPLOYEES for the Referenced Table drop list. Ensure that the Referenced Constraint field holds the value EMP_EMP_ID_PK. In the Associations field, select RELATIVE_ID for the Local Column field and click OK.
|
You can add data to the DEPENDENTS table by performing the following steps:
. |
With the DEPENDENTS table still selected, you should have the Data tab already selected. If not, select it.
|
||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
. |
Then click Insert Row .
|
||||||||||||||||||
. |
Enter the following data and then click Commit to save the row to the database.
|
||||||||||||||||||
. |
The outcome of the commit action displays in the log window. The SQL statement that was committed is listed beneath the Commit Successful message.
|
||||||||||||||||||
. |
You can also load multiple rows at one time using a script. Click File > Open...
|
||||||||||||||||||
. |
Navigate to the sqldev3.0 directory, select the load_dep.sql file and click Open.
|
||||||||||||||||||
. |
Select the HR_ORCL connection in the connection drop list to the right of the SQL Worksheet.
|
||||||||||||||||||
. |
The SQL from the script is shown. Click Run Script . The data is inserted. Click the DEPENDENTS tab to view the results.
|
||||||||||||||||||
. |
To view the data, make sure the Data tab is selected and click Refresh to show all the data. All of the data is displayed.
|
||||||||||||||||||
. |
You can export the data so it can be used in another tool, such as, a spreadsheet. Right-click on one of the values in any column and select Unload.
|
||||||||||||||||||
. |
Select one of the file types, such as csv. You can identify a file location and name for the output. In this example, call the file export.csv and place it in your working directory. Click Next.
|
||||||||||||||||||
. |
In step 2, you can specify the data. It defaults to selecting all rows of the table. You can add a WHERE clause. Accept the defaults and click Next.
|
||||||||||||||||||
. |
Click Finish.
|
||||||||||||||||||
. |
If you review the export.csv file, you should see the following:
|
One way to access the DEPENDENTS data is to generate a SELECT statement on the DEPENDENTS table and add a WHERE clause. Perform the following steps:
. |
Select the HR_ORCL Database Connection, right-click and select Open SQL Worksheet.
|
---|---|
. |
Drag and Drop the DEPENDENTS table from the list of database objects in the Connections navigator to the SQL statement worksheet area.
|
. |
A dialog window appears. You can specify the type of SQL statement to create. Accept the default to create a SELECT statement and click Apply.
|
. |
Your SELECT statement is displayed. You can modify it in the SQL Worksheet and run it.
|
. |
Add the WHERE clause where relative_id > 110 to the end of the SELECT statement BEFORE the ';'. Click Run Statement .
|
. |
The results are shown. Only rows where the RELATIVE_ID > 110 are displayed.
|
If you need to execute a SQL statement frequently, you can create a custom report based on the SQL. In addition, you can run a report of your database data dictionary using bind variables. Bind variables enable you to run your query multiple times and pass in different values.
Perform the following steps:
. |
Highlight the SQL statement in the HR_ORCL SQL Worksheet that you executed, right-click and select Create Report...
|
---|---|
. |
Enter the name RELATIVE_ID greater than 110 into the Name field. Optionally, you can add a Description and a Tooltip. Click Apply.
|
. |
Select the Reports tab, expand User Defined Reports. You should see the report you created in the previous step. Select the report you just created.
|
. |
In the Select Connection dialog, select HR_ORCL from the drop list and click OK to connect to your database and run the report.
|
. |
The results of your report are shown.
|
. |
You can also run a Data Dictionary report. Data dictionary reports hold meta data about your objects. This includes information such as your table definitions. Expand Data Dictionary Reports > Data Dictionary. Then select Dictionary Views.
|
. |
Deselect the NULL check box, enter col for the Value and click Apply.
|
. |
All the Data Dictionary views that contain 'col' in its name are displayed. Note: in the output shown above, the ALL_CONS_COLUMNS would hold information on constraints on columns. The ALL_COLL_TYPES holds information on the data types for your columns.
|
Oracle SQL Developer contains extensive PL/SQL editing capabilities. In this topic, you create a Package Specification and s Package Body. This package adjusts an employee's salary. Perform the following steps:
. |
Select File > Open using the main menu.
|
---|---|
. |
Browse to the sqldev3.0 directory, select createHRpack.sql. Click Open.
|
. |
Select the HR_ORCL database connection from the the drop list on the right. Examine the code.
|
. |
Click Run Script .
|
. |
The package specification and the package body compile successfully. Note: The script first drops the package, so the first time you run this script, you will see ora errors. Ignore the errors and observe the output at the bottom that the package is created. Click the Connections navigator.
|
. |
Expand HR_ORCL > Packages and select HR_PACK to verify and view the package definition.
|
. |
Double-click HR_PACK BODY to view the package body definition.
|
. |
Click any one of the - to collapse the code or press + to expand the code.
|
. |
If your line numbers do not appear, you can right-click in the line number area and click Toggle Line Numbers to turn them on. This is useful for debugging purposes.
|
. |
In the Connections Navigator, select Packages > HR_PACK, right-click and select Run.
|
. |
A parameter window appears. Make sure that the GET_SAL target is selected. You need to set the input parameters here for P_ID and P_INCREMENT. Input parameters are values that you are passing into the program at runtime.
|
. |
Set the P_ID to 102 and P_INCREMENT to 1.2. This means for that the Employee who has the ID 102, their salary is increased by 20%. The current SALARY for EMPLOYEE_ID 102 is 17000. Click OK.
|
. |
The value returned is 20400.
|
. |
To test the Exception Handling, right-click on HR_PACK in the navigator and select Run.
|
. |
This time, change the P_INCREMENT value to 5 and click OK.
|
. |
In this case, an exception was raised with "Invalid increment amount" because the P_INCREMENT value was greater than 1.5.
|
In this tutorial, you have learned how to:
About Oracle |Oracle and Sun | | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Your Privacy Rights | |