| Oracle FAQ | Your Portal to the Oracle Knowledge Grid | |
|  |  | |||
Home -> Community -> Usenet -> c.d.o.misc -> Re: HELP: Sequences and primary keys beginner question.
Here is a very basic trigger.
assuming table EMP is:
PK   number(12,0);
LAST_NAME varchar2(20);
FIRST_NAME varchar2(20);
and emp_sequence is our sequence generator
create or replace trigger emp_pk_trg
before insert of em
for each row
pk_seq emp.pk%type;
begin
select emp_sequence.nextval into pk_seq from dual; :new.pk := pk_seq; end;
/
On Sat, 21 Jun 1997 00:25:09 GMT, asdg_at_sdf.lkj (s) wrote:
>Hi, 
>        I realize that this question must have been asked many times.  
>However, I have looked at all the FAQ pages I can find and have looked through 
>many manuals for many hours with no luck, so I turn to you.
>        I want to use a sequence to create the next number for a primary key 
>field.  All the manuals indicate that this is 'easy' or 'convenient', but none 
>of them tell me how.
>        I know that I cannot make reference to the sequence's NEXTVAL in the 
>DEFAULT column in the CREATE TABLE command (though this amazes me).
>        I know how to write a trigger that will fire on an INSERT statement 
>and populate the keyed field with the NEXTVAL.  I know that I can also include 
>the NEXTVAL in every insert command I ever perform against a table.
>
>        I just can't believe that I have to do this for every table that I 
>want to have an autoincrementing primary key!  There's got to be a better way, 
>this just seems incredibly laborious.
>
>        Any help would be greatly appreciated.  Please reply to my email 
>address below (after modification).
>
>Thank you,
>
>        
>        
>
>________________________________________________
>Ben Harmon
>ben_at_dascom.com.nospam
>**Please remove .nospam suffix if you want to mail me**
>________________________________________________
Received on Mon Jun 23 1997 - 00:00:00 CDT
|  |  |