You are on page 1of 90

SQL DEVELOPER CONCEPTS AND USAGE

Preface
1.1About SQL Developer
Oracle SQL Developer is a graphical version of SQL*Plus that gives database developers a convenient
way to perform basic tasks. You can browse, create, edit, and delete (drop), run SQL statements and
scripts also edit and debug PL/SQL code, manipulate and export (unload) data and view and create
reports.

You can connect to any target Oracle Database schema using standard Oracle Database
authentication. Once connected, you can perform operations on objects in the database.

You can connect to schemas for MySQL and selected third-party (non-Oracle) databases, such as
Microsoft SQL Server, Sybase Adaptive Server, and IBM DB2, Teradata and view metadata and data
in these databases and you can migrate these databases to Oracle Database.

1.2 Installing and Getting Started with SQL Developer


To install and start SQL Developer, you simply download a ZIP file and unzip it into a desired parent
directory or folder, and then type a command or double-click a file name. You should read
the Oracle SQL Developer Installation Guide before you perform the installation. After you have read
the installation guide, the basic steps are:

1. Unzip the SQL Developer kit into a folder (directory) of your choice, which will be referred to
as <sqldeveloper_install>. Unzipping the SQL Developer kit causes a folder
named sqldeveloper to be created under the <sqldeveloper_install> folder.
For example, if you unzip the kit into C:\, the folder C:\sqldeveloper is created, along with
several subfolders under it.
If Oracle Database (Release 11 or later) is also installed, a version of SQL Developer is also
included and is accessible through the menu system under Oracle. This version of SQL
Developer is separate from any SQL Developer kit that you download and unzip on your
own, so do not confuse the two, and do not unzip a kit over the SQL Developer files that are
included with Oracle Database. Suggestion: Create a shortcut for the SQL Developer
executable file that you install, and always use it to start SQL Developer.

2. To start SQL Developer, go to the sqldeveloper directory under


the <sqldeveloper_install> directory, and do one of the following:
On Linux and Mac OS X systems, run sh sqldeveloper.sh.
On Windows systems, double-click sqldeveloper.exe.
If you are asked to enter the full pathname for the JDK, click Browse and find it. For example,
on a Windows system the path might have a name similar to C:\Program
Files\Java\jdk1.7.0_51. (If you cannot start SQL Developer, it could be due to an error in
specifying or configuring the JDK.), if the JDK not available in your system install JDK latest
version from oracle site then proceed for sqldeveloper installation.

If you want to become familiar with SQL Developer concepts before using the interface, read
the rest of this chapter before proceeding to the next step.
2.1 Creating Database Connection
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. If you do not have the icon located on your desktop, perform the following steps
to create a shortcut to launch SQL Developer 19.2 directly from your desktop.

In Windows, open the directory where the SQL Developer 19.2 is located, right-
click sqldeveloper.exe  and select Send to > Desktop (create shortcut).

In Linux, right-click in your Desktop and select Create Launcher and enter the application
details.

2. On the desktop, you will find an icon named Shortcut to sqldeveloper.exe (in Windows)


or SQL Developer 19.2 (in Linux). Double-click the icon to open SQL Developer 19.2.
3. Your Oracle SQL Developer opens.

4. In the Connections navigator, right-click Connections and select New Connection.

5. The New / Select Database Connection dialog opens. Enter the connection details as follows
and click Test.

Connection Name: HR_ORCLPDB
User Name: hr
Password: <your_password> (Select the Save Password checkbox)(default password <hr> )
Connection Type: Basic
Connection Color: Select the red color. (The color is set as the border of any SQL Worksheet
and other windows associated with the connection. Setting the color connection is useful
when working with multiple connections.)
Hostname: localhost
Service Name: ORCLPDB
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. Connection was saved and you can see the newly created Connection < HR_ORCLPDB> in the
connection list, now right click on the connection created then click connect.
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.

2.2 Creating User and Granting Privileges

Expand the sys connection. Right-click Other Users and select Create User.


Enter the following and click the System Privileges tab.

User name: test
Password test
Default Tablespace Users
Temporary Tablespace Temp

Select the following privileges and click Apply.

CREATE PROCEDURE
CREATE SEQUENCE
CREATE SESSION
CREATE TABLE
CREATE TRIGGER
CREATE TYPE
CREATE VIEW
UNLIMITED TABLESPACE
 

Review the SQL that was executed. Click Close.


 

2.3 Exporting Objects

You can use the Export Wizard to export some or all database objects of a database connection to a
file. The export file will not only contain the SQL data definition language (DDL) statements to create
the exported objects, but if you choose to export the data too, the export file will contain Data
Manipulation Language (DML) to populate the exported objects.

1. Select Tools > Database Export


2. Enter c:\< directory where you want>\export.sql in the File field and select
the hr_orcl connection.

3. Under the DDL options the Terminator and Pretty Print DDL options are selected by default.
Review the rest of the options available, leaving them unchecked. Click Next.
4. If the export file exists, you receive a prompt to confirm overwriting. Click Yes.

5. The Types to Export window includes the Object Types you want to be exported from
the hr_orcl connection. By default all the object types are selected. Click Toggle All check
box to deselect all the object types.
6. Select only the object type Tables and then click Next.

Note: You must select at least one object type.


7. Click Go to search all objects that meet the selection criteria you specified in the previous
step.
8. All tables owned by hr are listed. Click the tables EMPLOYEES and DEPARTMENTS and then
use the arrow key to move them to the list box on the right hand side of the window.
9. Click Next.

10. You can review the information that will be used to create the output file, which will contain
statements to export database objects and data according to your specifications. To create
the output file, click Finish.
11. The file is also displayed in a SQL Worksheet window, where you can edit it and run it as a
script. Select the test_orcl connection in the drop down list.
12. Click Run Script. 
 

13. The script ran successfully. In the Connections navigator, expand the test_orcl connection.
14. Expand Tables. The DEPARTMENT and EMPLOYEES tables appear in the list.
2.4 Copying Schemas

You can use the Database Copy to copy objects directly from a source to a target schema. This is a quick and very useful
way of copying all tables and data from one schema to another. To copy tables between two schemas perform the
following steps:

1. Select Tools > Database Copy

2. You will copy the schema objects from the hr_orcl connection to the test_orcl connection.


Select hr_orcl for Source Connection and test_orcl for Destination Connection and
click Next.
3. Notice in the Copy Summary that all the tables in HR will be created in Test, even the EMPLOYEES
and DEPARTMENTS. Click Finish.
4.The result of the comparison is displayed in a SchemaCopy log file in the SQL Developer
window. Review the full results log. Do you expect the errors you find? Click Refresh to see the
newly copied objects.
5.The newly copied objects are displayed.

6.Scroll down in the SchemaCopy log to see that you received an error when the CREATE TABLE
"DEPARTMENTS" statement was run indicating that it already existed.
2.5 Browse and Navigate through Objects
Once the Connection is created Expand the Connection Create < HR_ORCLPDB > to see all the
objects in the database like tables, views, indexes, constraints, functions, procedures, packages and
trigger and many as below under the connections .
1. Expand Tables Object to see all the tables

Note: Like table you can Expand any objects in the list to see those specific objects like
views, indexes, constraints, functions, procedures, packages and triggers ..etc
2. Select the EMPLOYEES table to view the table definition.

3. Then click the Data tab.

4. To see the data model for EMPLOYEE table click on Model Tab
5. To see the constraints of EMPLOYEE table click on Constraints tab.

6. You can sort the EMPLOYEES table data by double clicking the arrow icon next to the column you
want to sort on. Here the table data is sorted on EMP_ID.
7. 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.

8. To remove the filter, click Remove.


9. Click the DEPARTMENTS table in the Connections navigator.

10. There are a number of constraints for the DEPARTMENTS table. To view the various
constraints on the table, click the Constraints tab.

11. 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  .

12. The Edit Table dialog has a number of fields. Select Constraints beneath the Search field.
13. 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.
2.6 Adding a New Table Using the Create Table Dialog Box
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:

1. Right-click Tables and select New TABLE...

2. Enter DEPENDENTS for the Table Name and select the Advanced check box.


3. 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.
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   .
5. 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   .
6. For the next column, enter BIRTHDATE for the Name, select DATE for the type. Then
click Add Column   .

7. 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.
8. New Table DEPENDENTS appears in the list of Tables

2.7 Changing a Table Definition


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:

1. In the Connections navigator, select the DEPENDENTS table.

2. Right-click DEPENDENTS Table, select Column then Add...


3. Enter RELATIVE_ID, select NUMBER for the Data Type (from the drop list), set the Precision
to 6 and Scale to 0.
Note: If you want to see the SQL that is generated for you, click the SQL tab.

4. The confirmation verifies that a column has been added.


Click OK.

5. Expand the DEPENDENTS table to review the update


2.8 Adding Table Constraints

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:

1. Right-click DEPENDENTS table and select Edit...


2. Click the Constraints node in the tree.

3. Click the drop down arrow next to the Add Constraint icon   and select New Primary Key
Constraint.

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.

5. Next, you add a Foreign Key constraint. Click the drop down arrow next to the Add
Constraint icon   and select New Foreign Key Constraint
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.
7. To see the updated constraints click DEPENDENTs Table and click Constraints tab on Right
Pane.
2.9 Adding Data to the Table

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.

2. Then click Insert Row  .

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. The outcome of the commit action displays in the log window. The SQL statement that was
committed is listed beneath the Commit Successful message.
5. You can also load multiple rows at one time using a script. Click File > Open...

6. Navigate to the scripts directory directory, select the load_dep.sql file and click Open.


7. Select the HR_ORCLPDB connection in the connection drop list to the right of the SQL
Worksheet.

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


The data is inserted. Click the DEPENDENTS tab to view the results.

9. 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


2.10 Exporting Data

1. 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.
2. Click on the drop down next to Format and select the format you want to export the data
into, such as csv. Note that you have various format options available.

Click Next.
Browse the target location and give the file name to save
3. The Export Summary is displayed. Click Finish.
4. Review the Dependens_data.csv file, you should see the following:

2.11 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_ORCLPDB Database Connection, right-click and select Open SQL


Worksheet
Drag and drop the DEPENDETNS table from Tables list on Left side under Connection to SQL
WORK AREA in SQL Work Sheet.
2. 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.

3. Your SELECT statement is displayed. You can modify it in the SQL Worksheet and run it.

4. Add the WHERE clause where relative_id > 110 to the end of the SELECT statement


BEFORE the ';'.

Click Run Statement   .
5. The results are shown. Only rows where the RELATIVE_ID > 110 are displayed.

Note : Instead of Drag and drop the Table object on SQL Work Area directly we can write the query
in SQL Worksheet are and run the script to get the result.

2.12 Creating Reports

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_ORCLPDB SQL Worksheet that you executed, right-
click and select Create Report...
2. Enter the name RELATIVE_ID greater than 110 into the Name field. Optionally, you can add
a Description and a Tooltip. Click Apply.

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_ORCLPDB from the drop list and click OK to
connect to your database and run the report.

5. The results of your report are shown.


6. 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.

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.
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.

2.13 Delete Selected Row(s) in sqldeveloper

To delete row(s) from Data Tab On the Connections tab, expand hr_orclpdb.

Under the hr_orclpdb icon, a list of schema object types appears.

1. Expand Tables.

A list of tables appears.

2. Select DEPENDENTS table.

3. In the right frame, click the tab Data.

The Data pane appears, showing the rows of the DEPENDENTS  table.

4. Click the row where FIRST_NAME is Jill and Vicki.


5. Click the icon Delete Selected Row(s).

6. Click the icon Commit Changes.

The row is deleted.

Under the Data pane is the Data Editor - Log pane.

7. Check the Data Editor Log pane for the message "Commit Successful".
2.14 Adding an Index with the Create Index Tool

To create an index, use either the SQL Developer tool Create Index or the DDL
statement CREATE INDEX.

This tutorial shows how to use the Create Index tool to add an index to the DEPENDENTS table
(Create Table Definition). The equivalent DDL statement is:

CREATE INDEX DEP_FNAME_IX ON DEPENDENTS (FIRST_NAME ASC);

To add an index to the DEPENDENTS table using the Create Index tool:

1. On the Connections tab, expand hr_orlcpdb.

2. Under the hr_orclpdb icon, a list of schema object types appears.

3. Expand Tables a list of tables appears, including DEPENDENTS.

4. Right-click DEPENDENTS a list of choices appears.

5. Select Index a list of choices appears.


6. Select Create Index The Create Index window opens.
7. For Schema, accept the default, HR.

For Name, type DEP_FNAME_IX Select the tab Definition.

The Definition pane shows the default values for index properties.

In the field labeled "Column Name or Expression:", type FIRST_NAME.


(For all other properties, accept the default values.)

Click OK.

Now the DEPENDENTS table has an index named DEP_FNAME_IX on the column FIRST_NAME.

2.15 Changing default date format in sqldeveloper


Default date format in sql developer is <dd-MM-rr > ex < 21-04-99 >
To display the date along with time follow the below steps.

1. Go to tools -- > Preferences -- > then select NLS


2. Change the Date Format from dd-MM-rr to the required format here I am using

DD-Mon-YYYY HH24:MI:SS then click OK and execute the Query and see the result.
Resultant output.
2.16 Creating and Executing PLSQL Objects

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:

1. Select File > Open using the main menu.

2. Browse to the scripts directory where the scripts available in your local or server,


select createHRpack.sql. Click Open.
3. Select the HR_ORCLPDB database connection from the the drop list on the right.
Examine the code.

4. Click Run Script   .

5. 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.
6. Expand HR_ORCLPDB > Packages and select HR_PACK to verify and view the package
definition.

7. Double-click HR_PACK BODY to view the package body definition.

8. Click any one of the - to collapse the code or press + to expand the code.
9. 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.
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.

11. Select Unsplit to go back to the original view.

12. In the Connections Navigator, select Packages > HR_PACK, right-click and select Run.


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.
14. 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.
15. The value returned is 82280.
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.


18. In this case, an exception was raised with "Invalid increment amount" because the
P_INCREMENT value was greater than 1.5.

2.17 Searching Objects


You can find various types of objects (tables, columns, declarations within functions or
procedures, and so on) associated with an Oracle database connection. After searching an
object you can also open it in editing pane and work with it. In Oracle SQL Developer
Release 1.5 and later, you can take advantage of the PLScope feature in Oracle Database
12c, which allows users to search for parameter declarations and references. To search for
database objects, perform the following steps

1. Select View Menu -> Then Find DB Objects


2. Then select the Connection from the drop down under object explore below.
3. Then Expand the required Object type to search specific objects.
Here I am searching for tables.
4. Type the text in search box and click lookup icon

5. Output

6. Also we can filter the objects by applying filter from object Explorer below.
Expand Connection and select and Right Click on Tables then select Apply Filter.
Select NAME from the Filter dialogue Box and Search Operator from the Drop
Downs.

See the Filtered Output below.


Connection to Teradata in SQL Developer
1 Download JDBC
You can download JDBC for Teradata at this link. The version of the driver I use is Teradata JDBC
Driver 16.20.00.13.

2 Add JDBC driver in SQL Developer


It is time to add JDBC in SQL Developer. To do this, select on the menu bar Tools -> Preferences …
Then find the tab Database -> Third Party JDBC Drivers

Click the Add Entry … button and select a .jar file from your directory. For me it looks like this:

The path to the JDBC file should appear in the list of drivers. Then click OK and restart SQL
Developer.
3. Create a database connection
Add a new connection to the database by selecting New Database Connection…

Change the database type to Teradata and enter your database settings.

Connection to MS SQL Server in SQL Developer


Connecting in SQL Developer to the Microsoft SQL Server database looks the same as in the case of
Teradata. The only thing you need to change is definitely the driver 🙂 jTDS JDBC allows you to
connect to MS SQL Server and Sybase.
1. Download JDBC
To connect to Microsoft SQL Server you will need jTDS JDBS, which you can download for free here. I
will use the jTDS JDBC 1.3.1 driver

2. Add JDBC driver in SQL Developer


1. Select Tools -> Preferences in the menu bar.
2. Then find the Database -> Third Party JDBC Drivers tab.
3. Click the Add Entry … button and select a .jar file from your directory.

4. The path to the JDBC file will appear in the list of drivers.
Then click OK and restart SQL Developer.

3. Create a database connection


1. Add a new connection to the database by selecting New Database Connection …
2. Change the database type to SQLServer and enter your database settings..
Reference Docs:
SQLDeveloper Reference :

https://docs.oracle.com/en/database/oracle/sql-developer/19.2/rptug/sql-developer-tutorial.html

https://www.appservgrid.com/documentation111/docs/rdbms12cr1/RPTUG/intro.htm#RPTUG4130
5

You might also like