postgresql - Postgres 9.4 如何对 int 字段进行最大约束

标签 postgresql postgresql-9.4

我是 postgres 的新手,一直在尝试找到一种方法来对字段设置最大限制。我的数据库中有一个名为 friends 的字段,其类型为 INT 我想对该字段施加 30 的约束,以便该字段中保存的数字不能被高于 30。我正在使用 postgres 9.4 和 PgAdmin 3 。我正在阅读此页http://www.postgresql.org/docs/9.4/static/sql-set-constraints.html但找不到任何东西。

最佳答案

为此使用 CHECK 约束。

创建架构时,只需键入:

CREATE TABLE sometable (
  ...
  friends int CONSTRAINT high_friends CHECK (friends <= 30)
)

关于postgresql - Postgres 9.4 如何对 int 字段进行最大约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35075100/

相关文章:

postgresql - 为什么不能直接在jsonb_array_elements上查询?

PostgreSQl pg_dump权限错误?

sql - 进行 psql 导出(使用正则表达式替换所有列)的最佳方法是什么?

xml - 使用 XPath : SQL Error [42601]: ERROR: syntax error at or near "[" 在 PostgreSQL 中提取 XML 值时出错

sql - 插入错误 : no destination for result data

hibernate - 如何配置要存储为 (postgres 9.4) jsonb 的 grails 域类属性?

postgresql - Postgres 9.4 恢复不工作

postgresql - 识别导致 pg_stat_user_tables 中显示的 seq_scan 的查询

java - 连接池不会重用空闲连接

django - 完整性错误: null value in column "id" for all models/fields with ForeignKey after postgres restore from dump