Home » Other » Client Tools » How to create a auto indent primary key ?
How to create a auto indent primary key ? [message #38688] Mon, 06 May 2002 07:52 Go to next message
hug
Messages: 6
Registered: May 2002
Junior Member
i want to create a primary key that indent himself (without specifing something).
for example : i want that the first time I insert a record in the table, the primary key is set to 1.
When I insert a second record, the corresponding primary key must be set to 2 automatically.

How can I do that thing in SQL or with the Oracle Schema Manager application ?

Thanks to respond ...

hug
Re: How to create a auto indent primary key ? [message #38690 is a reply to message #38688] Mon, 06 May 2002 11:55 Go to previous message
Todd Barry
Messages: 4819
Registered: August 2001
Senior Member
The usual approach is to use a combination of a sequence and a trigger. Keep in mind though that a sequence does not guarantee a gap-free set of numbers, just unique values.

create sequence sequence_name;  -- check docs for various parameters on this command
 
create or replace trigger trigger_name
  before insert on table_name
  for each row
begin
  if :new.pk_column is null then
    select sequence_name.nextval
      into :new.pk_column
      from dual;
  end if;
end;
/
Previous Topic: substr
Next Topic: How to use IN parameters with in the begin statement..
Goto Forum:
  


Current Time: Thu Mar 28 18:17:33 CDT 2024