Home » RDBMS Server » Server Administration » Create table with auto increment column - HELP
Create table with auto increment column - HELP [message #371350] Fri, 06 October 2000 02:13 Go to next message
Martin Johansson
Messages: 11
Registered: October 2000
Junior Member
Hi.. I want to create a table that has a column that auto increments every time I insert a row. How do I write this?

/Martin
Re: Create table with auto increment column - HELP [message #371352 is a reply to message #371350] Fri, 06 October 2000 02:43 Go to previous messageGo to next message
Prem
Messages: 79
Registered: August 1998
Member
Martin,
If i got your question right, you want to create a table having a column which increments its value when records are inserted into it. Something like Autonumber?

You'll have to use database trigger then. Nothing to do with table. The table creation syntax remains the same.

Example.
Create table auto_test (id number, name varchar2(100));

create sequence seq_auto_test start with 1;

create or replace trigger trg_auto_test
before insert on auto_test
for each row
begin
select seq_auto_test.nextval into :new.id
from dual;
end;
/

now if you issue an insert like

Insert into auto_test (name) values ('Hello1');
Insert into auto_test (name) values ('Hello2');
Insert into auto_test (name) values ('Hello3');
Insert into auto_test (name) values ('Hello4');

Select * from auto_test;

it should show 1, 2, 3, 4 for the 4 rows resp.

hth

Prem :)
Re: Create table with auto increment column - HELP [message #371819 is a reply to message #371350] Mon, 11 December 2000 01:55 Go to previous message
Madhavi
Messages: 23
Registered: November 1999
Junior Member
fist create a table and then you want the values to be inserted automatically for this u can make use of the sequence.

inesert into tablename values
(s1.eno,&ename,&sal...)
this should sove your problem
Previous Topic: Assign Rollback segments in PL/SQL
Next Topic: What's wrong with this procedure
Goto Forum:
  


Current Time: Fri May 17 02:03:15 CDT 2024