我可以在PostgreSQL表创建后添加UNIQUE约束吗? [英] Can I add a UNIQUE constraint to a PostgreSQL table, after it's already created?

查看:2536
本文介绍了我可以在PostgreSQL表创建后添加UNIQUE约束吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下表:

 tickername | tickerbbname  | tickertype
------------+---------------+------------
 USDZAR     | USDZAR Curncy | C
 EURCZK     | EURCZK Curncy | C
 EURPLN     | EURPLN Curncy | C
 USDBRL     | USDBRL Curncy | C
 USDTRY     | USDTRY Curncy | C
 EURHUF     | EURHUF Curncy | C
 USDRUB     | USDRUB Curncy | C

我不希望任何给定<$ c $的列数超过一列c> tickername / tickerbbname 对。我已经创建了表并且其中包含大量数据(我已经确保它符合唯一标准)。然而,随着它变得越来越大,错误的空间也在蔓延。

I don't want there to ever be more than one column for any given tickername/tickerbbname pair. I've already created the table and have lots of data in it (which I have already ensured meets the unique criteria). As it gets larger, though, room for error creeps in.

有没有办法添加 UNIQUE 约束此时?

Is there any way to add a UNIQUE constraint at this point?

推荐答案

psql 的内联帮助:

\h ALTER TABLE

还有 postgres docs (一个优秀的资源,加上易于阅读)。

Also documented in the postgres docs (an excellent resource, plus easy to read, too).

ALTER TABLE tablename ADD CONSTRAINT constraintname UNIQUE (columns);

这篇关于我可以在PostgreSQL表创建后添加UNIQUE约束吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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