Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.misc -> Re: Such a thing as auto increment in oracle?
Nicholas Carey wrote:
>
> On 05 Nov 2001, Sybrand Bakker <postbus_at_sybrandb.demon.nl>
> spake and said:
>
> > On Mon, 05 Nov 2001 21:48:18 +1100, Chris Newman
> > <chris_newman_at_bigpond.com> wrote:
> >
> >>I am using Oracle version 7 for Windows 95 and want to
> >>increment values in a column one at a time starting with 1,
> >>2, 3 etc to serve as a primary key. MySQL calls this feature
> >>auto-increment. How do I do this please
> >
> > In Oracle you can create auto increment number as an object
> > called 'sequence'.
> >
> > First create a sequence,
> >
> > SQL> create sequence my_seq;
> >
> > ...
>
> Or you can do it in the more 'relationally correct' way:
>
> create table foo
> ( id int not null primary key ,
> col_1 varchar2(64) not null
> )
>
> insert into foo ( id , col_1 )
> select max(id)+1 id ,
> 'you-value-here' col_1
> from foo
>
> As they say in the land of Perl, "TMTOWTDI" (There's
> more than one way to do it).
> --
Yes, TMTOWTDI, but the way above with the SQL statement will not always
result in a new unique value. Consider using a PRE-INSERT trigger along
with a sequence for the table - see the PL/SQL user's guide. This will
guarantee unique values.
-- Ron Reidy Oracle DBA Reidy Consulting, L.L.C.Received on Mon Nov 05 2001 - 23:13:26 CST
![]() |
![]() |