Search code examples
sqldatabaseoracle-databaseauto-incrementrownum

Is the use of rownum safe to update a table with incremented IDs on Oracle?


Similar to MySql I want update (fill) an empty column with incremental values -- with Oracle. I.e. after

ALTER TABLE data ADD
(
  id  number
);

I want for all records the ID column to receive unique values. After that I will enable Not Null and unique constraints to make it a primary key.

I came up quickly with

UPDATE TABLE data SET id = rownum;

but I have a bad feeling about this. It works in my tests as expected, but an example is no proof :-)

Is it safe to use rownum in this manner in an update-statement?


Solution

  • Yes, I've never had a problem with that method, but enabling not null and unique constraints does not make it a primary key -- adding a primary key constraint makes it a primary key ;)