PL/SQL code for an Oracle trigger that will auto-increment specified
field:
create or replace trigger <trigger>
before insert on <table>
for each row
begin
if :new.<field> is null then
select <sequence-name>.nextval into :new.<field> from dual;
end if;
end;
You have to create a sequence first. Example:
CREATE SEQUENCE <sequence-name>
INCREMENT BY 1
START WITH -99999999999999999999999999
MAXVALUE 9999999999999999999999999999999999999999999999
MINVALUE -999999999999999999999999999999999999999999999
CYCLE CACHE 5 ORDER;
Of course, replace values in <xxx> as needed.
If you use the aboce code verbatim, you will a field incrementing from
-99999... until 99999 and then it will begin all over again.
-Stathis.
Sebastian Stadtlich wrote:
>
> oracle does not have an autoincrement.
> it has the more powerfull concept of TRIGGERS
> unfortunatelly you have to write them in pl/sql or java...
> have a look on the net for oracle trigger tutorial or
> you can
> find a super oracle-feature-documentation here :
> http://conf.php.net/oci2
> http://conf.php.net/pres/slides/oci/paper.txt
>
> which features a small non-trigger-work-around
>
> sebastian
>
> > -----Urspr&oacgr;ngliche Nachricht-----
> > Von: Duy B [mailto:[EMAIL PROTECTED]]
> > Gesendet: Dienstag, 10. Juli 2001 04:36
> > An: [EMAIL PROTECTED]
> > Betreff: [PHP] Oracle question
> >
> >
> > Dear all,
> > If i want to use id field in Oracle database, how can i do?
> > For example, in MySQL
> > create table test (
> > id int not null auto-increment primary key,
> > ten char(10));
> >
> > So that, when i insert a new row into MySQL database, id
> > increases by 1.
> >
> > But in Oracle i couldn't use as that.
> > Somebody could help me.
> > Thanks you all,
> > BaoDuy
> >
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]