Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2003
    Posts
    7

    Unanswered: Dynamic key generation

    Apologies for the 'newbie' nature of this question, but I want to automatically assign the primary key of a tuple to the max + 1 of the primary keys allocated so far in the table (they're number variables). Can I do this automatically through a trigger, if so how, or is there some other way to do it.

    Cheers.

  2. #2
    Join Date
    Oct 2003
    Location
    Slovakia
    Posts
    482

    Re: Dynamic key generation

    Originally posted by smythst
    Apologies for the 'newbie' nature of this question, but I want to automatically assign the primary key of a tuple to the max + 1 of the primary keys allocated so far in the table (they're number variables). Can I do this automatically through a trigger, if so how, or is there some other way to do it.

    Cheers.
    CREATE SEQUENCE name_of_sequence
    INCREMENT BY 1
    START WITH 1
    MINVALUE 1
    MAXVALUE 99999999999999
    NOCYCLE
    NOORDER
    NOCACHE
    /


    CREATE TRIGGER name_of_triger
    BEFORE INSERT
    ON table_name
    REFERENCING NEW AS NEW OLD AS OLD
    FOR EACH ROW
    DECLARE ln_num NUMBER;
    BEGIN
    SELECT name_of_sequence.nextval into ln_num FROM dual;
    :new.ID:=ln_num;
    End;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •