469,292 Members | 1,417 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,292 developers. It's quick & easy.

Can I add another PK to the existed table?

4
Hi,here's the thing:
I have an existed table(many records have been inserted into it) which already has a PK, but now I want to insert records with the same id, can I just add another PK to realize this?
I hope someone can help me out.thanks ^_^
Dec 12 '11 #1

✓ answered by Rabbit

You can't add two primary keys but you can add one composite key comprised of both columns.
Expand|Select|Wrap|Line Numbers
  1. alter table TEST_T_NEW 
  2. add constraint PK_TEST_ID_T primary key (TEST_ID, TEST_FLAG)
  3. using index ;
It will have no effect on the old table because it's a separate table from the new table. The new table you create is not linked in any way to the old table. And as long as the fields comprising the new composite key is unique, you can even insert the records from the old table into the new table. But if that was your goal, there's no need to create a new table. You could just drop the primary key and create your new one on the same table.

6 21831
Rabbit
12,516 Expert Mod 8TB
You mean change what the primary key is? Yes you can do that. Just make sure your foreign keys are updated.
Dec 12 '11 #2
LM2011
4
First, thank you for your reply.Well, I mean I want to change the one PK table into a two-PK table. By the way, I already built an index on the column which has the primary key constraint. Can I still do that?
For example:
create table TEST_T_OLD
(
TEST_ID VARCHAR2(56) not null ,
TEST_NAME VARCHAR2(130),
... ,
TEST_FLAG VARCHAR2(64)
)
alter table TEST_T_OLD
add constraint PK_TEST_ID_T primary key (TEST_ID)
using index ;
-------------Now I want to make a few changes to TEST_T_OLD so that ---it will look like this:
create table TEST_T_NEW
(
TEST_ID VARCHAR2(56) not null ,
TEST_NAME VARCHAR2(130),
... ,
TEST_FLAG VARCHAR2(64) not null
)
alter table TEST_T_NEW
add constraint PK_TEST_ID_T primary key (TEST_ID)
using index ;

alter table TEST_T_NEW
add constraint PK_TEST_FLAG_T primary key (TEST_FLAG).

The problem is that TEST_T_OLD already stored many records before, and I don't know what effect that the above mentioned change will have on them..
Looking forward to your reply. Thank you.
Dec 12 '11 #3
Rabbit
12,516 Expert Mod 8TB
You can't add two primary keys but you can add one composite key comprised of both columns.
Expand|Select|Wrap|Line Numbers
  1. alter table TEST_T_NEW 
  2. add constraint PK_TEST_ID_T primary key (TEST_ID, TEST_FLAG)
  3. using index ;
It will have no effect on the old table because it's a separate table from the new table. The new table you create is not linked in any way to the old table. And as long as the fields comprising the new composite key is unique, you can even insert the records from the old table into the new table. But if that was your goal, there's no need to create a new table. You could just drop the primary key and create your new one on the same table.
Dec 12 '11 #4
LM2011
4
"You could just drop the primary key and create your new one on the same table". I can drop the primary key and create a new composite key on the same table(the old one), and just start to insert new records, and this do not affect the old records, right?
Dec 13 '11 #5
Rabbit
12,516 Expert Mod 8TB
You're not changing anything that would affect the records.
Dec 13 '11 #6
LM2011
4
Thank you very much^_^
Dec 13 '11 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by Jon Combe | last post: by
21 posts views Thread by Dan | last post: by
5 posts views Thread by Adam W. Saxton | last post: by
1 post views Thread by HandersonVA | last post: by
9 posts views Thread by Miro | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.