how to add new column to existing composite primary key
I have encountered a problem in that I already have a composite primary key in a MYSQL table. But now I have added another column to that table and due to some requirement changes, I have to modify that composite primary key in such a way that 开发者_开发知识库I need to add that previously mentioned column to that composite primary key list. Can anyone tell me how to alter that table without dropping existing composite primary key. I am doing this in a Rails project
You can't alter the primary key. You have to drop and re-add it:
ALTER TABLE MyTable
DROP PRIMARY KEY,
ADD PRIMARY KEY (old_col1, old_col2, new_col);
but if a key no exist? example:
ALTER TABLE xxxx ADD id INT NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY(id,id2,id3);
精彩评论