Lu2 Lo5
Lu2 Lo5
Lu2 Lo5
A schema is a collection of database objects. A schema is owned by a database user and shares
the same name as the user. Schema objects are logical structures created by users. Some objects,
such as tables or indexes, hold data. Other objects, such as views or synonyms, consist of a
definition only.
Note: There is no relationship between a tablespace and a schema. Objects in the same schema
can use storage in different tablespaces, and a tablespace can contain data from different
schemas.
Every object in the database belongs to one schema and has a unique name within that schema.
Multiple database objects can share the same name, if they are in different schemas. You can use
the schema name to unambiguously refer to objects. For example, hr.employees refers to the table
named employees in the hr schema. (The employees table is owned by hr.) The terms database
object and schema object are used interchangeably.
When you create a database object, you must ensure that you create it in the intended schema.
One method is to log in to the database as the user who owns the schema and then create the
object. Generally, you place all the objects that belong to a single application in the same
schema.
You can create and manipulate schema objects with SQL or with SQL Developer.
When creating schema objects using SQL Developer, you can click the DDL tab to display the
SQL statement that is the equivalent of the schema object properties that you specified with the
graphical user interface. SQL Developer submits this SQL statement to create the schema object.
1|Page
This option shows the statement even if it is incomplete, so you must enter all specifications for
the schema object to see the complete SQL statement that SQL Developer submits.
As a database administrator (DBA), you can create, modify, and delete schema objects in your
own schema and in any other schema. For purposes of this discussion, a database administrator is
defined as any user who is granted the DBA role. This includes the SYS and SYSTEM users by
default. Oracle recommends granting the DBA role only to those users who require administrative
type access.
You can enable other users to manage schema objects without necessarily granting them DBA
privileges. For example, a common scenario is to enable an application developer to create,
modify, and delete schema objects in his or her own schema. To do so, you grant
the RESOURCE role to the application developer.
Oracle SQL Developer is a graphical version of SQL*Plus that gives database administrators a
convenient way to perform basic tasks involving schema objects. For example, you can browse,
create, edit, and delete (drop) database schema objects. You can also run SQL statements and
scripts, import and export table data, find invalid schema objects, and view reports.
You can connect to any target Oracle database schema using standard Oracle database
authentication. Once connected, you can perform operations on schema objects.
To install and start SQL Developer, you 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.
When you start SQL Developer, the SQL Developer window appears.
2|Page
The SQL Developer window generally uses the left side for navigation to find and select objects,
and the right side to display information about selected objects.
The menus along the top of the page contain standard entries, plus entries for features specific to
SQL Developer.
The left side of the SQL Developer window has tabs and panes for the Connections and Reports
navigators, icons for performing actions, and a hierarchical tree display for the currently selected
navigator.
In the figure, the HR database connection appears in the Connections navigator, and the schema
objects for the HR schema appear in the metadata tree.
The metadata tree in the Connections navigator displays all the objects (categorized by object
type) accessible to the defined connections. To select an object, expand the appropriate tree node
or nodes, then click the object.
The right side of the SQL Developer window has tabs and panes for objects that you select or
open. For example, the object pane in the figure displays information about a table named
EMPLOYEES. (If you hold the mouse pointer over the tab label -- EMPLOYEES in this figure -
- a tooltip displays the object's owner and the database connection.)
3|Page
2.5.3. Creating a Database Connection Using SQL Developer
A database connection is a SQL Developer object that specifies the necessary information for
connecting to a specific database as a specific user of that database. You must have at least one
database connection (existing, created, or imported) to use SQL Developer.
1. In the Connections navigator in SQL Developer, right-click the Connections node and
select New Connection. The New / Select Database Connection dialog box appears, with
the Oracle tab displayed.
In the Connection Name field, enter the name to use for this database connection.
In the Username field, enter the name of the user for whom this database
connection is being created.
Basic
TNS
LDAP
Advanced
Local/Bequeath
When you choose a connection type, the fields below will change to be
appropriate for the selected connection type. This example describes the fields for
the Basic connection type.
In the Role field, select Default or SYSDBA, based on the role assigned to the user.
In the Hostname field, enter the name of the host where the database is located.
4|Page
In the Port field, enter the port for the database.
In the SID field, enter the SID for the database (when the database connection is
for a non-CDB user or for a multitenant container database (CDB) user):
In the Service name field, enter the service name for the pluggable database
(PDB), including the domain name (when the database connection is for a PDB
user):
5|Page
When a database connection to a PDB is created for an administrative user such
as SYS, SYSDBA is typically specified in the Role field for the connection.
3. Optionally, click Test to test that the data you provided will allow the specified user to
connect to the database.
4. When you are finished, click Connect to connect using the database connection, or
click Save to save the database creation.
Viewing Tables
You can use SQL Developer to list all the tables in a specified schema, and to view the
definitions of individual tables.
To view tables:
1. In the Connections navigator in SQL Developer, navigate to the Tables node for the
schema that includes the table you want to display.
If the view is in your own schema, navigate to the Tables node in your schema.
6|Page
If the table you want to display is in another user's schema, navigate to the Other Users
node, expand it, find the name of the schema the table is in, and navigate to the Tables
node.
Note: You must have the necessary privileges to view other schemas and the objects in
those schemas.
2. Open the Tables node. The list of tables in the schema appears.
A tab with the table name appears in the object pane, with the Columns subtab displayed.
You can view the table definition on this tab.
Besides viewing table names and table definitions in SQL Developer, you can view the data
stored in the table, and the SQL statement used to display the data. You can also change the SQL
statement to alter the results set.
1. In SQL Developer, search for a table as described in "Viewing Tables". For example,
search for the tables in the HR schema.
2. Select the table that contains the data. For example, select countries.
A tab with the table name appears in the object pane, with the Columns subtab displayed.
7|Page
The data for the table appears on the Data subtab.
5. (Optional) Click the SQL subtab to view the SQL statement that defines the table.
You can also write and submit your own SQL SELECT statement to see the contents of a table.
You can run SQL statements by starting a SQL Worksheet session in SQL Developer. To do so,
from the Tools menu, select SQL Worksheet.
In the following example, you create a table called purchase_orders in the HR schema. The table has
the following columns:
8|Page
To create the PURCHASE_ORDERS table in the HR schema:
1. In SQL Developer, navigate to the Tables node in the schema where you want to create
the new table.
The Create Table dialog box appears, with the Table tab displayed.
You can create simple tables quickly using only the default options on the Table tab. If
you want to create more advanced tables, select the Advanced checkbox, which provides
many more options for table creation. Unselect the Advanced checkbox to return to the
default options on the Table tab.
5. In the Columns section, enter column information for each of the columns in
the PURCHASE_ORDERS table as specified in the table in the introduction to this topic. For
example, for the first column in the PURCHASE_ORDERS table, enter the
name PO_NUMBER and the data type NUMBER, select the Not Null check box, and click in
the PK column to indicate that PO_NUMBER is the primary key for the table. Then click
the green plus sign icon to add information for the next column. Continue in this manner
until you have added the information for all of the columns to PURCHASE_ORDERS.
9|Page
6. Click OK.
The PURCHASE_ORDERS table appears under the Tables node in the Connections
navigator.
You can use SQL Developer to add and delete table columns and to manage table
constraints.
In this example, you use SQL Developer to add columns to a table. Specifically, you will add
two new columns named po_date_received and po_requestor_name to the purchase_orders table that you
created previously in "Example: Creating a Table".
1. In SQL Developer, navigate to the Tables node in the HR schema, following the
instructions in "Viewing Tables".
2. Expand the Tables node. The list of tables in the schema appears.
10 | P a g e
The Edit Table dialog box appears.
4. At the top right of the Columns section, click the green plus sign icon.
Name PO_DATE_RECEIVED
Type DATE
6. At the top right of the Columns section, click the green plus sign icon again.
7. In the Column Properties section, enter the following information about the
new po_requestor_name column:
Name PO_REQUESTOR_NAME
Type VARCHAR2
Size 40
11 | P a g e
8. Click OK.
The PURCHASE_ORDERS table appears under the Tables node in the Connections
navigator.
9. To view the new columns, click PURCHASE_ORDERS in the Connections navigator. Then,
on the PURCHASE_ORDERS tab in the objects pane, click the Columns subtab to view the
new columns.
12 | P a g e
3. In the Columns section, click the PO_REQUESTOR_NAME column, and then click the red
X icon.
4. Click OK.
5. On the PURCHASE_ORDERS table tab in the object pane, click the Columns subtab to view
the list of columns in the table.
In this example, you use SQL Developer to add a table constraint to a table. Specifically, you
will add a table constraint to the purchase_orders table that you created in "Example: Creating a
Table". To enforce the rule that the po_date_received value must be either the same day as, or later
than, the value of po_date, you will add a check constraint.
Note: You can also add constraints during table creation, as shown in "Example: Creating a
Table". In that example, you added a primary key constraint.
4. To the right of the Constraints section, click the green plus sign icon and choose New
Check Constraint.
In the Constraints section, a new row appears at the bottom of the list of constraints.
6. Leave the check mark in the Enabled column and leave Not Deferrable.
13 | P a g e
7. In the Check Condition section, enter the following condition for this constraint:
8. Click OK.
9. On the PURCHASE_ORDERS tab in the object pane, click the Constraints subtab to
view the current constraints in the table.
There are a few ways in which you can modify a table constraint. You can change the status of
an existing table constraint, for example, from an enabled state to a disabled state. In this
example, you use SQL Developer to disable the check constraint that you created for
the purchase_orders table in "Example: Adding a New Table Constraint".
14 | P a g e
2. Right-click the PURCHASE_ORDERS table and select Constraint, and then Disable Single.
The Disable Single dialog box appears.
4. Click Apply. A confirmation message appears advising that the constraint has been
disabled.
5. On the PURCHASE_ORDERS tab in the object pane, click the Constraints subtab to
view the PO_CHECK_RCVD_DATE constraint. The Status column for this constraint
shows a value of DISABLED.
You can delete constraints from a table with SQL Developer. Deleting a table constraint may
cause the deletion of other constraints. For example, if you delete the primary key constraint
from a table (the parent table) that is referenced in a foreign key constraint in another table (the
child table), then the foreign key constraint in the child table is also deleted through a cascading
delete mechanism.
In this example, you delete the check constraint that you created for the purchase_orders table
in "Example: Adding a New Table Constraint".
2. Right-click the PURCHASE_ORDERS table and select Constraint, and then Drop.
4. Click Apply.
A confirmation message appears advising that the constraint has been dropped.
15 | P a g e
5. On the PURCHASE_ORDERS tab in the object pane, click the Constraints subtab. The
PO_CHECK_RCVD_DATE constraint no longer appears in this table.
You can use SQL Developer to load data into a table. You can load data from an .xls file or a
.csv file into the table.
In this example, you load data into the PURCHASE_ORDERS table that you created in "Example:
Creating a Table". For simplicity, this example loads only three rows.
To prepare for this example, you must create a text file named load.csv on the file system of the
database host computer or on the file system of your local computer. The contents of the file
should be as follows:
1,Office Equipment,25-MAY-2012,1201,13-JUN-2012
2,Computer System,18-JUN-2012,1201,27-JUN-2012
3,Travel Expense,26-JUN-2012,1340,11-JUL-2012
Note: This example assumes that the columns in the PURCHASE_ORDERS table are the
following: PO_NUMBER, PO_DESCRIPTION, PO_DATE, PO_VENDOR, and PO_DATE_RECEIVED. If
your PURCHASE_ORDERS table does not have all these columns (or has additional columns), then
modify the data in the text file accordingly.
2. Right-click the PURCHASE_ORDERS table and select Import Data. The Open dialog box
appears.
3. Navigate to and select the load.csv file that includes the data you want to import into the
table, and then click Open.
16 | P a g e
The Data Import Wizard appears, with the Data Preview page displayed.
4. Make sure that Header is deselected, Format is set to csv, Line Terminator is set
to environment default, and that Left Enclosure and Right Enclosure are set to none. Then
click Next.
A value in the Import Row Limit field that is greater than the number of rows in
your .csv file.
7. Move the columns that you want to import into the Selected Columns list, and arrange
them in the order you want.
9. Map the source data from the .csv file to the existing table. For each source data column
on the left, select the column on the right to import this data into.
17 | P a g e
For example, in the Source Data Columns table on the left, select the first column, and
then in the Target Table Columns table on the right, select in the Name field the name of
the column in the database table that will store that data. Map the data for each of the
columns in the Source Data Columns table to the appropriate column in the Target Table
Columns table.
The figure shows the data in the last column in the Source Table Columns table being
mapped to the last column (PO_DATE_RECEIVED) in the Target Table Columns table. At
this point, the previous four columns in the Source Table Columns table have already
been mapped to the appropriate columns in the Target Table Columns table.
3. Deleting a Table
If you no longer need a table, then you can delete it using SQL Developer. When you delete a
table, the database deletes the data and dependent objects of the table (such as indexes), and
removes the table from the data dictionary.
18 | P a g e
When you delete a table from a locally managed tablespace that is not the SYSTEM tablespace,
the database does not immediately reclaim the space associated with the table. Instead, it places
the table and any dependent objects in the recycle bin. You can then restore the table, its data,
and its dependent objects from the recycle bin if necessary. You can view the contents of the
recycle bin by clicking Recycle Bin on the Tables page. Note that users can see only tables that
they own in the recycle bin. See Oracle Database Administrator’s Guide for more information
about the recycle bin, including how to view, purge, and recover tables for which you are not the
owner
To delete a table:
2. Right-click the PURCHASE_ORDERS table and select Table and then Drop.
About Indexes
Indexes are optional schema objects that are associated with tables. You create indexes on tables
to improve query performance. Just as the index in a guide helps you to quickly locate specific
information, an Oracle Database index provides quick access to table data.
You can create as many indexes on a table as you need. You create each index on one or more
columns of a table. For example, in a purchase orders table, if you create an index on the vendor
number column, then you can sequentially access the rows of the table in vendor number order,
without having to actually sort the rows. Additionally, you can directly access all purchase orders
issued to a particular vendor without having to scan the entire table.
19 | P a g e
After an index is created, it is automatically maintained and used by the database. Changes to the
data or structure of a table, such as adding new rows, updating rows, or deleting rows, are
automatically incorporated into all relevant indexes. This is transparent to the user.
Some indexes are created implicitly through constraints that are placed on a table. For example,
the database automatically creates an index on the columns of a primary key constraint or unique
key constraint.
Viewing Indexes
To view indexes:
1. In the Connections navigator in SQL Developer, navigate to the Indexes node for the
schema that includes the index you want to view.
If the index is in your own schema, navigate to the Indexes node in your schema.
If the index you want to view is in another user's schema, navigate to the Other Users
node, expand it, find the name of the schema the index is in, and navigate to the Indexes
node.
Note: You must have the necessary privileges to view other schemas and the objects in
those schemas.
The names of the indexes in the selected schema appear below the Indexes node.
A tab with the index name appears in the object pane, with the Columns subtab
displayed. You can view the index definition on this tab.
20 | P a g e
Example: Creating an Index
When you create an index using SQL Developer, you specify one or more table columns to be
indexed and the type of index to create.
In this example, you create an index on the PROD_DESC column in the SH.PRODUCTS table.
(The SH schema is part of the sample schemas.)
1. In SQL Developer, view the tables in the SH schema, by following the instructions
in "Viewing Tables".
2. Right-click the PRODUCTS table, and select Index, and then Create Index.
The Create Index dialog box appears, with the Definition tab displayed.
3. At the top of the dialog box, confirm that SH appears in the Schema field and
that PROD_DESC_IDX appears in the Name field.
Click the green check mark item in the Expressions section, and in the row that
displays, enter PROD_DESC in the Expressions column, and <Not Specified> in
the Order column.
If the index includes multiple columns, choose either ASC (ascending) or DESC
(descending) in the Order field. The value you choose determines the sort order to
be used for the index.
21 | P a g e
5. Click OK to create the index.
If you no longer need an index, then you can delete it using SQL Developer.
In this example, you delete the PROD_DESC_IDX index that you created previously on the
SH.PRODUCTS table in "Example: Creating an Index".
Note: You cannot delete an index that is currently used to enforce a constraint. You must disable
or delete the constraint and then, if the index is not deleted as a result of that action, delete the
index.
1. In SQL Developer, view the tables in the SH schema, by following the instructions
in "Viewing Tables".
2. Right-click the PRODUCTS table, and select Index, and then Drop.
The Drop dialog box appears, with the Prompts tab displayed.
22 | P a g e
3. In the Drop Index field, select PROD_DESC_IDX.
About Views
Views are customized presentations of data in one or more tables or other views. You can think
of them as stored queries. Views do not actually contain data, but instead derive their data from
the tables upon which they are based. These tables are referred to as the base tables of the view.
Similar to tables, views can be queried, updated, inserted into, and deleted from, with some
restrictions. All operations performed on a view actually affect the base tables of the view.
Views can provide an additional level of security by restricting access to a predetermined set of
rows and columns of a table. They can also hide data complexity and store complex queries.
Many important views are in the SYS schema. There are two types: static data dictionary
views and dynamic performance views. Complete descriptions of the views in the SYS schema are
in Oracle Database Reference.
The data dictionary views are called static views because they change infrequently, only when a
change is made to the data dictionary. Examples of data dictionary changes include creating a
new table or granting a privilege to a user.
A DBA_ view displays all relevant information in the entire database. DBA_ views are
intended only for administrators.
An example of a DBA_ view is DBA_TABLESPACES, which contains one row for each tablespace
in the database.
An ALL_ view displays all the information accessible to the current user, including
information from the schema of the current user, and information from objects in other
schemas, if the current user has access to those objects through privileges or roles.
23 | P a g e
An example of an ALL_ view is ALL_TABLES, which contains one row for every table for which
the user has object privileges.
A USER_ view displays all the information from the schema of the current user. No
special privileges are required to query these views.
An example of a USER_ view is USER_TABLES, which contains one row for every table owned by
the user.
The columns in the DBA_, ALL_, and USER_ views are usually nearly identical. The USER_ view
usually does not have an OWNER column.
Dynamic performance views monitor ongoing database activity. They are available only to
administrators. The names of dynamic performance views start with the characters V$. For this
reason, these views are often referred to as V$ views.
An example of a V$ view is V$SGA, which returns the current sizes of various System Global
Area (SGA) memory components.
Displaying Views
You can use SQL Developer to list the views in a specified schema. You can also display the
view definitions.
To display views:
1. In the Connections navigator in SQL Developer, navigate to the Views node for the
schema that includes the view you want to display.
If the view is in your own schema, navigate to the Views node in your schema.
If the view you want to display is in another user's schema, navigate to the Other Users
node, expand it, find the name of the schema the view is in, and navigate to the Views
node.
24 | P a g e
Note:You must have the necessary privileges to view other schemas and the objects in
those schemas.
2. Open the Views node. The list of views in the schema appears.
A tab with the view name appears in the object pane, with the Columns subtab displayed.
You can view the view definition on this tab.
In this example, you use SQL Developer to create a view named king_view, which uses
the HR.EMPLOYEES table as its base table. (The HR schema is part of the sample schemas.) This
view filters the table data so that only employees who report directly to the manager King, whose
employee ID is 100, are returned in queries. In an application scenario, this view adds an
additional level of security to the HR.EMPLOYEES table while providing a suitable presentation of
relevant information for manager King.
1. In the Connections navigator in SQL Developer, navigate to the Views node in the HR
schema, by following the instructions in "Displaying Views".
The Create View dialog box appears, with the SQL Query tab displayed.
In the SQL Query field, enter the following SQL statement that will be used to
create KING_VIEW:
25 | P a g e
4. Click OK. The KING_VIEW is created and appears in the list of views for the HR schema.
1. In the Connections navigator in SQL Developer, navigate to the Views node in the HR
schema and find the KING_VIEW, by following the instructions in "Displaying Views".
2. Click the KING_VIEW. A tab with the view name appears in the object pane, with the
Columns subtab displayed.
3. Click the Data subtab in the object pane. The data selected by the view appears.
4. (Optional) You can also test the view by submitting the following SQL statement in
SQL*Plus or SQL Developer:
If you no longer need a view, then you can delete it using SQL Developer.
26 | P a g e
In this example, you delete the HR.KING_VIEW view that you created previously in "Example:
Creating a View".
1. In the Connections navigator in SQL Developer, navigate to the Views node in the HR
schema and find the KING_VIEW, by following the instructions in "Displaying Views".
2. Right-click the KING_VIEW, and then select Drop. The Drop dialog box appears.
Without sequences, sequential values can only be produced programmatically. A new primary
key value can be obtained by selecting the most recently produced value and incrementing it.
This method requires a lock during the transaction and causes multiple users to wait for the next
value of the primary key; this waiting is known as serialization
If you want to make your PK auto increment, you need to set the ID column property for
that primary key.
27 | P a g e
Synonym
A synonym is an alias for any schema object such as a table or view. Synonyms provide an easy
way to hide the underlying database structure from an application or a user. For example, you
can create a synonym named personnel as an alias for the employees table in the hr schema.
If you use SQL Developer, you can view the newly created synonym in under the Synonym
nodes as shown in the following picture:
28 | P a g e
29 | P a g e