title: Oracle SQL - Add Primary Key to table
body: <p>I have some columns with no primary key and <strong>want to add a primary key column</strong>.</p>
  
  <pre><code>NAME    Age
  -------------
  Peter   45
  Bob     25
  John    56
  Peter   45
  </code></pre>
  
  <p><strong><em>Some collegues suggest to add a PK with a sequences and triggers</em></strong>:
  <a href="https://stackoverflow.com/questions/11464396/add-a-auto-increment-primary-key-to-existing-table-in-oracle">Add a auto increment primary key to existing table in oracle</a></p>
  
  <p>This is nice, but <strong><em>my customers use a Database User with no rights to add sequences or triggers</em></strong>.
  I want to prevent to contact dozens of DBA administrators to alter user rights or to run my scripts.</p>
  
  <p><strong><em>This is my suggestion to add a PK with only an update statement: (I need help in Step 2)</em></strong></p>
  
  <p><em>Step 1:</em> Create the ID column (<em>I have DB rights for this</em>)</p>
  
  <pre><code>ALTER TABLE PERSON ADD ID NUMBER(10,0);
  </code></pre>
  
  <p><em>Step 2:</em> <strong>Question: Can I initialize the ID column with unique values based on the order of the rows or something else? How?</strong></p>
  
  <pre><code>UPDATE PERSON SET ID = something-unique
  </code></pre>
  
  <p><em>Step 3:</em> Add the primary key contraint afterwords: (<em>I DB have rights for this</em>)</p>
  
  <pre><code>ALTER TABLE PERSON ADD CONSTRAINT PK_ID PRIMARY KEY(ID);
  </code></pre>
  
  <p><em>Step 4:</em> Afterwords: the primary key is managed and added by my application.</p>
  
  <p>This will be the result:</p>
  
  <pre><code>ID(PK)  NAME    Age
  ---------------------
  1       Peter   45
  2       Bob     25
  3       John    56
  4       Peter   45
  </code></pre>
  
  <p>Thanks folks!</p>