Postgres SQL约束一个字符类型(Postgres SQL constraint a character type)

编程入门 行业动态 更新时间:2024-10-28 19:33:33
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

更多推荐

本文发布于:2023-07-19 16:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1183604.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符   类型   SQL   Postgres   character

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!