Archive

Archive for the ‘Oracle’ Category

AutoNumber And Identity Functionality

April 13, 2011 Leave a comment

Developers who are used to AutoNumber columns in MS Access or Identity columns in SQL Server often complain when they have to manually populate primary key columns using sequences. This type of functionality is easily implemented in Oracle using triggers.

First we create a table with a suitable primary key column and a sequence to support it:

CREATE TABLE departments (
  ID           NUMBER(10)    NOT NULL,
  DESCRIPTION  VARCHAR2(50)  NOT NULL);

ALTER TABLE departments ADD (
  CONSTRAINT dept_pk PRIMARY KEY (ID));

CREATE SEQUENCE dept_seq;

Next we create a trigger to populate the ID column if it’s not specified in the insert:

CREATE OR REPLACE TRIGGER dept_bir 
BEFORE INSERT ON departments 
FOR EACH ROW
WHEN (new.id IS NULL)
BEGIN
  SELECT dept_seq.NEXTVAL
  INTO   :new.id
  FROM   dual;
END;
/

Finally we can test it using the automatic and manual population methods:

SQL> INSERT INTO departments (description)
  2  VALUES ('Development');

1 row created.

SQL> SELECT * FROM departments;

        ID DESCRIPTION
---------- --------------------------------------------------
         1 Development

1 row selected.

SQL> INSERT INTO departments (id, description)
  2  VALUES (dept_seq.NEXTVAL, 'Accounting');

1 row created.

SQL> SELECT * FROM departments;

        ID DESCRIPTION
---------- --------------------------------------------------
         1 Development
         2 Accounting

2 rows selected.

SQL>

The trigger can be modified to give slightly different results. If the insert trigger needs to perform more functionality than this one task you may wish to do something like:

CREATE OR REPLACE TRIGGER dept_bir 
BEFORE INSERT ON departments 
FOR EACH ROW
BEGIN
  SELECT NVL(:new.id, dept_seq.NEXTVAL)
  INTO   :new.id
  FROM   dual;
  
  -- Do more processing here.
END;
/

To overwrite any values passed in you should do the following:

CREATE OR REPLACE TRIGGER dept_bir 
BEFORE INSERT ON departments 
FOR EACH ROW
BEGIN
  SELECT dept_seq.NEXTVAL
  INTO   :new.id
  FROM   dual;
END;
/

To error if a value is passed in you should do the following:

CREATE OR REPLACE TRIGGER dept_bir 
BEFORE INSERT ON departments 
FOR EACH ROW
BEGIN
  IF :new.id IS NOT NULL THEN
    RAISE_APPLICATION_ERROR(-20000, 'ID cannot be specified');
  ELSE
    SELECT dept_seq.NEXTVAL
    INTO   :new.id
    FROM   dual;
  END IF;
END;
/

Hope this helps.

Back to the Top.

Categories: Oracle

Listener

June 14, 2010 Leave a comment

Single-Tier Architecture

The single-tier architecture dates back to the days of monolithic mainframes connected  by dumb terminals. The entire application comprising layers such as user interfaces,business rules, and data was collocated on the same physical host. The users interacted with these systems using terminals or consoles, which had very limited text-based processing capabilities.

Figure: Single-Tier Architecture

Two-Tier Architecture

2-tier architecture is used to describe client/server systems where the client requests resources and the server responds directly to the request, using its own resources. This means that the server does not call on another application in order to provide part of the service.

Figure: Two-Tier Architecture

Categories: Oracle

Redo Log File

June 14, 2010 Leave a comment


Redo Log File:

Redo Log file contains any changes made to the data in database buffer cache. Every
database should have at least two redolog files groups.
Check Redo Log file Status:
SQL> select group#,status from v$log;
GROUP# STATUS
———- —————-
1 CURRENT
2 INACTIVE
3 INACTIVE
The log files have the following status values:
Name Meaning
USED Indicates either that a log has just been added but never used.
CURRENT Indicates a valid log that is in use.
ACTIVE Indicates a valid log file that is not currently in use.
CLEARING Indicates a log is being re-created as an empty log due to DBA
action.
CLEARING
CURRENT

Means that a current log is being cleared of a closed thread. If a log
stays in this status, it could indicate there is some failure in the log
switch.
INACTIVE Means that the log is no longer needed for instance recovery but
may be needed for media recovery.
The v$logfile table has a status indicator that gives these additional codes:
Name Meaning
INVALID File is inaccessible.
STALE File contents are incomplete (such as when an instance is shut down
with SHUTDOWN ABORT or due to a system crash).
DELETED File is no longer used.
Null File in use.
Redo Log Buffer
LGWR
Online Redo Log file

2
Adding Redo Log Groups:

SQL> ALTER DATABASE ADD LOGFILE GROUP 4
‘C:\oracle\product\10.2.0\oradata\dba12\REDO04.LOG’
SIZE 10M;
Adding Redo Log Members:
SQL> ALTER DATABASE ADD LOGFILE MEMBER
‘C:\oracle\product\10.2.0\oradata\dba12\REDO04b.LOG’ TO GROUP 4;
Check the file Location of redo log files:
SQL> select group#,member from v$logfile;
GROUP# MEMBER
————————————————————-
3 C:\ORACLE\PRODUCT\10.2.0\ORADATA\DBA12\REDO03.LOG
2 C:\ORACLE\PRODUCT\10.2.0\ORADATA\DBA12\REDO02.LOG
1 C:\ORACLE\PRODUCT\10.2.0\ORADATA\DBA12\REDO01.LOG
4 C:\ORACLE\PRODUCT\10.2.0\ORADATA\DBA12\REDO04.LOG
4 C:\ORACLE\PRODUCT\10.2.0\ORADATA\DBA12\REDO04B.LOG
Dropping Online Redo Log Member:
SQL> ALTER DATABASE DROP LOGFILE MEMBER
‘C:\oracle\product\10.2.0\oradata\dba12\REDO04B.LOG’;
Dropping Online Redo Log Groups:
SQL> ALTER DATABASE DROP LOGFILE GROUP 4;
Move Redo Log File Destinations
1. SQL>SHUTDOWN;
2. Copy the redo log file in new location.
3. SQL> STARTUP MOUNT;
4. SQL> ALTER DATABASE RENAME
FILE ‘C:\oracle\product\10.2.0\oradata\dba12\REDO01.LOG’
TO ‘C:\oracle\product\10.2.0\oradata\dba12\redologfile\REDO01.LOG’;
5. SQL> alter database open;
Forcing Log Switch:
SQL> ALTER SYSTEM SWITCH LOGFILE;
Forcing Checkpoint:
SQL> ALTER SYSTEM CHECKPOINT;

Categories: Oracle

PFile and SPFile

June 14, 2010 Leave a comment

PFILE:
PFILE is a text file which can be modified by text editor. Oracle server required PFILE to
start an oracle Instance.
SPFILE:
SPFILE is a binary file which can not be modified by text editor. It is created from
PFILE.
How the Oracle Instance is initialized
When the Oracle instance start, first it looks to the $ORACLE_HOME/dbs
(UNIX, Linux) or ORACLE_HOME/database (Windows) directory for the
following files (in this order):
1. spfileSID.ora (SPFILE)
2. Default SPFILE (SPFILE)
3. initSID.ora (PFILE)
4. Default PFILE (PFILE)
SPFILE advantages
1. No need to restart the database in order to have a parameter changed and
the new value stored in the initialization file
2. Reduce human errors: Parameters are checked before changes are
accepted
3. An SPFILE can be backed-up with RMAN (RMAN cannot backup
PFILEs)
How could I switch from SPFILE to PFILE and vice-versa?
Switch from SPFILE to PFILE:
1) CREATE PFILE FROM SPFILE;
2) Backup and delete SPFILE
3) Restart the instance
Switch from PFILE to SPFILE:
1) CREATE SPFILE FROM PFILE=’Location of the PFILE’;
2) Restart the instance (the PFILE will be in the same directory but will not be
used. SPFILE will be used instead)
Converting SPFILE to PFILE and vice-versa
This could be done in order to have a backup in the other format or to
change the initialization file for the database instance.
CREATE PFILE FROM SPFILE;
CREATE SPFILE FROM PFILE=’/oradata/initORCL.ora’;
CREATE SPFILE = ‘/oradata/spfileORCL.ora’ FROM PFILE = ‘/oradata/initORCL.ora’ ;

Categories: Oracle
Follow

Get every new post delivered to your Inbox.