Escolar Documentos
Profissional Documentos
Cultura Documentos
Adding a New Table Using the Create Table Dialog Box ....................................................... 12
Summary ................................................................................................................................... 41
This tutorial introduces Oracle SQL Developer 4.0 and shows you how to manage your database objects.
Time to Complete
Approximately 50 minutes
Introduction
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.
Prerequisites
Install Oracle SQL Developer 4.0 from OTN. Follow the readme instructions here .
Note: This tutorial was created in a Linux environment. If you are using a Windows environment, the
screenshots may be slightly different.
The first step to managing database objects using Oracle SQL Developer 4.0 is to create a database connection.
Perform the following steps:
1. 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 4.0 directly from your desktop.
In Windows, open the directory where the SQL Developer 4.0 is located, right-click
sqldeveloper.exe and select Send to > Desktop (create shortcut).
2. On the desktop, you will find an icon named Shortcut to sqldeveloper.exe (in Windows) or SQL
Developer4.0 (in Linux). Double-click the icon to open SQL Developer 4.0.
5. The New / Select Database Connection dialog opens. Enter the connection details as follows and
click Test.
6. 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.
7. The connection was saved and you see the newly created connection in the Connections list.
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.
9. Expand Tables.
11. The data in the table is shown. In the next topic, you create a new table and populate the table with
data.
13. To filter the table data, click on the filter icon next to the column you want to set the filter on. Here
the filter is set in the SALARY column.
16. There are a number of constraints for the DEPARTMENTS table. To view the various constraints on
the table, click the Constraints tab.
Click Edit .
18. The Edit Table dialog has a number of fields. Select Constraints beneath the Search field.
19. 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.
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.
Perform the following steps:
2. Enter DEPENDENTS for the Table Name and select the Advanced check box.
Note: By checking Cannot be NULL, you are adding a constraint on the table that specifies the
column must hold values.
4. 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 .
6. For the next column, enter BIRTHDATE for the Name, select DATE for the type. Then click Add
Column .
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:
Note: If you want to see the SQL that is generated for you, click the SQL tab.
Click OK.
In this topic, you create the Primary and Foreign Key Constraints for the 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:
4. 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.
6. You will link the 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.
Under the Referenced Constraint section, ensure the schema selected is HR. Select EMPLOYEES
from Table drop list. Ensure that the 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:
1. With the DEPENDENTS table still selected, you should have the Data tab already selected. If not,
select it.
3. Enter the following data and then click Commit to save the row to the database.
ID: 209
FIRST_NAME: Sue
LAST_NAME: Littlefield
BIRTHDATE: 01-JAN-97
RELATION: Daughter
RELATIVE_ID: 110
4.
5. The outcome of the commit action displays in the log window. The SQL statement that was
committed is listed beneath the Commit Successful message.
7. Navigate to the sqldev3.0 directory, select the load_dep.sql file and click Open.
The data is inserted. Click the DEPENDENTS tab to view the results.
11. 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 Export.
Click Next.
Accessing Data
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:
1. Select the HR_ORCL Database Connection, right-click and select Open SQL Worksheet
3. 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.
5. Add the WHERE clause where relative_id > 110 to the end of the SELECT statement BEFORE
the ';'.
6. 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:
1. Highlight the SQL statement in the HR_ORCL SQL Worksheet that you executed, right-click and
select Create Report...
3. 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.
4. In the Select Connection dialog, select HR_ORCL from the drop list and click OK to connect to your
database and run the report.
Expand Data Dictionary Reports > Data Dictionary. Then select Dictionary Views.
7. Deselect the NULL check box, enter col for the Value and click Apply.
8. All the Data Dictionary views that contain 'col' in its name are displayed.
Oracle SQL Developer contains extensive PL/SQL editing capabilities. In this topic, you create a Package
Specification and Package Body. This package adjusts an employee's salary.
Perform the following steps:
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.
8. Click any one of the - to collapse the code or press + to expand the code.
10. You can split the code editor display either horizontally or vertically. To do so, right click HR_PACK
Body and select Split Vertically for a vertical split or Split Horizontally for a horizontal split.
13. 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.
16. To test the Exception Handling, right-click on HR_PACK in the navigator and select Run.
17. This time, change the P_INCREMENT value to 5 and click OK.
Summary