Beruflich Dokumente
Kultur Dokumente
As I understand it, industry standard VARCHAR types can store 'empty' strings, but
Oracle currently does not, although it reserves the right to change the functionality
of VARCHAR types to have this feature. Oracle invented the VARCHAR2 type which is
the non-standard varchar (which changes empty strings to nulls), and
*currently* the VARCHAR is just a synonym for VARCHAR2. Oracle advises everyone
to use VARCHAR2 and not VARCHAR if they wish to maintain backward compatiblity.
If a column in a row has no value, then column is said to be null, or to contain a null.
Nulls can appear in columns of any datatype that are not restricted by NOT NULL or
PRIMARY KEY integrity constraints. Use a null when the actual value is not known or
when a value would not be meaningful. Do not use null to represent a value of zero,
because they are not equivalent. (Oracle currently treats a character value with
a length of zero as null. However, this may not continue to be true in future
releases, and Oracle recommends that you do not treat empty strings the same as
NULLs.) Any arithmetic expression containing a null always evaluates to null. For
example, null added to 10 is null. In fact, all operators (except concatenation) return
null when given a null operand. Oracle is usually careful to maintain backward
compatibility, so I'd be slightly surprised to see this change even in Oracle 9 and very
surprised to see it change in any version of Oracle 8.
Oracle7 assigns a ROWNUM value to each row as it is retrieved, before rows are
sorted for an ORDER BY clause, so an ORDER BY clause normally does not affect
the ROWNUM of each row. However, if an ORDER BY clause causes Oracle7 to use an
index to access the data, Oracle7 may retrieve the rows in a different order than
without the index, so the ROWNUMs may differ than without the ORDER BY clause.
You can use ROWNUM to limit the number of rows returned by a query, as in
this example:
You can also use ROWNUM to assign unique values to each row of a table, as
in this example:
Note that conditions testing for ROWNUM values greater than a positive integer
are always false.
If so, how ? ..... Solution Summary: YOU HAVE TO REPLICATE THE ENTIRE TABLE
"With masters, you must still replicate the entire table; you cannot replicate a
partition. If you want to replicate selected paritions only rather than the entire table
use updatable snapshots."
Workaround: Create a Snapshot withe a WHERE Clause, which SELECT's only from
one Partition.
Many batch jobs must change a user password to a "dummy" password to login as
this user temporarilly. The problem is always the encrypted password which cannot
be de-crypted so that the password can be reset to the original. There is a way to do
this, using the VALUES keyword, this is exactly what IMPORT does when it must
create users. We show you how to perform this task using the famous account of
SCOTT/TIGER. If a DBA doesn't know the password "TIGER", he can temporarilly
change this password to another password, logon as user SCOTT, and set the
password back after the jobs as user SCOTT is done. Follow the instructions below:
o Connect as a DBA User and get the encypted passord for User SCOTT
sqlplus system/manager
select password from dba_users where username = 'SCOTT';
PASSWORD
------------------------------
F894844C34402B67
o Connect as User SCOTT with the temporary password 'hello' and do the
necessary jobs as SCOTT
sqlplus scott/hello
It's often very interesting to know the size of each object in the database. For
example if you need to pin a package, trigger in the memory. Note, that NOT the size
of a table including the rows will be shown, but the size of the table itself.
--
#######################################################################
######################
--
-- %Purpose: Show Size of each Object itself (without content) in the
Database
--
-- Show size of each object in the database. Note
-- that NOT the size including the rows will be
-- shown for tables, but the size for the table itself.
--
--
#######################################################################
######################
--
set feed off;
set pagesize 10000;
set wrap off;
set linesize 200;
set heading on;
set tab on;
set scan on;
set verify off;
spool show_obyect_size.lst
ttitle off;
First get the top 10 CPU-intensive Oracle processes on the operating system with the
first column giving the %CPU used, the second column unix PID, the third column
USER , the fourth column TERMINAL, and the last column Unix PROCESS (works only
for UNIX).
Now you can specify the found PID in the following SQL-Statement:
Table created.
NAME VORNAME
-------- --------
zahn martin
1 row deleted.
The TRACE File for the Controlfile can be found in the directory defined by the init.ora Parameter:
core_dump_dest.
Top-N queries
Suppose you want to retrieve and sort information on the 4 most recently
hired employees in a very efficient way. This can be achieved using an inline view
combined with ORDER BY and ROWNUM.
Inline Views
An in-line view, which is a feature of a Top-N SQL query, is a subquery. This type of
subquery differs from a regular subquery by containing an ORDER BY clause which is
not allowed in a regular subquery. The ROWNUM condition, which is an enhanced
sorting mechanism, would be used in the outer query to complete the Top-N SQL
query.
Example
We need to retrieve and sort information on the 4 most recently hired employees
from the following list (marked in blue color).
The first approach is to used the following query, which does not select the Top-4
rows !
SELECT empno,ename,hiredate
FROM emp
WHERE ROWNUM < 5
ORDER BY hiredate DESC;
SELECT *
FROM (SELECT empno,ename,hiredate
FROM emp
ORDER BY hiredate DESC)
WHERE ROWNUM < 5;
The purpose of this query is to retrieve and sort information on the 4 most recently
hired employees. This is a Top-N SQL query which is more efficient than a regular
query because Oracle stores a maximum of only 5 rows as the data is retrieved from
the table avoiding sorting all of the rows in the table at once. The WHERE clause
contains 'ROWNUM < 5' which prevents sorting on more than 5 rows at one time --
cool isn't it ?
Sometimes you run an INSERT or DELETE statement that takes a long time to
complete. You have wondered how many rows have already been inserted or deleted
so that you can decide whether or not to abort the statement. Is there a way to
display how many rows have been deleted while the statement is occurring ?
You can query the V$SESSION_LONGOPS table to track the progress of the
statement.
SELECT sid,sofar,totalwork,time_remaining
FROM v$session_longops
WHERE sid = 10 and time_remaining > 0;
Deleting: D:\ORADATA\ARK1\ARC\ARK1_83.ARC
Deleting: D:\ORADATA\ARK1\ARC\ARK1_84.ARC
Deleting: D:\ORADATA\ARK1\ARC\ARK1_85.ARC
The procedure can easily enhanced to read the ARCHIVEDIR from the data dictionary
and to enter the days back to delete (instead of the fixed 30 days) as an argument
for the procedure DELETE_ARCHIVE_LOGS.
So far so good. We’ll now demonstrate what happens, when we insert records from
two concurrent transactions. Delete one record in order to be able to insert one
record hereafter:
Invoke a second session by starting a SQL-Plus again and run the statement
commit;
Commit complete.
Oracle does not support the "dirty read“ isolation level which is defined with:
"A transaction reads data written by concurrent uncommitted transaction“
commit;
Commit complete.
Remarks
If an exact maximum number of records is specified for a table, using a lock is the
only solution to avoid the above situation demonstrated. Often we want to limit the
number of deteilrecords in a way, that an end user can’t claim a huge amount of disk
space by inserting unbound number of records. In this case, the simple solution
shown at the begin will do it.
If the table doesn’t use a foreign key constraint, you can implement an auxilliary
table, insert one row and lock this row instead of the masterrecord.
UNIX remains a fabulous choice for hosting Oracle databases. Both technologies have proven to
be more flexible and more stable for more time than most of us care to remember.
The remarkable 'Korn' shell, when coupled with the strength of the standard UNIX tool set, seems
to be able to extract the best from any programmer. While many Oracle customers shy away from
actively adopting and supporting UNIX development, most are perfectly happy to accept 'shell
script', and 'shell script' is all we need for many back-end applications. This article demonstrates
how SQL*Plus can be used in the same manner as any UNIX tool. And you will find that
interacting with an Oracle database via a 'shell script' program is simple, flexible and dynamic--
the UNIX way.
Background / Overview
SQL*Plus will happily integrate into the standard UNIX IO streams. The simplest way
to demonstrate this point is that the following UNIX command.
Note, that you first must start the Korn Shell (usually /bin/ksh), '$' is the Shell
Prompt.
/bin/ksh
$ print "select * from dual;" | sqlplus -s scott/tiger
D
-
X
Once you grasp this, the possibilities for using SQL*Plus from UNIX can become quite
exciting. You can construct and run Dynamic SQL or PL/SQL statements without
difficulty. There is also a mechanism available to a shell script programmer that
avoids commit or rollback processing between SQL*Plus transactions.
By using pipes to deliver SQL or PL/SQL statements to SQL*Plus, you can avoid the
use of temporary files and construct SQL or PL/SQL statements 'on-the-fly'. You can
also use pipes to collect the output generated from SQL*Plus (thereby avoiding
temporary files, once again), and interpret the output of pipes using the UNIX tool
set.
This article uses a simple UNIX file to Oracle table interface to demonstrate
UNIX/Oracle communications techniques.
If you want to retrieve TABLE_NAMES from ALL_TABLES where the table name is like
'ADD_' using the following query, you may notice that the query is returning
ADDRESS and ADD_CODES:
TABLE_NAME
----------
ADDRESS
ADD_CODES
If you try to escape the '_' character with the following query, you will still get the
same result.
select distinct table_name from all_tables
where table_name like 'ADD\_%'
Therefore the question is: How do you use LIKE to find data that contains an
underscore or percent sign ? The answer is to escape the underscore and/or percent
sign in the LIKE template. You have to designate what character you would like to
use via the ESCAPE keyword. A slash is commonly used, but any character would
actually work:
Most of us are familiar with the NVL function which checks for the existence of NULL
values. Oracle provides a new function called NVL2 which checks for the existence of
NOT NULL. The syntax for this function is as follows.
NVL2(expr1,expr2,expr3);
If expr1 is not null then the function will return expr2. Otherwise, the function will
return expr3. The expr1 can have any datatype and arguments expr2 and expr3 can
be of any datatype other than LONG. The datatype of the return value is that of
expr2.
Example:
The above query checks for the presence of NOT NULL in the COMM field, and
returns 1 where COMM is NOT NULL and 0 where it is NULL.
Here are the differences between the NVL and NVL2 functions:
o NVL returns the first argument if the first argument is not null, whereas NVL2
returns the second argument if the first argument is not null and returns the
third argument if the first argument is null.
o In NVL, the datatype of the return is that of the first argument, in NVL2 the
datatype of the return is that of the second argument.