Postgresql 将现有列添加到复合主键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36468950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Postgresql add existing column to composite primary key
提问by David Simic
I have a table in postgresql with a composite primary key. The primary key consists of two columns named:
我在 postgresql 中有一个带有复合主键的表。主键由两列组成:
DATETIME, UID
I have a another (non-null) column named ACTION already existing in this table. How do I add ACTION to the composite primary key? Ie: I'd like the resulting primary key of the table to be the triplet:
我有一个名为 ACTION 的另一个(非空)列已经存在于该表中。如何将 ACTION 添加到复合主键?即:我希望表的结果主键是三元组:
DATETIME, UID, ACTION
回答by Garrett
First drop the primary key constraint. You can get the name of the constraint by typing
首先删除主键约束。您可以通过键入来获取约束的名称
\d my_table
and look under indexes for something like:
并在索引下查找类似的内容:
"my_table_pkey" PRIMARY KEY, btree (datetime, uid)
Drop it by doing:
通过执行以下操作删除它:
alter table my_table drop constraint my_table_pkey;
Then create the new composite primary key by doing:
然后通过执行以下操作创建新的复合主键:
alter table my_table add constraint my_table_pkey primary key (datetime, uid, action);