Creating primary key values with trigger

From: Pedro Pimentel <ppf_at_fct.unl.pt>
Date: Wed, 7 Sep 1994 23:17:16 GMT
Message-ID: <1994Sep7.231716.3493_at_fct.unl.pt>


  I think it would be nice if oracle could create primary key values automagically (like 'counter' fields in MS Access). I was thinking about something like this:

create table myTable (

  ID         number(5)   CONSTRAINT pk_myTable PRIMARY KEY,
  field1     char(30),

  ....
) ;

create sequence seq_myTable ;

create or replace trigger check_myTable
  before insert
  on myTable
  for each row
begin
  :new.ID := seq_myTable.nextval ;
end ;

  This doesn't work because Oracle checks for null values in the ID field before running the trigger.

  This way I have to use a default value (usually 0) in the ID field in the form application (I'm using MS Access, much better than SQL*Forms :) ) so that oracle passes by the not null check and then runs the trigger.

  Any other ideia, comments ?

--
Pedro Pimentel
e-mail: ppf_at_fct.unl.pt
www:    http://ester.fct.unl.pt/pedro1.htm
Received on Thu Sep 08 1994 - 01:17:16 CEST

Original text of this message