如何将新列添加到现有的组合主键 [英] how to add new column to existing composite primary key

查看:72
本文介绍了如何将新列添加到现有的组合主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,因为我在MYSQL表中已经有一个复合主键.但是现在我在该表中添加了另一列,并且由于某些要求更改,我不得不以某种方式修改该复合主键,以至于需要将先前提到的列添加到该复合主键列表中.谁能告诉我如何在不删除现有复合主键的情况下更改该表.我在Rails项目中这样做

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);

这篇关于如何将新列添加到现有的组合主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆