Use Sequence in a trigger : Create Trigger « Trigger « Oracle PL / SQL






Use Sequence in a trigger

   

SQL>
SQL> create table foo(
  2    id number primary key,
  3    data varchar2(100) );

Table created.

SQL>
SQL> create sequence foo_seq;

Sequence created.

SQL>
SQL> create or replace trigger myTrigger
  2    before insert
  3       on foo
  4    for each row
  5  begin
  6    select foo_seq.nextval
  7      into :new.id
  8    from dual;
  9  end;
 10  /

Trigger created.

SQL>
SQL> insert into foo ( data ) values ( 'Christopher' );

1 row created.

SQL>
SQL> insert into foo ( id, data )values ( 5, 'Sean' );

1 row created.

SQL>
SQL> select * from foo;

        ID DATA
---------- ----------------------------------------------------------------------------------------------------
         1 Christopher
         2 Sean

SQL>
SQL> drop table foo;

Table dropped.

SQL> drop sequence foo_seq;

Sequence dropped.

SQL>
SQL>

   
    
  








Related examples in the same category

1.create or replace trigger
2.Oracle's syntax for creating a trigger based on two tables
3.Trigger on each row
4.Empty trigger(before insert or update or delete)
5.Create tigger on wrapper table
6.This trigger sends messages over a pipe to record inserts into myStudent.
7.Cascade inserts into myStudent into session and lecturer.
8.Trigger is autonomous and hence the changes will be logged even if the original transaction rolls back.
9.Trigger Which Modifies a Mutating Table
10.Creating a Trigger with cursor inside
11.Autonumbering Trigger
12.Use RAISE_APPLICATION_ERROR in a trigger
13.Show errors for a trigger
14.Submit job from a trigger
15.Use sysdate and user function in a trigger