Add a field to a composite primary key

In a MySQL database, I have a table already created with several fields.  Let's say field1 and field2 make up my composite primary key.  What command would I enter to add a third field to my composite primary key?  My end result would mean that my composite primary key is now field1, field2 and field3.  I want to be able to do this without having to delete and recreate the entire table... I've already got a ton of data in there!

These are the 2 queries I've tried:

1.  alter table table_name add NEW_field int primary key after field2;

2.  alter table table_name add primary key (EXISTING_field);

In the first query, I try adding a new field to the table and making it part of the composite key.  In the second query, I try adding an existing field to the composite key.  Both of these queries fail - I get the error : "ERROR 1068 (42000): Multiple primary key defined".
a2hITAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Nick UpsonPrincipal Operations EngineerCommented:
I think you need to drop the primary key, then add it again with the new definition, so
 
alter table table_name drop primary key;
alter table table_name add primary key (field1, field2, field3);
 
 

 
0
a2hITAuthor Commented:
I didn't think that a table could exist in MySQL without having a primary key, but what you say makes sense, so I tried it.  The first statement failed though and returned this error:

ERROR 1025 (HY000): Error on rename of '.\a2h\#sql-700_7' to '.\a2h\phonecharges' (errno: 150)

At this point i can tell you that 'a2h' is the name of my database, and 'phonecharges' is the name of the table I'm working with.
0
UmeshMySQL Principle Technical Support EngineerCommented:
This should work...

ALTER TABLE phonecharges  DROP PRIMARY KEY, ADD PRIMARY KEY  (`field1`,`field1`,`field1`);

BTW, , if an ALTER TABLE fails and it refers to errno 150, that means a foreign key definition would be incorrectly formed for the altered table.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
UmeshMySQL Principle Technical Support EngineerCommented:
I just tried.. its working...


 CREATE TABLE `phonecharges` (                                
             `ID` int(10) unsigned NOT NULL auto_increment,          
             `FK` int(10) default NULL,                              
             `date` datetime NOT NULL,                               
             PRIMARY KEY  (`ID`,`FK`)                                     
           );
 
ALTER TABLE phonecharges DROP PRIMARY KEY, ADD PRIMARY KEY  (`ID`,`FK`,`date`);
 
 
 
show create table phonecharges;
 
CREATE TABLE `phonecharges` (
  `ID` int(10) unsigned NOT NULL auto_increment,
  `FK` int(10) NOT NULL default '0',
  `date` datetime NOT NULL,
  PRIMARY KEY  (`ID`,`FK`,`date`)
)

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.