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 ^_^
You can't add two primary keys but you can add one composite key comprised of both columns. - alter table TEST_T_NEW
-
add constraint PK_TEST_ID_T primary key (TEST_ID, TEST_FLAG)
-
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
You mean change what the primary key is? Yes you can do that. Just make sure your foreign keys are updated.
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.
You can't add two primary keys but you can add one composite key comprised of both columns. - alter table TEST_T_NEW
-
add constraint PK_TEST_ID_T primary key (TEST_ID, TEST_FLAG)
-
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.
"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?
You're not changing anything that would affect the records.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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) ,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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);...
|
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
|
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...
|
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...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
|
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...
| |