Professional Documents
Culture Documents
Agenda:
Background to using SQL for Applications users
How the underlying data structures relate to what you see on the screen with Oracle
Applications
Basic relational database concepts
Introducing the SQL language
Basic SELECT syntax
Using Logical operators
Single Row functions
Group Functions
Joining multiple tables
Sub-Queries
Brief overview of DML (Insert, Update and Delete)
Commit statements
Brief overview of DDL (Create, Alter, Drop and Truncate)
Brief overview of DCL (Grant and Revoke, System and object privileges)
Oracles Data Dictionary
Reports written in SQL*Plus
Page 1-1
SQL
Page 1-2
SQL
Database Tables
Journals table
ID
Name
Job
Salary
Dept
753
Jones
Sales
$25K
755
Brown
Mgr
$50K
757
Smith
Clerk
$15K
Primary Key:
Journal Header ID
Journal Lines
Foreign Key:
Journal Header ID
In a relational database, data is stored in two dimensional tables which are made up of rows
and columns.
Each column contains a particular type of data and each row contains a single record.
When database columns are created, the type of data they can contain is specified as either
character strings (VARCHAR2 or CHAR), numeric (NUMBER) or date/time values
(DATE). Size constraints can also be specified for most column data-types e.g.
VARCHAR2(15) stores alphanumeric data up to 15 characters.
Each record may be uniquely identified by a primary key a column made up of unique
value which allows individual records to be identified.
Links between tables are made by duplicating the value of a primary key in a related record.
The value in the related record is known as a foreign key.
Oracles type of database is called a relational database because of the way records are linked
via these related values.
This leads to a master-detail arrangement of data. Understanding this hierarchical
arrangement of data seems to be one of the key insights which new Applications users need
to really understand in order to work comfortably with Oracle Apps.
It is also possible to make specific columns mandatory when they are defined. This is done
by placing a not null constraint in their definition.
If a record does not populate an optional column the value in this field is called null. This is
an important concept which we will mention frequently. (The opposite to a null is a real
value i.e. a field which has been populated with some kind of data).
N.B. In reality some of Oracles Application tables do not have the sorts of constraints
which we might expect this is because some of this logic is enforced by the Forms front
end. The reason for this is so that Forms provides the user quicker feedback about errors they
have made, for example in attempting to duplicate a primary key value.
From a users point of view, database tables are organised in non-intuitive (master detail)
ways but the design is intended to reduce data redundancy and create efficient data access
(redundancy = storage duplication).
Page 1-3
Page 1-4
SQL
To present the user with the data in the way they expect, it is necessary to join the tables so
that a record in the parent table is linked to one in the child table where the key values
match. We will see exactly how this is done later in this course.
Oracles Technical Reference Manuals (TRMs) contain overview diagrams called ER
Diagrams (Entity Relationship Diagrams) which show data entities (tables) and their
relationships (primary / foreign keys).
The TRMs also describe each table including the size and type of data in each column.
Each Application has a Technical Reference Manual.
Oracles TRMs can be purchased from Oracle or accessed electronically through Metalink.
Most tables also contain a set of empty columns to enable the Descriptive Flexfields
feature. These columns are called ATTRIBUTE N (where N is a number).
SQL
Journals Window
Header Block / Region
Name
Status
Tables and
Blocks
Category
Journals table
Source
Cr
Amt
Desc.
Journal Lines
The underlying tables are represented on the users screen using a tool called Forms, which
both paints the screens and manipulates the data.
Each table is represented by a block within a window. A block is a logical group of fields.
It may present the data as single records (as in the journal header block), or as a multi-record
block (as in the journal lines block).
The term block is used in some of the diagnostic tools, so it may be helpful to understand
it.
This is the actual Applications window represented diagrammatically in the previous slide.
It will now be clear that in this window the user is looking at data from two tables.
A Forms developer has written code which joins these tables so that when the user retrieves
a particular journal, they see the lines related to THAT journal, rather than some other
journal.
It also explains why when I put my cursor in one of journal lines and invoke the Find
Window, I get a different system response than if I have my cursor in one of the fields in the
journal header region.
To anthropomorphise this a little:
If my cursor is in the journal header region and I invoke the find window, the system thinks:
aha, she wants to search for a particular journal.
If my cursor is in one of the lines, the system thinks: shes looking for a particular line in this
journal.
Page 1-5
SQL
Page 1-6
SQL
AR
AP
PO
APPS
Within Oracle Applications there are a number of helpful links to the underlying technology
components.
Within the database, there are product schemas (database user areas).
For every application which is installed, you will have a schema which stores its data, for
example GL, AP, PA, etc
Help ~ Record History tells you not only which user created the record (the so-called Row
Who data written transparently with each record) but also the name of the table from which
the data is drawn.
The GL schema contains all the tables, views and other database objects belonging to the
General Ledger Application.
Help ~ Diagnostics ~ Examine provides access to values hidden from the user. These include
foreign keys and codes represented by different display values.
Help ~ About Oracle Applications includes version and path information.
You must have a password to use some of these tools your DBA can provide this to you.
The Apps Schema has access to the complete Oracle Applications data model. It consists
mainly of PL/SQL logic rather than storage (in packaged procedures and views). Much of
this logic is there to enable Oracles Multi-Org technology.
Since the advent of Multi-Org technology all users write to the various tables in GL, AP etc
through procedures stored in the APPS schema, which ensures that new records are tagged
with the organisation ID linked to that user (usually through a profile option at Responsibility
level). In the same way when users retrieve data, the APPS schema filters the data so that
users only see the data relating to their organisation.
So the APPS schema is a sort of gate-keeper to the other schemas.
In this way, Multi-Org technology allows multiple organisations to secure data within a
single database.
It is supported by an ORG_ID column on tables and an MO:Operating Unit profile option for
each Responsibility (which creates an implicit where clause linking the users organisation
only with records which have the appropriate Org_ID value).
Page 1-7
SQL
Page 1-8
SQL
Primary Key:
EmpNo
Dept table
Primary Key:
DeptNo
This set up script for this data is shipped with every Oracle database.
The SQL acronym stands for Structured Query Language and is pronounced sequel.
It might seem simplistic, however it gives you an easy environment in which to learn the
skills which you can subsequently use with large volumes of data in the Applications
environment.
Oracles version complies with the international (ANSI) standard and is only marginally
different to that of other vendors (like Microsofts SQLServer or IBMs DB2).
SQL commands are not case sensitive.
As a general principle, users should never attempt to update the database directly using SQL.
Updating the database directly (rather than through the Applications) can seriously damage
the integrity of your data and invalidate your support contract.
SQL commands are typically issued from a utility like Oracles SQL Plus tool or Quests
TOAD.
Both these tools require client-side set-up (i.e. installation on you PC) before you can use
them.
For both tools you will also need a database user name and password from your DBA. You
cannot use your Applications login here.
Page 1-9
SQL
Page 1-10
SQL
To use the where clause to restrict rows, you must use a logical condition (more about logical
operators in the next slide).
Dates and alphanumeric literals (so-called character data) must be enclosed in single quotes
and are case sensitive.
Default date format is DD-MON-YY
A select statement without a where clause will retrieve all of the records in the table.
By default the order by clause sorts the output in an ascending sequence, you can change this
by using the DESC qualifier as shown.
The numbers in the final statement in the above slide are a short-hand way of referring to the
columns in the select list.
You can sort by columns which you do not display (i.e. are not in your select list).
To execute the command you use either a semi-colon on the final command line or a forward
slash on the following line.
Page 1-11
SQL
Page 1-12
SQL
Negative Conditions
Logical Operators
In the example on the previous slide the Where clause restricts the data retrieved based on a
strict matching of values to a single literal value.
Page 1-13
SQL
Page 1-14
SQL
Introducing Functions
Other Tricks
Other tricks which can be used in Select statements include the following:
Functions are mini-programs which can be applied to column values to manipulate them.
Simple arithmetic e.g. Date_column + 7 (i.e. a week after the recorded date)
Multiplication and division have precedence over addition and subtraction.
Use parenthesis to enforce execution order.
Nulls will cause very strange results well see how to handle them in a moment.
The NVL function may be one of the most important functions for Applications users.
Concatenation e.g. Number ||ID_column (i.e. this adds the word number to every ID
value retrieved)
A column value which has not been populated for a particular record is said to contain a null
value.
The logical operators AND and OR may be used to combine operators, e.g
where period_name = JAN-01 or period_name = FEB-01
Remember to use parenthesis to control the order in which such statements are evaluated
otherwise AND takes precedence over OR
Null values produce unexpected results when they are involved in arithmetic.
NVL allows null values to be replaced in ways that produce sensible results.
The syntax is NVL(col, alt) where col is the column or value to be evaluated and alt is the
value to substitute in if a null is found in col.
For example:
1,000 + null = null.
For example:
1,000 + NVL(null, 0) = 1,000
NVL only replaces nulls, it will not affect real values.
Page 1-15
SQL
Page 1-16
SQL
Functions are classified according to the type of data which they are used with: Number,
Date, Character or Conversion functions.
TO_CHAR is an example of a conversion function. Similar functions exist for the other datatypes as well (To_Number, To_Date). They can also be used for some kinds of formatting.
ROUND is an example of a function which manipulates numbers.
UPPER is an example of a function which manipulates character strings.
SUBSTR is am example of a string operator (also called a character function).
These functions return values for each row passed to them, so they are called single row
functions; later we will meet functions which produce one result for a group of rows.
Functions can also be nested in which case the innermost function executes first out to
the outermost function.
Page 1-17
SQL
Page 1-18
SQL
Note that logical operations including dates can be complicated by the time portion which is
invisible by default. This makes the TRUNC or ROUND functions valuable because you can
effectively remove the time component from a logical operation.
N.B. the Sign function can be useful in determining which values are larger.
For example Sign (Col_A Col_B) will return +1 if Col_B is less than Col_A
Page 1-19
SQL
Page 1-20
SQL
Statement:
Select to_char (sysdate,
'fmDay ddth "of" Month YYYY') today
from dual
Output:
TODAY
-------------------------------Monday 16th of April 2007
This example shows the flexibility and power of Oracles functions.
Dual is a dummy table which allows operations like this one i.e. where the structure of the
select statement requires a from clause even though the required result is not found in a
table.
Clever developers can use the decode functions in some very interesting ways.
In this case the To_Char function takes 2 arguments or inputs: the date it is to manipulate
and the mask which tells the system how to display the information. The mask is made up
of tokens. You can see a complete list of these tokens and all the arguments accepted by
Oracle functions in the SQL Language reference manuals supplied by Oracle as part of their
database server documentation (or available in softcopy form Oracles OTN Technet).
The syntax is: DECODE (expr, search, result [, search, result...] [,default])
Where expr is the expression (column or value) being evaluated
and search is the value to find
and result is the value to replace it with
(you may have as many search and result pairs as you require
and default is the value to output if the expression contains none of the specified search
values
Page 1-21
Page 1-22
SQL
SQL
Joining Tables
Group Functions
In contrast to the previous functions which return one result for each row evaluated, group
functions produce one result for set of rows.
If joining N tables, at least N - 1 join conditions must be specified. Omitting the join when
referencing more than one table creates a Cartesian Product i.e. invalid results
To avoid ambiguous column references, column names such as Invoice_ID above must be
prefixed with a table reference (because a column of the same name exists in both columns
e.g. emp.deptno).
When using group functions, the select list may only contain columns which are subject to a
group function e.g. sum(col) or those columns which are listed in the group by clause.
Use the Where Clause to exclude rows from your groups. You cannot use group functions to
create conditions in the where clause.
It is in fact good practice to preface all column names with their table names this makes it
easier for the database to parse the statement. To simplify such statements it is therefore
advisable to add short table aliases.
Use the Having Clause to remove a group based on a group characteristic, for example the
following statement excludes any organisations which have had less than 500 invoices :
Select AVG(Invoice_amount)
from AP_Invoices_all
group by org_ID
having count(org_ID) > 500;
Group functions handle null values in the way you would expect i.e. they ignore them
As you can see this makes real-life SQL statements quite long and difficult to write; an
example showing the actual statement required to achieve these results is given on the next
page. The process which a Developer would use to research the details is also explained.
Page 1-23
SQL
Page 1-24
SQL
Join
Example
PO_Vendors
Supplier Name
AP_Invoices_All
Vendor_ID
AP_Invoices_Distributions_All
CC_ID
GL_Code_Combinations
Account Code
Actual Statement:
select vendor_name, c.segment1||'.'||c.segment2||'.'||c.segment3||'.'||c.segment4||'.'||c.segment5
account_code
from PO_vendors V,
AP_Invoices_All I,
AP_Invoice_Distributions_All D,
GL_Code_Combinations C
where i.Invoice_ID = d.Invoice_ID
and i.vendor_id = v.vendor_id
and d.dist_code_combination_id = c.code_combination_id
Page 1-25
ACCOUNT_CODE
------------------------01.420.7410.0000.000.
In this slide, the major tables supporting the Accounts Payable functionality are displayed,
however, this display does make it look somewhat simpler than it in fact is
For example, in the AP_INVOICES_ALL table the Supplier name value which the user sees
on the Applications screen, has to be retrieved by the developer from the PO_VENDORS
table by joining on the Vendor_ID (code). Similarly the Invoice Distributions table does not
contain the account code which the user sees it only contains an ID (foreign key) pointing
to where the account code is really stored.
This account code ID is called the CC_ID or Code Combination ID and it must be joined to
the GL_CODE_COMBINATIONS table in order to retrieve the code which the user is used
to seeing.
Output:
VENDOR_NAME
------------------------Building Management Inc.
Developers unfamiliar with a particular module will need to invest some time and effort to
become familiar with its tables.
The tables are all quite large and the data will not be meaningful to the developers in the
same way which it is to the business user thus the ideal development methodology for
modifications involves close collaboration between a business user and a developer.
SQL
Page 1-26
SQL
Sub-Queries
Sub-Queries are a very powerful tool for solving more difficult data retrieval problems.
If you are reviewing SQL scripts written by applications developers you are likely to see such
statements. They are easier to read if you remember that they are executed sequentially: the
most deeply nested statement executes first and then passes its results to the next most
deeply nested statement etc.)
Note that RowNum cannot be used meaningfully in conjunction with an order by clause as it
limits the rows retrieved before they are sorted.
If need be, use a highlighter to identify each sub-query and read them as if they were
executing sequentially from the most deeply nested statement, remembering that the nested
statement passes its results to the statement in which it is nested.
Page 1-27
SQL
Page 1-28
SQL
Sample Data
Emp Table
EMPNO
7369
7499
7521
7566
7654
7698
7782
7788
7839
7844
7876
7900
7902
7934
ENAME
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
JAMES
FORD
MILLER
JOB
CLERK
SALESMAN
SALESMAN
MANAGER
SALESMAN
MANAGER
MANAGER
ANALYST
PRESIDENT
SALESMAN
CLERK
CLERK
ANALYST
CLERK
MGR
7902
7698
7698
7839
7698
7839
7839
7566
7698
7788
7698
7566
7782
HIREDATE
13-JUN-83
15-AUG-83
26-MAR-84
31-OCT-83
05-DEC-83
11-JUN-84
14-MAY-84
05-MAR-84
09-JUL-84
04-JUN-84
04-JUN-84
23-JUL-84
05-DEC-83
21-NOV-83
SAL
800
1600
1250
2975
1250
2850
2450
3000
5000
1500
1100
950
3000
1300
COMM
300
500
1400
DEPTNO
20
30
30
20
30
30
10
20
10
30
20
30
20
10
Dept Table
DEPTNO
10
20
30
40
Oracle maintains its own list of which objects exist in the database. This is called the data
dictionary (dict).
You can use standard SQL commands to find out information about various objects.
DNAME
ACCOUNTING
RESEARCH
SALES
OPERATIONS
LOC
NEW YORK
DALLAS
CHICAGO
BOSTON
There are three classes of views (pseudo tables) which you can query, they have the prefixes
USER_xxx, ALL_xxx and DBA_xxx
For most of us the ALL_xxx views will probably be the most useful as they contain
information about all the objects which you can access.
Here are some examples:
Select text from ALL_VIEWS
Where view_name = AP_INVOICES;
If you are looking for a reference book, we recommend Oracle N: the Complete Reference by
Kevin Loney and George Koch (where N is the current version i.e. 8i, 9i, 10g etc)
You can also download Oracle documentation without charge from Oracles OTN site
(http://otn.oracle.com.index.html) although you may find it a little daunting to begin with.
The Describe command (also Desc) will list the columns / structure of any specified table e.g.
Desc GL_JE_Headers
This command does not need a semi-colon to execute it.
Page 1-29
Page 1-30
SQL
SQL