Use trigger to create autoincrement column : Before Insert Trigger « Trigger « Oracle PL / SQL






Use trigger to create autoincrement column

    
SQL>
SQL> CREATE TABLE myTable (
  2     myTableID INT PRIMARY KEY NOT NULL,
  3     Name     VARCHAR(50),
  4     PhoneNo  VARCHAR(15) DEFAULT 'Unknown Phone');

Table created.

SQL>
SQL> CREATE SEQUENCE SEQ;



SQL>
SQL> CREATE OR REPLACE TRIGGER AUTONUMBER
  2  BEFORE INSERT ON myTable
  3  FOR EACH ROW
  4  BEGIN
  5     SELECT SEQ.NEXTVAL
  6     INTO :NEW.myTableID FROM DUAL;
  7  END;
  8  /

Trigger created.

SQL>
SQL>
SQL> drop table myTable;

Table dropped.

   
    
    
    
  








Related examples in the same category

1.Create a BEFORE INSERT trigger
2.Get system time in a before insert trigger
3.Change letter case in a trigger