I want to create serial numbers in oracle

3.6k Views Asked by At

I want to create a serial number in Oracle. I have tried this query

alter table tablename add(ID NUMBER);

CREATE SEQUENCE SEQ_ID START WITH 1 INCREMENT BY 1 MAXVALUE 31611805 MINVALUE 1 NOCYCLE;

UPDATE tablename SET ID= SEQ_ID.NEXTVAL 

I have,

NID
----- 
ABD90
BGJ89
HSA76

and I want:

ID NID
---------
1  ABD90 
2  BGJ89
3  HSA76

Above code is not working. I am new to oracle.Please help in generating above result.

1

There are 1 best solutions below

0
On

If you want to add unique ID for each NID then, you can do it this way-

  UPDATE tablename t
       SET t.ID =
              (SELECT SEQ_ID.NEXTVAL
                 FROM tablename sub_t
                WHERE t.NID = sub_t.NID);