Unix Technical Forum

How to add a primary key column

This is a discussion on How to add a primary key column within the Oracle Miscellaneous forums, part of the Oracle Database category; --> I have a Table without a primary key and want to add a primary key. The problem is: how ...


Go Back   Unix Technical Forum > Database Server Software > Oracle Database > Oracle Miscellaneous

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-07-2008, 09:47 AM
Ralf Zwanziger
 
Posts: n/a
Default How to add a primary key column

I have a Table without a primary key and want to add a primary key.
The problem is: how to update the existing columns with unique number
values? I have the table with a column PK_Table and a sequence
SEQ_Table. I thought the following statement would do it:

Update Table set PK_Table = (select SEQ_Table.NextVal from dual)

but this only results in ORA-02287: Sequence not allowed here.
Any idea how to solve the problem, maybe with a cursor in PL/SQL?

Thanks,
Ralf

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-07-2008, 09:47 AM
sybrandb@yahoo.com
 
Posts: n/a
Default Re: How to add a primary key column

Ralf Zwanziger <goldensurfer@gmx.de> wrote in message news:<ims4gv4ccmpsb7eoeh4mu86i2knatu93p6@4ax.com>. ..
> I have a Table without a primary key and want to add a primary key.
> The problem is: how to update the existing columns with unique number
> values? I have the table with a column PK_Table and a sequence
> SEQ_Table. I thought the following statement would do it:
>
> Update Table set PK_Table = (select SEQ_Table.NextVal from dual)
>
> but this only results in ORA-02287: Sequence not allowed here.
> Any idea how to solve the problem, maybe with a cursor in PL/SQL?
>
> Thanks,
> Ralf


CURSOR FOR LOOP
BEGIN
for i in (select * from table for update of pk_table) loop
update table
set pk_table = (select seq_table.nextval from dual)
where current of i;
end loop;
end;
/

and that is all. Homework!

Sybrand Bakker
Senior Oracle DBA
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-07-2008, 09:48 AM
Daniel Morgan
 
Posts: n/a
Default Re: How to add a primary key column

Ralf Zwanziger wrote:

> I have a Table without a primary key and want to add a primary key.
> The problem is: how to update the existing columns with unique number
> values? I have the table with a column PK_Table and a sequence
> SEQ_Table. I thought the following statement would do it:
>
> Update Table set PK_Table = (select SEQ_Table.NextVal from dual)
>
> but this only results in ORA-02287: Sequence not allowed here.
> Any idea how to solve the problem, maybe with a cursor in PL/SQL?
>
> Thanks,
> Ralf


1. Rename the table
2. CREATE TABLE <original_table_name> AS
SELECT sequence_name.NEXTVAL AS column_alias, field_name, field_name
FROM renamed_table;

--
Daniel Morgan
http://www.outreach.washington.edu/e...ad/oad_crs.asp
damorgan@x.washington.edu
(replace 'x' with a 'u' to reply)


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:57 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com