Postgres SQL约束一个字符类型(Postgres SQL constraint a character type)
我在Postgres中有一个表定义。 我想为一个Character数据类型的列添加一个约束,只有3个允许的值:
CREATE TABLE my_table ( id character varying(255) NOT NULL, uid character varying(255) NOT NULL, my_text text NOT NULL, is_enabled boolean NOT NULL );所以我希望my_text列只包含'A','B'或'C'作为值。
我在哪里可以找到关于此的一些文档?
I have a table definition in Postgres. I would like to add a constraint to a column that is of Character datatype to have only 3 allowed values:
CREATE TABLE my_table ( id character varying(255) NOT NULL, uid character varying(255) NOT NULL, my_text text NOT NULL, is_enabled boolean NOT NULL );So I want the my_text column to contain only 'A', 'B' or 'C' as values.
Where can I find some documentation on this?
最满意答案
使用检查约束:
CREATE TABLE my_table ( id character varying(255) NOT NULL, uid character varying(255) NOT NULL, my_text text NOT NULL, is_enabled boolean NOT NULL, constraint check_allowed check (my_text in ('A', 'B', 'C')) );手册中的更多细节: http : //www.postgresql.org/docs/current/static/ddl-constraints.html#DDL-CONSTRAINTS-CHECK-CONSTRAINTS
Use a check constraint:
CREATE TABLE my_table ( id character varying(255) NOT NULL, uid character varying(255) NOT NULL, my_text text NOT NULL, is_enabled boolean NOT NULL, constraint check_allowed check (my_text in ('A', 'B', 'C')) );More details in the manual: http://www.postgresql.org/docs/current/static/ddl-constraints.html#DDL-CONSTRAINTS-CHECK-CONSTRAINTS
更多推荐
发布评论