[英]Check constraint on existing column with PostgresQL
我正在尝试对现有列设置检查约束。
有什么办法可以从 PostgreSQL 实现这一点吗?
使用alter table
添加新约束:
alter table foo
add constraint check_positive check (the_column > 0);
更多细节和示例在手册中:
http://www.postgresql.org/docs/current/static/sql-altertable.html#AEN70043
编辑
通过使用IN
运算符,以相同的方式检查特定值:
alter table foo
add constraint check_positive check (some_code in ('A','B'));
如果您同意(或想要)Postgres 生成约束名称,您可以使用以下速记语法。
ALTER TABLE foo
ADD CHECK (column_1 > 2);
您可以使用alter table
命令添加新约束。 从 文档这个例子:
ALTER TABLE distributors
ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5) NO INHERIT;
您必须根据本地要求替换约束名称以及表名称和内容。
这应该这样做:
create table test (
id serial
);
alter table test
add constraint id_not_null
check (id is not null);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.