| Oracle® Database Express Edition 2 Day Developer's Guide 11g Release 2 (11.2) Part Number E18147-07 |
|
|
PDF · Mobi · ePub |
This chapter contains the following topics:
Deployment is installing your application in one or more environments where other users can run it.
The schema in which you develop your application is called the development environment. (The development environment for the sample application is the sample schema HR.) The other environments in which you deploy your application are called deployment environments. These environments might exist in your organization; if not, you can create them.
The first deployment environment is the test environment. In the test environment, you can thoroughly test the functionality of the application, determine whether it is packaged correctly, and fix any problems before deploying it in the production environment.
You might also deploy your application to an education environment, either before or after deploying it to the production environment. An education environment provides a place for users to practice running the application without affecting other environments.
To deploy an application, you run one or more installation script files. If these files do not exist, you can create them, with SQL Developer or any text editor.
An installation script file is an executable file (.sql file) that contains an installation script. An installation script is composed ot DDL statements and (optionally) INSERT statements. When you run an installation script file, the DDL statements create the schema objects of your application in the deployment environment, and the INSERT statements insert the data from the tables in your development environment (the source tables) into the corresponding tables in the deployment environment (the new tables).
Topics:
When you run an installation script file, its DDL statements create the schema objects of your application in the deployment environment. To create installation script files correctly, and to run multiple installation script files in the correct order, you must understand the dependencies between the schema objects of your application.
If the definition of object A references object B, then A depends on B. Therefore, you must create B before you create A. Otherwise, the statement that creates B either fails or creates B in an invalid state, depending on the object type.
Typically, you install schema objects and data in the deployment environment in this order:
Package specifications
Tables (with constraints and indexes) in correct order
Sequences (often used by triggers)
Triggers
Synonyms
Views (which might reference functions, procedures, or synonyms)
Package bodies
Data
However, for a complex application, the order for creating the objects is rarely obvious. Usually, you must consult the database designer or a diagram of the design.
See Also:
Oracle Database Advanced Application Developer's Guide for more information about schema object dependencies
When you run an installation script file that contains INSERT statements, the INSERT statements insert the data from the source tables into the corresponding new tables. For each source table in your application, you must determine whether any constraints could be violated when their data is inserted in the new table. If so, you must first disable those constraints, then insert the data, and then try to re-enable the constraints. If a data item violates a constraint, you cannot re-enable that constraint until you correct the data item.
If you are simply inserting lookup data in correct order, constraints are not violated. Therefore, you do not need to disable them first.
If you are inserting data from an outside source (such as a file, spreadsheet, or older application), or from many tables that have much dependent data, disable the constraints before inserting the data.
Some possible ways to disable and re-enable the constraints are:
Using SQL Developer, disable and re-enable the constraints one at a time.
Edit the installation script file, adding SQL statements that disable and re-enable each constraint.
Create a SQL script with SQL statements that disable and enable each constraint.
Find the constraints in the Oracle Database Express Edition (Oracle Database XE) data dictionary, and create a SQL script with the SQL statements to disable and enable each constraint.
To find and enable the constraints used in the EVALUATIONS, PERFORMANCE_PARTS, and SCORES tables, enter these statements into a SQL Worksheet window:
SELECT 'ALTER TABLE '|| TABLE_NAME || ' DISABLE CONSTRAINT '||
CONSTRAINT_NAME ||';'
FROM user_constraints
WHERE table_name IN ('EVALUATIONS','PERFORMANCE_PARTS','SCORES');
SELECT 'ALTER TABLE '|| TABLE_NAME || ' ENABLE CONSTRAINT '||
CONSTRAINT_NAME ||';'
FROM user_constraints
WHERE table_name IN ('EVALUATIONS','PERFORMANCE_PARTS','SCORES');
This topic explains how to use SQL Developer to create installation script files, when and how to edit installation script files that create sequences and triggers, and how to create installation script files for the schema objects and data of the sample application.
The tutorials in this topic assume that you created the objects in the sample schema HR, using the instructions in this document, and are deploying the sample application in another standard HR schema.
Note:
To do the tutorials in this document, you must be connected to Oracle Database XE as the userHR from SQL Developer.Topics:
To create an installation script file with SQL Developer, use the Database Export tool. You specify the name of the installation script file and the objects and data to export, and SQL Developer generates DDL statements for creating the objects and INSERT statements for inserting the data into new tables, and writes these statements to the installation script file.
Note:
In the following procedure, you might have to enlarge the SQL Developer windows to see all fields and options.To create an installation script file with the Database Export tool:
If you have not done so, create a directory for the installation script file, separate from the Oracle Database XE installation directory (for example, C:\my_exports).
In the SQL Developer window, click the menu Tools.
A menu appears.
From menu, select Database Export.
The Source/Destination window opens.
In the Source/Destination window:
In the Connection field, select from the menu your connection to the development environment.
Select the desired Export DDL options (and deselect any selected undesired options).
Note:
Do not deselect Terminator, or the installation script file will fail.For descriptions of the Export DDL Options, see Oracle Database SQL Developer User's Guide.
If you do not want the installation script to export data, deselect Export Data.
In the Save As field, accept the default Single File and type the full path name of the installation script file (for example, C:\my_exports\hr_export.sql).
The file name must end with .sql.
The Types to Export window appears, listing the types of objects that you can export. To the left of each object type is a check box. By default, every check box is selected.
In the Types to Export window:
Deselect the check boxes for the types that you do not want to export.
Selecting or deselecting Toggle All selects or deselects all check boxes.
Click Next.
The Specify Objects window appears.
In the Specify Objects window:
Click More.
More fields appear, including the Schema and Type fields.
In the Schema field, select your schema from the menu.
In the Type field, select from the menu either ALL OBJECTS or a specific object type (for example, TABLE).
Click Lookup.
A list of objects appears in the left frame. If the value of the Type field is ALL OBJECTS, then the list contains all objects in the selected schema. If the value of the Type field is a specific object type, then the list contains all objects of that type in the selected schema.
Move the objects that you want to export from the left frame to the right frame:
To move all objects, click >>. (To move all objects back, click <<.)
To move selected objects, select them and then click >. (To move selected objects back, select them and click <.)
Click Next.
If you deselected Export Data in the Source/Destination window, then the Export Summary window appears—go to step 3.
If you did not deselect Export Data in the Source/Destination window, then the Specify Data window appears. The lower frame lists the objects that you specified in the Specify Objects window.
In the Specify Data window:
Move the objects whose data you do not want to export from the lower frame to the upper frame:
To move all objects, click the double upward arrow icon. (To move all objects back, click the double downward arrow icon.)
To move selected objects, select them and then click the single upward arrow icon.
Click Next.
The Export Summary window appears.
In the Export Summary window, click Finish.
The Exporting window opens, showing that exporting is occurring. When exporting is complete, the Exporting window closes, and the SQL Worksheet shows the contents of the installation script file that you specified in the Source/Destination window.
In the installation script file, check that:
Referenced objects are created before their dependent objects.
Tables are created before data is inserted into them.
If necessary, edit the file in the SQL Worksheet or any text editor.
See Also:
Oracle Database SQL Developer User's Guide for more information about SQL Developer dialog boxes
Oracle Database Advanced Application Developer's Guide for information about schema object dependencies
"Tutorial: Creating an Installation Script File for the Sample Application"
For a sequence, SQL Developer generates a CREATE SEQUENCE statement whose START WITH value is relative to the current value of the sequence in the development environment.
If your application uses the sequence to generate unique keys, and you will not insert the data from the source tables into the corresponding new tables, then you might want to edit the START WITH value in the installation script file. You can edit the installation script file in the SQL Worksheet or any text editor.
See Also:
"Tutorial: Creating a Sequence"If your application has a BEFORE INSERT trigger on a source table, and you will insert the data from that source table into the corresponding new table, then you must decide if you want the trigger to fire before each INSERT statement in the installation script file inserts data into the new table.
For example, in the sample application, NEW_EVALUATION_TRIGGER fires before a row is inserted into the EVALUATIONS table, and generates the unique number for the primary key of that row, using EVALUATIONS_SEQ. The trigger fires once for each row affected by the triggering INSERT statement.
The source EVALUATIONS table is populated with primary keys. If you do not want the installation script to put new primary key values in the new EVALUATIONS table, then you must edit the CREATE TRIGGER statement in the installation script file as shown in bold:
CREATE OR REPLACE TRIGGER NEW_EVALUATION_TRIGGER BEFORE INSERT ON EVALUATIONS FOR EACH ROW BEGIN IF :NEW.evaluation_id IS NULL THEN :NEW.evaluation_id := evaluations_seq.NEXTVAL END IF; END;
Also, check the current value of the sequence. If it not is greater than the maximum value in the primary key column, make it greater.
You can edit the installation script file in the SQL Worksheet or any text editor.
Two alternatives to editing the installation script file are:
Change the trigger definition in the source file and then re-create the installation script file.
For information about changing triggers, see "Changing Triggers".
Disable the trigger before running the data installation script file, and then re-enable it afterward.
For information about disabling and enabling triggers, see "Disabling and Enabling Triggers".
See Also:
"Creating Triggers"This tutorial shows how to use the SQL Developer tool Database Export to create a single installation script file for the sample application.
Note:
In the following procedure, you might have to enlarge the SQL Developer windows to see all fields and options.To create an installation script file for the sample application:
If you have not done so, create a directory for the installation script file, separate from the Oracle Database XE installation directory (for example, C:\my_exports).
In the SQL Developer window, click the menu Tools.
A menu appears.
From menu, select Database Export.
The Source/Destination window opens.
In the Source/Destination window:
In the Connection field, select hr_conn from the menu.
Accept the default Export DDL options.
Accept the default Export Data option (selected).
In the Save As field, accept the default Single File and type the full path name of the installation script file: C:\my_exports\hr_export.sql.
Click Next.
The Types to Export window appears. All types are selected.
In the Types to Export window:
Deselect the check boxes for all types except those that the sample application uses, which are: Tables, Views, Indexes, Triggers, Constraints, Sequences, Synonyms, Package Spec, and Package Body.
Click Next.
The Specify Objects window appears.
In the Specify Objects window:
Click More.
More fields appear, including the Schema and Type fields.
In the Schema field, select HR from the menu.
In the Type field, select TABLE from the menu.
Click Lookup.
The names of the tables in the HR schema appear in the left frame.
In the left frame, select the names of the tables that you created for the sample application—EVALUATIONS, EVALUATIONS_LOG, PERFORMANCE_PARTS, and SCORES.
Click >>.
The selected tables move to the right frame.
In the Type field, select SEQUENCE from the menu.
Click Lookup.
The names of the sequences in the HR schema appear in the left frame.
In the left frame, select the name of the sequence that you created for the sample application—EVALUATIONS_SEQ.
Click >>.
The selected sequence moves to the right frame.
In the Type field, select TRIGGER from the menu.
Click Lookup.
The names of the triggers in the HR schema appear in the left frame.
In the left frame, select the names of the triggers that you created for the sample application—EVAL_CHANGE_TRIGGER and NEW_EVALUATION_TRIGGER.
Click >>.
The selected triggers move to the right frame.
In the Type field, select PACKAGE from the menu.
Click Lookup.
The names of the packages in the HR schema appear in the left frame.
In the left frame, select the name of the package that you created for the sample application—EMP_EVAL.
Click >>.
The selected package moves to the right frame, where it appears as HR.EMP_EVAL.
In the Type field, select PACKAGE BODY from the menu.
Click Lookup.
The names of the package bodies in the HR schema appear in the left frame.
In the left frame, select EMP_EVAL Body.
Click >>.
The selected package body moves to the right frame, where it appears as HR.EMP_EVAL.
In the Type field, select SYNONYM from the menu.
Click Lookup.
The names of the synonyms in the HR schema appear in the left frame.
In the left frame, select the name of the synonym that you created for the sample application—EMP.
Click >>.
The selected synonym moves to the right frame.
In the Type field, select VIEW from the menu.
Click Lookup.
The names of the views in the HR schema appear in the left frame.
In the left frame, select the name of the view that you created for the sample application—EMP_LOCATIONS.
Click >>.
The selected view moves to the right frame.
Click Next.
The Specify Data window appears. The lower frame lists the objects that you specified in the Specify Objects window.
In the Specify Data window:
In the lower frame, select every object except PERFORMANCE_PARTS (the only sample application table to which you added data).
Click the upward arrow icon.
The selected objects move from the lower frame to the upper frame, indicating that their data is not to be exported.
Click Next.
The Export Summary window appears.
In the Export Summary window, click Finish.
The Exporting window opens, showing that exporting is occurring. When exporting is complete, the Exporting window closes, and the SQL Worksheet shows the contents of the installation script file C:\my_exports\hr_export.sql.
In the installation script file, check that:
Referenced objects are created before their dependent objects.
Tables are created before data is inserted into them.
If necessary, edit the file in the SQL Worksheet or any text editor.
Note:
The deployment environment must be different from the development environment, and is assumed to be another standardHR schema.If you created a single installation script file for the sample application, you can install the sample application by connecting to the deployment environment as the user HR and then running the installation script in either SQL*Plus or in the SQL Worksheet of SQL Developer. The command for running the installation script has this syntax:
@full_path_name_of_installation_script_file
To run an installation script file in SQL Developer:
If necessary, create a connection to the deployment environment.
For Connection Name, type a name that is not the name of the connection to the development environment.
Connect to Oracle Database XE as user HR in the deployment environment.
For Connection Name, enter the name of the connection to the deployment environment.
A new pane appears. On its tab is the name of the connection to the deployment environment. The pane has two subpanes, SQL Worksheet and Query Builder.
In the SQL Worksheet pane, type the command for running the installation script:
@full_path_name_of_installation_script_file
For example:
@C:\my_exports\hr_export.sql
Click the icon Run Script.
The script runs. Its output appears in the Script Output pane, under the SQL Worksheet pane.
In the Connections pane, if you expand the connection to the deployment environment, and then expand the type of each object that the sample application uses, you see the objects of the sample application.
See Also:
Oracle Database SQL Developer User's Guide for more information about running scripts in SQL Developer
SQL*Plus User's Guide and Reference for more information about using scripts in SQL*Plus
After installing your application in a deployment environment, you can check its validity in the following ways in SQL Developer:
In the Connections pane:
Expand the connection to the deployment environment.
Examine the definitions of the new objects.
In the Reports pane:
Expand Data Dictionary Reports.
A list of data dictionary reports appears.
Expand All Objects.
A list of objects reports appears.
Select All Objects.
The Select Connection window appears.
In the Connection field, select from the menu the connection to the deployment environment.
Click the icon OK.
The Enter Bind Values window appears.
Select either Owner or Object.
Click Apply.
The message "Executing Report" shows, followed by the report itself.
For each object, this report lists the Owner, Object Type, Object Name, Status (Valid or Invalid), Date Created, and Last DDL. Last DDL is the date of the last DDL operation that affected the object.
In the Reports pane (next to the Connections pane), select Invalid Objects.
The Enter Bind Values window appears.
Click Apply.
For each object whose Status is Invalid, this report lists the Owner, Object Type, and Object Name.
See Also:
Oracle Database SQL Developer User's Guide for more information about SQL Developer reportsAfter verifying that the installation of your application is valid, you might want to archive your installation script file or files in a source code control system. Before doing so, add comments to each file, documenting its creation date and purpose. If you ever must deploy the same application to another environment, you can use these archived files.
See Also:
Oracle Database Utilities for information about Oracle Data Pump, which enables very high-speed movement of data and metadata from one database to another