473,327 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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 29943
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

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

Similar topics

7
by: Jon Combe | last post by:
I have created the following test SQL code to illustrate a real problem I have with some SQL code. CREATE TABLE JCTable ( CustomerName varchar(50) ) ALTER TABLE JCTable ADD CustomerNo int...
3
by: Ben | last post by:
Hi everyone, Now I am using Visual Basic.net to write a program but having some headaches. The situation is as follows: I am using ODBC connection to connect one database( non-SQL server) ,...
21
by: Dan | last post by:
Hi, just ran into my first instance of a backend Access97 database not compacting. I'm getting the "MSACCESS.EXE has generated errors.." message on compact. I've narrowed it down to the largest...
5
by: Adam W. Saxton | last post by:
We have a few existing stored procedures which create a Global Temporary Table (##), do some work on the table and then delete the table. The issue we have is that if our Server application is...
1
by: HandersonVA | last post by:
There is itemlookup table, which stores item number and item description. Also there is a child table, pricehistory, of the itemlookup table, which stores different prices and different date...
3
by: angelnjj | last post by:
I'm going to do my best to describe purpose and what I need...here goes. I'm writing a data entry "quality" report and working. I'm trying to identify anytime a user adds a new contact to the db...
6
by: erosche | last post by:
Hi... I'm the new comer in this scripts network. Also quite new in SQL coding. I would like to ask some sql scripts. Eg. I have a table as belows : Item Warehouse...
3
by: Gison | last post by:
I try to create a table from another existed table but always error. Please help, does any one know what's wrong in my sentence? ======= create table item_bak as (select * from item);...
9
by: Miro | last post by:
VS2008 I have created 3 tables. Vendors Customers PhoneNumbers each have their own key Vendor has: VendorID - int unique identifier Customer has: CustomerID - int unique identifier
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.