managing schema objects -...
TRANSCRIPT
2017530 Managing Schema Objects
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/12c/r1/2day_dba/12cr1db_ch8schema/12cr1db_ch8schema.html 1/29
Purpose
In this tutorial, you learn how to use SQL Devevloper to manage tables, indexes, and views. You also learn how tomanage program code that is stored in the database.
Time to Complete
Approximately 1 hour
Introduction
A schema is a collection of database objects. A schema is owned by a database user and shares the same name asthe 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.
Software Requirements
The following is a list of hardware and software requirements:
Oracle Database 12c
Prerequisites
Before starting this tutorial, you should:
Install the Sample SchemasComplete the Administering User Accounts and Security tutorial
A database connection is a SQL Developer object that specifies the necessary information for connecting to a specificdatabase, as a specific user of that database. You must have at least one database connection (existing, created, orimported) to use SQL Developer. To create a database connection , perform the following steps:
1. In a terminal window, set the environment variable. Change directories to the sqldeveloper directory under$ORACLE_HOME. Invoke SQL Developer by executing the sh sqldeveloper.sh command.
2. In the Connections navigator, rightclick the Connections node and select New Connection.
Overview
Creating a Database Connection in SQL Developer
Managing Schema Objects
2017530 Managing Schema Objects
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/12c/r1/2day_dba/12cr1db_ch8schema/12cr1db_ch8schema.html 2/29
2. In the Connections navigator, rightclick the Connections node and select New Connection.
3. Enter a connection name of your choice, username of system and password for the SYSTEM user. Select"Save Password" if you want to save your password for future connections as this user. Accept the defaultconnection type and role. Enter the hostname, port, and SID. You can click Test to ensure that the connectionworks correctly.
4. Click Connect.
2017530 Managing Schema Objects
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/12c/r1/2day_dba/12cr1db_ch8schema/12cr1db_ch8schema.html 3/29
5. Your connection is displayed in the Connections tab on the left side and a SQL worksheet is openedautomatically.
In this topic, you view table definitions and table data. You also create a new table and modify it.
Viewing Tables
1. Expand the system node in Oracle SQL Developer.
Managing Tables
2017530 Managing Schema Objects
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/12c/r1/2day_dba/12cr1db_ch8schema/12cr1db_ch8schema.html 4/29
2. Expand the Other Users node and then expand the HR node.
2017530 Managing Schema Objects
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/12c/r1/2day_dba/12cr1db_ch8schema/12cr1db_ch8schema.html 5/29
3. Expand the Tables (Filtered) node.
2017530 Managing Schema Objects
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/12c/r1/2day_dba/12cr1db_ch8schema/12cr1db_ch8schema.html 6/29
4. Select the EMPLOYEES table. Detailed information about the table is displayed in the object pane. TheColumns tab displays the column names and definitions.
Viewing Table Data
1. Select the EMPLOYEES table, click the Data tab to view the data stored in the table.
2017530 Managing Schema Objects
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/12c/r1/2day_dba/12cr1db_ch8schema/12cr1db_ch8schema.html 7/29
2. The Data tab shows the rows stored in the EMPLOYEES table.
3. To sort the rows by last name, rightclick the LAST_NAME column name and select Sort in the menu.
4. Select the LAST_NAME column and click the rightarrow to move it to the Selected Columns list. Click OK.
2017530 Managing Schema Objects
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/12c/r1/2day_dba/12cr1db_ch8schema/12cr1db_ch8schema.html 8/29
5. The data is now displayed in sorted order.
Creating a Table
1. In this topic you create a new table in the APPUSER schema. You created the APPUSER schema in theAdministering User Accounts and Security User tutorial. Expand the APPUSER node in Oracle SQL Developer.
2017530 Managing Schema Objects
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/12c/r1/2day_dba/12cr1db_ch8schema/12cr1db_ch8schema.html 9/29
2. Rightclick the Tables node and select New Table.
3. Enter purchase_orders in the Name field. Enter po_number in the Column Name field. Select NUMBERas the Type. Select "Not Null" and "Primary Key." Click Add Column.
4. Enter po_description in the Column Name field. Select VARCHAR2 as the type. Enter 200 in the Sizefield. Click Add Column.
2017530 Managing Schema Objects
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/12c/r1/2day_dba/12cr1db_ch8schema/12cr1db_ch8schema.html 10/29
5. Enter po_date in the Column Name field. Select DATE for the type and select the Not Null column. Click AddColumn.
6. Enter po_vendor in the Column Name field. Select NUMBER as the type and select the Not Null column.Click OK.
2017530 Managing Schema Objects
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/12c/r1/2day_dba/12cr1db_ch8schema/12cr1db_ch8schema.html 11/29
7. The PURCHASE_ORDERS table appears in the Tables list for the APPUSER user.
8. Select the PURCHASE_ORDERS table. Click the Columns tab to view the column definitions.
Adding Columns to a Table
1. Rightclick the PURCHASE_ORDERS table and select Edit.
2017530 Managing Schema Objects
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/12c/r1/2day_dba/12cr1db_ch8schema/12cr1db_ch8schema.html 12/29
2. The Edit Table dialog box appears. Click the green plus sign to add a column.
3. Enter po_date_received in the Name field. Select DATE in the Type menu. Click the green plus signagain.
4. Enter po_requestor_name in the Name field. Select VARCHAR2 in the Type menu. Enter 40 in the Sizefield. Click OK.
2017530 Managing Schema Objects
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/12c/r1/2day_dba/12cr1db_ch8schema/12cr1db_ch8schema.html 13/29
5. The Columns tab shows the new columns.
Loading Data Into a Table
1. Download the load_po.csv file.
2. Rightclick the APPUSER user and select Edit User.
3. On the Create/Edit User window, click Quotas.
2017530 Managing Schema Objects
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/12c/r1/2day_dba/12cr1db_ch8schema/12cr1db_ch8schema.html 14/29
4. Select Unlimited for the APPTS tablespace and click Apply.
5. Click Close.
2017530 Managing Schema Objects
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/12c/r1/2day_dba/12cr1db_ch8schema/12cr1db_ch8schema.html 15/29
6. Expand Tables. Rightclick the PURCHASE_ORDERS table and select Import Data.
7. Select the load_po.csv file. Click Open.
8. Deselect Header. Ensure that Format is set to csv. Select UTF8 in the Encoding menu. Select none for LeftEnclosure. Click Next.
2017530 Managing Schema Objects
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/12c/r1/2day_dba/12cr1db_ch8schema/12cr1db_ch8schema.html 16/29
9. Ensure that Import Method is set to Insert. Click Next.
10. Click Next.
2017530 Managing Schema Objects
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/12c/r1/2day_dba/12cr1db_ch8schema/12cr1db_ch8schema.html 17/29
11. Verify each column in the Source Data Columns list and its value in the Name field BEFORE clicking Next. Afterverifying all five columns, click Next.
12. Click Finish.
2017530 Managing Schema Objects
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/12c/r1/2day_dba/12cr1db_ch8schema/12cr1db_ch8schema.html 18/29
12. Click Finish.
13. The data was successfully imported. Click OK.
14. Select the PURCHASE_ORDERS table and click the Data tab to see the new rows.
In this topic you view index definitions. You also create a new index.
Viewing Indexes
Managing Indexes
2017530 Managing Schema Objects
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/12c/r1/2day_dba/12cr1db_ch8schema/12cr1db_ch8schema.html 19/29
1. Expand the HR user in the Other Users node. Expand Indexes.
2. Select EMP_DEPARTMENT_IX.
3. Information about the index definition is displayed in the Columns tab. Click the Details tab to view additionalinformation.
4. The Details tab shows additional information about the index definition.
Creating an Index
1. Select the SH user in the Other Users list and expand the Tables entry.
2017530 Managing Schema Objects
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/12c/r1/2day_dba/12cr1db_ch8schema/12cr1db_ch8schema.html 20/29
2. Rightclick the Products table. Select Index in the menu and then select Create Index.
3. Enter prod_desc_idx in the Name field. Select PROD_DESC in the "Column Name or Expression" field.Click OK.
2017530 Managing Schema Objects
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/12c/r1/2day_dba/12cr1db_ch8schema/12cr1db_ch8schema.html 21/29
4. Ensure that PRODUCTS is selected in the left hand navigator pane.
5. Select the Indexes tab on the right hand side. Your new PROD_DESC_IDX index is listed.
In this topic, you display view definitions. You also create a new view.
Displaying Views
1. Expand HR under Other Users in the left pane. Expand Views.
Managing Views
2017530 Managing Schema Objects
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/12c/r1/2day_dba/12cr1db_ch8schema/12cr1db_ch8schema.html 22/29
2. Select the EMP_DETAILS_VIEW view.
3. The Columns tab in the right pane displays the columns that are part of this view.
Creating a View
In this example, you create a view named KING_VIEW, which queries the HR.EMPLOYEES table. This view filters thetable data so that only employees who report directly to the manager named King, whose employee ID is 100, arereturned in queries.
1. Ensure the HR user is selected. Right click Views and select New View.
2. Enter king_view in the Name field. Enter SELECT * FROM hr.employees WHERE manager_id =100 in the SQL Query box. Click OK.
2017530 Managing Schema Objects
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/12c/r1/2day_dba/12cr1db_ch8schema/12cr1db_ch8schema.html 23/29
3. The new view is listed under Views in the left pane.
4. Select the KING_VIEW view.
5. The columns that are included in the view are displayed in the Columns tab. Click the Data tab.
2017530 Managing Schema Objects
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/12c/r1/2day_dba/12cr1db_ch8schema/12cr1db_ch8schema.html 24/29
6. The rows that are retrieved when the view is queried are displayed in the Data tab.
As a database administrator (DBA), you may be asked to revalidate schema objects that have become invalid. Schemaobjects (such as triggers, procedures, or views) might be invalidated when changes are made to objects on which theydepend. In this section you will create a new PL/SQL procedure. You will make a change to the table that is referencedin the procedure. This change will invalidate the procedure. You will then recompile the procedure.
Validating (Compiling) Invalid Schema Objects
1. Download the cr_add_po.sql file.
2. Log in to SQL*Plus as the SYSTEM user.
3. Execute the cr_add_po.sql script to create a new PL/SQL procedure namedAPPUSER.ADD_PO_HISTORY. Exit from SQL*Plus.
Managing Program Code Stored in the Database
2017530 Managing Schema Objects
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/12c/r1/2day_dba/12cr1db_ch8schema/12cr1db_ch8schema.html 25/29
4. Return to SQL Developer. Expand Procedures for the APPUSER user. Select your new ADD_PO_HISTORYprocedure to view it in SQL Developer.
5. Expand Tables for the APPUSER user. Rightclick the PURCHASE_ORDERS table and select Edit.
6. Select the PO_DESCRIPTION column. Change the value of Size to 250. Click OK.
2017530 Managing Schema Objects
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/12c/r1/2day_dba/12cr1db_ch8schema/12cr1db_ch8schema.html 26/29
6. Select the PO_DESCRIPTION column. Change the value of Size to 250. Click OK.
7. Select the Reports tab. Expand Data Dictionary Reports. Expand All Objects and select Invalid Objects.
8. In the Select Connection dialog box, click the green plus sign to create a new connection.
2017530 Managing Schema Objects
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/12c/r1/2day_dba/12cr1db_ch8schema/12cr1db_ch8schema.html 27/29
9. Enter appuser in the Connection Name field. Enter appuser in the Username field. Enter the password forappuser. Enter orcl in the SID field. Click Connect.
10. Click OK.
11. Click Apply in the Enter Bind Values window.
2017530 Managing Schema Objects
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/12c/r1/2day_dba/12cr1db_ch8schema/12cr1db_ch8schema.html 28/29
12. The Invalid Objects tab appears in the object pane. Note that the ADD_PO_HISTORY procedure is now invalid.
13. Select the ADD_PO_HISTORY procedure. Rightclick and select Compile.
14. Click Apply.
15. Click OK.
16. Close the Invalid Objects tab. Select Invalid Objects again in the left pane. Connect as the APPUSER andreselect Invalid Objects. Because you recompiled ADD_PO_HISTORY, there are no longer any invalid objects.
2017530 Managing Schema Objects
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/12c/r1/2day_dba/12cr1db_ch8schema/12cr1db_ch8schema.html 29/29
In this tutorial, you learned to:
Create a database connection in SQL DeveloperManage tablesManage indexesManage viewsManage stored program code
Resources
Oracle Database 2 Day DBA 12c Release 2 (12.1)
Credits
Lead Curriculum Developer: Veerabhadra Rao PutrevuOriginal Curriculum Developer: Donna Keesling, Salome ClementOther Contributors: Bert Rich
Summary