Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.misc -> Re: Increament
On Mon, 29 Oct 2001 12:02:22 +0800, Oliver Wong
<oliverwong_at_aforce.com> wrote:
>How can I create a field that will increament by n?
>Thank you!
>
>
>
Most probably you want to use a SEQUENCE and a TRIGGER.
Check out the CREATE SEQUENCE command. By default it starts at one and increments by one, but you can change both of these.
The CREATE TRIGGER command can create a trigger that will automatically put the number in the column.
An example:
CREATE TABLE MyTable (Id NUMBER PRIMARY KEY);
CREATE SEQUENCE MyTableSEQ NOCACHE ORDER;
CREATE OR REPLACE TRIGGER MyTableANR
BEFORE INSERT ON MyTable
FOR EACH ROW
BEGIN
SELECT MyTableSEQ.NEXTVAL INTO :NEW.Id FROM Dual;
END MyTableANR;
/
Brian Received on Sun Oct 28 2001 - 21:27:19 CST