SQL 如何更改 PostgreSQL 表并使列唯一?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/469471/
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
How do I ALTER a PostgreSQL table and make a column unique?
提问by Baishampayan Ghose
I have a table in PostgreSQL where the schema looks like this:
我在 PostgreSQL 中有一个表,其中的架构如下所示:
CREATE TABLE "foo_table" (
"id" serial NOT NULL PRIMARY KEY,
"permalink" varchar(200) NOT NULL,
"text" varchar(512) NOT NULL,
"timestamp" timestamp with time zone NOT NULL
)
Now I want to make the permalink unique across the table by ALTER-ing the table. Can anybody help me with this?
现在我想通过 ALTER-ing 表格使永久链接在整个表格中独一无二。有人可以帮我解决这个问题吗?
TIA
TIA
回答by Baishampayan Ghose
回答by Clint Pachl
Or, have the DB automatically assign a constraint name using:
或者,让数据库使用以下命令自动分配约束名称:
ALTER TABLE foo ADD UNIQUE (thecolumn);
回答by Stefan
it's also possible to create a unique constraint of more than 1 column:
也可以创建多于 1 列的唯一约束:
ALTER TABLE the_table
ADD CONSTRAINT constraint_name UNIQUE (column1, column2);