Tuesday, 25 October 2011

External Tables

External tables allow Oracle to query data that is stored outside the database in flat files. The ORACLE_LOADER driver can be used to access any data stored in any format that can be loaded by SQL*Loader. No DML can be performed on external tables but they can be used for query, join and sort operations. Views and synonyms can be created against external tables. They are useful in the ETL process of data warehouses since the data doesn't need to be staged and can be queried in parallel. They should not be used for frequently queried tables.

Limitations on external tables

Because external tables are new, Oracle has not yet perfected their use. In Oracle9i the feature has several limitations, including:
• No support for DML. External tables are read-only, but the base data can be edited in any text editor.
• Poor response for high-volume queries. External tables have a processing overhead and are not suitable for large tables.
Example: The example below describes how to create external files, create external tables, query external tables and create views.
Step I: Creating the flat files, which will be queried
The file "emp_ext1.dat" contains the following sample data:
101,Andy,FINANCE,15-DEC-1995
102,Jack,HRD,01-MAY-1996
103,Rob,DEVELOPMENT,01-JUN-1996
104,Joe,DEVELOPMENT,01-JUN-1996
The file "emp_ext2.dat" contains the following sample data:
105,Maggie,FINANCE,15-DEC-1997
106,Russell,HRD,01-MAY-1998
107,Katie,DEVELOPMENT,01-JUN-1998
108,Jay,DEVELOPMENT,01-JUN-1998

Copy these files under "C:\EXT_TABLES"
Step II: Create a Directory Object where the flat files will reside
SQL> CREATE OR REPLACE DIRECTORY EXT_TABLES AS 'C:\EXT_TABLES';

Directory created.
Step III: Create metadata for the external table
SQL> CREATE TABLE emp_ext
(
empcode NUMBER(4),
empname VARCHAR2(25),
deptname VARCHAR2(25),
hiredate date
)
ORGANIZATION EXTERNAL
(
TYPE ORACLE_LOADER
DEFAULT DIRECTORY ext_tables
ACCESS PARAMETERS
(
RECORDS DELIMITED BY NEWLINE
FIELDS TERMINATED BY ','
MISSING FIELD VALUES ARE NULL
)
LOCATION ('emp_ext1.dat','emp_ext2.dat')
)
REJECT LIMIT UNLIMITED;

Table created.

The REJECT LIMIT clause specifies that there is no limit on the number of errors that can occur during a query of the external data.
Step V: Creating Views
SQL> CREATE VIEW v_empext_dev AS
SELECT * FROM emp_ext
WHERE deptname = 'DEVELOPMENT';
View created.

Dropping External Tables
For an external table, the DROP TABLE statement removes only the table metadata in the database. It has no affect on the actual data, which resides outside of the database.

Canvas Level Properties

Primary Canvas property
Specifies the canvas that is to be the window's primary content view. At runtime, Form Builder always attempts to display the primary view in the window. For example, when you display a window for the first time during a session by executing the SHOW_WINDOW built-in procedure, Form Builder displays the window with its primary content view.
If, however, Form Builder needs to display a different content view because of navigation to an item on that view, the primary content view is superseded by the target view.


Window Style property
Specifies whether the window is a Document window or a Dialog window. Document and dialog windows are displayed differently on window managers that support a Multiple Document Interface (MDI) system of window management.
MDI applications display a default parent window, called the application window. All other windows in the application are either document windows or dialog windows.
Document windows always remain within the application window frame. If the operator resizes the application window so that it is smaller than a document window, the document window is clipped. An operator can maximize a document window so that is occupies the entire workspace of the application window.
Dialog windows are free-floating, and the operator can move them outside the application window if they were defined as Movable. If the operator resizes the application window so that it is smaller than a dialog window, the dialog window is not clipped.

Modal property
Specifies whether a window is to be modal. Modal windows require an end user to dismiss the window before any other user interaction can continue.

Hide on Exit property
For a modeless window, determines whether Form Builder hides the window automatically when the end user navigates to an item in another window.

Close Allowed property
Specifies whether the window manager-specific Close command is enabled or disabled for a window.
? Setting Close Allowed to Yes enables the Close command so that the Close Window event can be sent to Form Builder when the operator issues the Close command. However, to actually close the window in response to this event, you must write a When-Window-Closed trigger that explicitly closes the window. You can close a window programmatically by calling HIDE_WINDOW, SET_WINDOW_PROPERTY, or EXIT_FORM.
? On Microsoft Windows, if the operator closes the MDI parent window, Form Builder executes DO_KEY('Exit_Form') by default.


Move Allowed property
Specifies whether or not the window can be moved. Windows can be moved from one location to another on the screen by the end user or programmatically by way of the appropriate built-in subprogram.


Resize Allowed property
Specifies that the window is to be a fixed size and cannot be resized at runtime. The Resize Allowed property prevents an end user from resizing the window, but it does not prevent you from resizing the window programmatically with RESIZE_WINDOW or SET_WINDOW_PROPERTY.

Maximize Allowed property
Specifies that end users can resize the window by using the zooming capabilities provided by the runtime window manager.

Minimize Allowed property
Specifies that a window can be iconified on window managers that support this feature.

Minimized Title property
Specifies the text string that should appear below an iconified window.


Icon Filename property
Specifies the name of the icon resource that you want to represent the iconic button, menu item, or window.


Icon Filename property
Specifies the name of the icon resource that you want to represent the iconic button, menu item, or window.


Canvas Type property
Specifies the type of canvas. The type determines how the canvas is displayed in the window to which it is assigned, and determines which properties make sense for the canvas.
Content: The default. Specifies that the canvas should occupy the entire content area of the window to which it is assigned. Most canvases are content canvases.
Stacked: Specifies that the canvas should be displayed in its window at the same time as the window's content canvas. Stacked views are usually displayed programmatically and overlay some portion of the content view displayed in the same window.
Vertical Toolbar Canvas: Specifies that the canvas should be displayed as a vertical toolbar under the menu bar of the window. You can define iconic buttons, pop-lists, and other items on the toolbar as desired.
Horizontal Toolbar Canvas: Specifies that the canvas should be displayed as a horizontal toolbar at the left side of the window to which it is assigned.


Raise on Entry property
For a canvas that is displayed in the same window with one or more other canvases, Raise on Entry specifies how Form Builder should display the canvas when the end user or the application navigates to an item on the canvas.
? When Raise on Entry is No, Form Builder raises the view in front of all other views in the window only if the target item is behind another view.
? When Raise on Entry is Yes, Form Builder always raises the view to the front of the window when the end user or the application navigates to any item on the view.

Block Level Properties

Navigation Style property
Specifies how a Next Item or Previous Item operation is processed when the input focus is in the last navigable item or first navigable item in the block, respectively.

Same Record (Default): A Next Item operation from the block's last navigable item moves the input focus to the first navigable item in the block, in that same record.
Change Record: A Next Item operation from the block's last navigable item moves the input focus to the first navigable item in the block, in the next record. If the current record is the last record in the block and there is no open query, Form Builder creates a new record. If there is an open query in the block (the block contains queried records), Form Builder retrieves additional records as needed.
Change Block: A Next Item operation from the block's last navigable item moves the input focus to the first navigable item in the first record of the next block. Similarly, a Previous Item operation from the first navigable item in the block moves the input focus to the last item in the current record of the previous block. The Next Navigation Block and Previous Navigation Block properties can be set to redefine a block's "next" or "previous" navigation block.

Previous Navigation Block property
Specifies the name of the block that is defined as the "previous navigation block" with respect to this block. By default, this is the block with the next lower sequence in the form, as indicated by the order of blocks in the Object Navigator.

Next Navigation Block property
Specifies the name of the block that is defined as the "next navigation block" with respect to this block. By default, this is the block with the next higher sequence number in the form, as indicated by the order of blocks listed in the Object Navigator.

Query Array Size property
Specifies the maximum number of records that Form Builder should fetch from the database at one time.

Number of Records Buffered property
Specifies the minimum number of records buffered in memory during a query in the block.

Number of Records Displayed property
Specifies the maximum number of records that the block can display at one time. The default is 1 record. Setting Number of Records Displayed greater than 1 creates a multi-record block.

Query All Records property
Specifies whether all the records matching the query criteria should be fetched into the data block when a query is executed.
Yes - Fetches all records from query; equivalent to executing the EXECUTE_QUERY (ALL_RECORDS) built-in.
No - Fetches the number of records specified by the Query Array Size block property.


Record Orientation property
Determines the orientation of records in the block, either horizontal records or vertical records. When you set this property, Form Builder adjusts the display position of items in the block accordingly.


Single Record property
Specifies that the control block always should contain one record. Note that this differs from the number of records displayed in a block.


Database Block property
Specifies that the block is based on any of the following block data source types: table, procedure, transactional trigger, or sub-query. Yes or No.

Enforce Primary Key (Block) property
Indicates that any record inserted or updated in the block must have a unique key in order to avoid committing duplicate rows to the block's base table.


Query Allowed (Block) property
Specifies whether Form Builder should allow the end user or the application to execute a query in the block. When Query Allowed is No, Form Builder displays the following message if the end user attempts to query the block:
FRM-40360: Cannot query records here.

Query Data Source Type property
Specifies the query data source type for the block. A query data source type can be a Table, Procedure, Transactional Trigger, or FROM clause query.


Query Data Source Columns property
Specifies the names and datatypes of the columns associated with the block's query data source. The Query Data Source Columns property is valid only when the Query Data Source Type property is set to Table, Sub-query, or Procedure.

Query Data Source Arguments property
Specifies the names, datatypes, and values of the arguments that are to be passed to the procedure for querying data. The Query Procedure Arguments property is valid only when the Query Data Source Type property is set to Procedure.


Alias property
Establishes an alias for the table that the data block is associated with. The Data Block wizard sets the Alias property to the first letter of the table name. (For example, a table named DEPT would have a default alias of D.)

Include REF Item property
Creates a hidden item called REF for this block. This item is used internally to coordinate master-detail relationships built on a REF link. This item also can be used programmatically to access the object Id (OID) of a row in an object table.

WHERE Clause/ORDER BY Clause
The defaults WHERE Clause and default ORDER BY Clause properties specify standard SQL clauses for the default SELECT statement associated with a data block. These clauses are automatically appended to the SELECT statement that Form Builder constructs and issues whenever the operator or the application executes a query in the block.


Optimizer Hint property
Specifies a hint string that Form Builder passes on to the RDBMS optimizer when constructing queries. Using the optimizer can improve the performance of database transactions.
Insert Allowed (Block) property
Specifies whether records can be inserted in the block.


Update Allowed (Block) property
Determines whether end users can modify the values of items in the block that have the Update Allowed item property set to Yes. (Setting Update Allowed to No for the block overrides the Update Allowed setting of any items in the block.)


Locking Mode property
Specifies when Form Builder tries to obtain database locks on rows that correspond to queried records in the form. The following table describes the allowed settings for the Locking Mode property:

Automatic (default): Identical to Immediate if the datasource is an Oracle database. For other datasources, Form Builder determines the available locking facilities and behaves as much like Immediate as possible.
Immediate: Form Builder locks the corresponding row as soon as the end user presses a key to enter or edit the value in a text item.
Delayed: Form Builder locks the row only while it posts the transaction to the database, not while the end-user is editing the record. Form Builder prevents the commit action from processing if values of the fields in the block have changed when the user causes a commit action.


Delete Allowed property
Specifies whether records can be deleted from the block.


Key Mode property
Specifies how Form Builder uniquely identifies rows in the database. By default, the ORACLE database uses unique ROWID values to identify each row. Non-ORACLE databases do not include the ROWID construct, but instead rely solely on unique primary key values to identify unique rows. If you are creating a form to run against a non-ORACLE data source, you must use primary keys, and set the Key Mode block property accordingly.

Automatic (default): Specifies that Form Builder should use ROWID constructs to identify unique rows in the datasource but only if the datasource supports ROWID.
Non-Updateable: Specifies that Form Builder should not include primary key columns in any UPDATE statements. Use this setting if your database does not allow primary key values to be updated.
Unique: Instructs Form Builder to use ROWID constructs to identify unique rows in an ORACLE database.
Updateable: Specifies that Form Builder should issue UPDATE statements that include primary key values. Use this setting if your database allows primary key columns to be updated and you intend for the application to update primary key values.

Update Changed Columns Only property
When queried records have been marked as updates, specifies that only columns whose values were actually changed should be included in the SQL UPDATE statement that is sent to the database during a COMMIT. By default, Update Changed Columns Only is set to No, and all columns are included in the UPDATE statement.


Enforce Column Security property
Specifies when Form Builder should enforce update privileges on a column-by-column basis for the block's base table. If an end user does not have update privileges on a particular column in the base table, Form Builder makes the corresponding item non-updateable for this end user only, by turning off the Update Allowed item property at form startup.
Yes: Form Builder enforces the update privileges that are defined in the database for the current end user.
No: Form Builder does not enforce the defined update privileges.

DML Data Target Type property
Specifies the block's DML data target type. A DML data target type can be a Table, Procedure, or Transactional Trigger.

DML Data Target Name property
Specifies the name of the block's DML data target. The DML Data Target Name property is valid only when the DML Data Target Type property is set to Table.


Visual Attribute Group property
Specifies how the object's individual attribute settings (Font Name, Background Color, Fill Pattern, etc.) are derived.

Default: Specifies that the object should be displayed with default color, pattern, and font settings. When Visual Attribute Group is set to Default, the individual attribute settings reflect the current system defaults. The actual settings are determined by a combination of factors, including the type of object, the resource file in use, and the platform.
Named visual attribute: Specifies a named visual attribute that should be applied to the object. Named visual attributes are separate objects that you create in the Object Navigator and then apply to interface objects, much like styles in a word processing program. When Visual Attribute Group is set to a named visual attribute, the individual attribute settings reflect the attribute settings defined for the named visual attribute object. When the current form does not contain any named visual attributes, the poplist for this property will show Default.

White on Black property
Specifies that the object is to appear on a monochrome bitmap display device as white text on a black background.


Listed in Data Block Menu/Data Block Description
Specifies whether the block should be listed in the block menu and, if so, the description that should be used for the block.
Form Builder has a built-in block menu that allows end users to invoke a list of blocks in the current form by pressing [Block Menu]. When the end user selects a block from the list, Form Builder navigates to the first enterable item in the block.

Analytic Functions

Analytic Functions are designed to address such problems as "Calculate a running total", "Find percentages within a group", "Top-N queries", "Compute a moving average" and many more.
Analytic functions compute an aggregate value based on a group of rows. They differ from aggregate functions in that they return multiple rows for each group. The group of rows is called a window and is defined by the analytic clause. For each row, a "sliding" window of rows is defined. The window determines the range of rows used to perform the calculations for the "current row". Window sizes can be based on either a physical number of rows or a logical interval such as time.
Analytic functions are the last set of operations performed in a query except for the final ORDER BY clause. All joins and all WHERE, GROUP BY, and HAVING clauses are completed before the analytic functions are processed. Therefore, analytic functions can appear only in the select list or ORDER BY clause.

The Syntax

The Syntax of analytic functions is rather straightforward in appearance
Analytic-Function (<Argument>, <Argument>,...)
OVER (
<Query-Partition-Clause>
<Order-By-Clause>
<Windowing-Clause>
)
• Analytic-Function
Specify the name of an analytic function, Oracle actually provides many analytic functions such as AVG, CORR, COVAR_POP, COVAR_SAMP, COUNT, CUME_DIST, DENSE_RANK, FIRST, FIRST_VALUE, LAG, LAST, LAST_VALUE, LEAD, MAX, MIN, NTILE, PERCENT_RANK, PERCENTILE_CONT, PERCENTILE_DISC, RANK, RATIO_TO_REPORT, STDDEV, STDDEV_POP, STDDEV_SAMP, SUM, VAR_POP, VAR_SAMP, VARIANCE.
• Arguments
Analytic functions take 0 to 3 arguments.
• Query-Partition-Clause
The PARTITION BY clause logically breaks a single result set into N groups, according to the criteria set by the partition expressions. The words "partition" and "group" are used synonymously here. The analytic functions are applied to each group independently; they are reset for each group.
• Order-By-Clause
The ORDER BY clause specifies how the data is sorted within each group (partition). This will definitely affect the outcome of any analytic function.
• Windowing-Clause
The windowing clause gives us a way to define a sliding or anchored window of data, on which the analytic function will operate, within a group. This clause can be used to have the analytic function compute its value based on any arbitrary sliding or anchored window within a group.

Commit processing is the way Form Builder attempts to make the data in the database identical to the data in the form. Form Builder's normal cycle of operation is:

1 Read records from the database.

2 Allow the end user to make tentative insertions, updates, and deletions. The tentative changes appear only in the form. The database remains unchanged.

3 Post changes to the database. Form Builder does all of its remaining processing and sends the data to the database. After posting the data, Form Builder can only roll back the changes (via the [Clear Form] function key or CLEAR_FORM built-in) or commit them.

4 Form Builder commits the posted changes. They become permanent changes to the database.

The term commit processing refers to steps 3 and 4 of the above cycle. Normally these steps occur together. In response to the [Commit] key or invocation of the COMMIT_FORM built-in, Form Builder firsts posts, then commits the data to the database.
Posting can occur separately before committing. End users cannot issue posting commands, but triggers can invoke the POST built-in.

Note: Form Builder does not support a commit that does not include the normal Form Builder commit processing. For example, Form Builder does not support a commit issued from an Oracle Precompiled user exit or from a stored procedure.

Processing inserts, updates, and deletes

Posting consists of sending tentative changes from the form to the database. These are records that have been marked for insertion, update, or deletion since the last post. During posting, Form Builder processes inserts, updates, and deletes for all blocks in a form. Form Builder has a standard way to process these changes. Triggers provide a flexible mechanism for altering the standard behavior.
Insert: An insert is the pending insertion of a row in the database. Each insert has an associated SQL statement, which Form Builder executes when it posts the insert. The statement has the following form:

INSERT INTO table [(column, column, . . .)] VALUES (value, value, . . .);

Table The name of the base table for the current block.
Column A column corresponding to a base table item. If an item is a derived column, it does not appear in the column clause.
Value The value to insert into the corresponding column.
Update: An update is the pending update of an existing row in the database. Each update has an associated SQL statement, which Form Builder executes when it posts the update. The statement has the following form:

UPDATE table SET (column=value, column=value, . . .) WHERE ROWID=rowid_value;

Table The name of the base table for the current block.
Column A column corresponding to a base table item. If an item is a derived column, it does not appear in the column clause.
Value The value to update the corresponding column.
Rowid_value The ROWID value for the row Form Builder is updating.
Note:

The WHERE clause specifies only a ROWID value. This identifies the unique row that the database should update. Form Builder uses the ROWID construct only when the block's Key Mode property has the value Unique_Key (the default).

If an end user does not have update privileges for a column, and the block's Enforce Column Security property has the value Yes, Form Builder does not include the column in the UPDATE statement. Different end users can have different UPDATE statements, depending on their privileges, but the statement remains unchanged during an end user's Form Builder session.

Delete: A delete is the pending deletion of a row in the database. Each delete has an associated SQL statement, which Form Builder executes when it posts the delete. The statement has the following form:

DELETE FROM table WHERE ROWID=rowid_value;

Table The name of the base table for the current block.
Rowid_value The ROWID value for the row Form Builder is deleting.
Note: The WHERE clause specifies only a ROWID value. This identifies the unique row that the database should delete. Form Builder does not use the ROWID value for non-Oracle data sources.
Caution: If a commit fails, the ROWID value may not have a null value. Use record status rather than the ROWID value to test for success or failure of the commit.

When commit processing occurs

Form Builder performs commit processing when

A trigger or the [Commit] key invokes the COMMIT_FORM built-in.

Database items in the current block have changed since the last commit, and a trigger invokes the CLEAR_BLOCK built-in (with the DO_COMMIT argument).

Database items in the form have changed since the last commit, and a trigger invokes the CLEAR_FORM built-in (with the DO_COMMIT argument).

An end user answers Yes to the alert that asks

Do you want to commit the changes you have made?

The alert appears as a result of any of the following:

Database items in the current block have changed since the last commit, and any of the following events occurs:

CLEAR_BLOCK (with the ASK_COMMIT argument or with no argument)
COUNT_QUERY
ENTER_QUERY
EXECUTE_QUERY
NEW_FORM

Database items in any block of the form have changed since the last commit, and any of the following events occurs:

CLEAR_FORM (with the ASK_COMMIT argument or with no argument)
Leave the Form

Note: When a PL/SQL block issues a database commit from within Form Builder (via the SQL COMMIT statement), Form Builder commit processing occurs as if the COMMIT_FORM built-in had been invoked.

Changing data during commit processing

Commit processing performs validation and can fire triggers. As a result, a commit event can change database items. For some triggers Form Builder attempts to commit those changes to the database during the current commit event.

Caution: Form Builder can commit un-validated database changes made by the PRE-DELETE, PRE-INSERT, and PRE-UPDATE triggers.

For example, assume that a PRE-INSERT trigger selects a value into a base table item.

If Form Builder has not already processed the affected record's updates, it commits the changes during the current commit event without validating them.

If Form Builder has already processed the affected record's updates, it does not commit the changes, but it marks the records as having come from the database. Subsequently attempting to update the record before it is committed causes an error.

Caution: Form Builder can commit un-validated database changes made by the POST-COMMIT, POST-DELETE, POST-INSERT, and POST-UPDATE triggers.

For example, assume that a POST-INSERT trigger selects a value into a base table item.

If the affected record is in a block that the commit has processed or is processing, Form Builder does not commit the changes during the current commit event. If the commit succeeds, Form Builder marks all items and records as Valid. This results in an error during subsequent processing.

If the affected record is in a block that the commit has not yet processed, Form Builder validates the changes and commits them during the current commit event.

Replacing standard commit processing

The ON-INSERT, ON-UPDATE, and ON-DELETE triggers replace standard commit processing.