Search code examples
sqloracle

How to create a unique id for each insert in Oracle


I would like to have a unique ID for each INSERT. All rows for each INSERT must have the same ID, but different to the other IDs in the table. Is any standard way to do that in ORACLE?


Solution

  • You can use sequence in Oracle :

    CREATE SEQUENCE my_sq START WITH 1 INCREMENT BY 1;
    

    Then when you want to insert, use my_sq.NEXTVAL in the place of your id value.

    E.g

    INSERT INTO table(id,name) VALUES (my_sq.NEXTVAL, 'John')
    

    More info about SEQUENCE