Since your post mentions an artificial key, I assume you mean a surrogate key. I further assume that you want an integer value.
The answer is to do a Create Table As Select and use the pseudo column rownum to provide the key.
If you use the dept table in the example SCOTT schema, the following works:-
Code:
CREATE TABLE DEPT_WITH_KEY
AS
SELECT DEPTNO, DNAME,LOC,ROWNUM NEW_KEY
FROM DEPT;
Basically you create the new table by selecting everything from the existing one and including ROWNUM. Once this has succeeded,drop the original table, and rename the new one to be the same name as the original.
Original table is
Code:
DEPTNO,DNAME,LOC
10,ACCOUNTING,NEW YORK
20,RESEARCH,DALLAS
30,SALES,CHICAGO
40,OPERATIONS,BOSTON
Why do you need to add an artificial key. Can you explain a bit more about what the business and/or technical requirment for this is? If you absolutely need to do this then Thargs answer (or a variation of it) is the way to go.
But as the illustrious Mr Kyte would say:
The fastest way to do something is ... not to do it in the first place.
In order to understand recursion, you must first understand recursion.
alternatively, if space is an issue and your partitions aren't too big individually, you add the new surrogate key column to the table, create a new empty table with the same columns and then do a partition exchange to the new table. You could then use a combination of cursors, varrays/bulk collects and forall loops with a sequence to add the new key. Finally, partition exchange it back into the master table. All depends on maintenance slots I guess
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.