pg 18的not null 约束 在pg 17中,我们在创建约束的时候不支持约束暂时不生效,但是18中提供了此功能: 下面是相关测试: 首先 pg17中: zc=# select version(); version --------------------------------------------------------------------------------------------------------- PostgreSQL 17.4 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit (1 row) zc=# drop table t; DROP TABLE zc=# create table t ( a int not null, b text ); CREATE TABLE zc=# \d t Table "public.t" Column | Type | Collation | Nullable | Default --------+---------+-----------+----------+--------- a | integer | | not null | b | text | | | zc=# insert into t select null,1 from generate_series(1,2); ERROR: null value in column "a" of relation "t" violates not-null constraint DETAIL: Failing row contains (null, 1). 通过上面的测试可以看到我们默认情况下pg会检查 not null约束,影响了我们的数据插入。 pg 18中的测试情况: 我们可以先不加not null 约束,等插入数据后添加 not null 约束但是 not valid 修改数据都正常后再使他valid postgres=# select version(); version ----------------------------------------------------------------------------------------------------------- PostgreSQL 18devel on x86_64-pc-linux-gnu, compiled by gcc (GCC) 8.3.1 20191121 (Red Hat 8.3.1-5), 64-bit (1 row) postgres=# drop table t; DROP TABLE postgres=# create table t ( a int, b text ); CREATE TABLE postgres=# insert into t select null,1 from generate_series(1,2); INSERT 0 2 postgres=# alter table t add constraint c1 not null a not valid; ALTER TABLE postgres=# select * from t; a | b ---+--- | 1 | 1 (2 rows) postgres=# update t set a = 1; UPDATE 2 postgres=# alter table t validate constraint c1; ALTER TABLE postgres=# insert into t values (null, 'a'); ERROR: null value in column "a" of relation "t" violates not-null constraint DETAIL: Failing row contains (null, a). pg 17测试相关语法: zc=# select version(); version --------------------------------------------------------------------------------------------------------- PostgreSQL 17.4 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit (1 row) zc=# drop table t; DROP TABLE zc=# create table t ( a int, b text ); CREATE TABLE zc=# insert into t select null,1 from generate_series(1,2); INSERT 0 2 zc=# alter table t add constraint c1 not null a not valid; ERROR: syntax error at or near "not" LINE 1: alter table t add constraint c1 not null a not valid; 可以看到pg17中相关语法是暂时不支持的。
pg 18的not null 约束
来源:这里教程网
时间:2026-03-14 21:54:02
作者:
编辑推荐:
下一篇:
相关推荐
-
雷神推出 MIX PRO II 迷你主机:基于 Ultra 200H,玻璃上盖 + ARGB 灯效
2 月 9 日消息,雷神 (THUNDEROBOT) 现已宣布推出基于英
-
制造商 Musnap 推出彩色墨水屏电纸书 Ocean C:支持手写笔、第三方安卓应用
2 月 10 日消息,制造商 Musnap 现已在海外推出一款 Oce
热文推荐
- 近5年最值得期待的PG大版本
近5年最值得期待的PG大版本
26-03-14 - 没想到,windows下pg表空间迁移居然能这么玩
没想到,windows下pg表空间迁移居然能这么玩
26-03-14 - 【PGCCC】PostgreSQL 作为向量数据库:入门和扩展
【PGCCC】PostgreSQL 作为向量数据库:入门和扩展
26-03-14 - 【PGCCC】在 PostgreSQL 中,如何优化对于日期范围的查询?
【PGCCC】在 PostgreSQL 中,如何优化对于日期范围的查询?
26-03-14 - pg等保设置实操
pg等保设置实操
26-03-14 - 【PGCCC】如何加快 SQL 查询速度的同时保持SQL的简洁?
【PGCCC】如何加快 SQL 查询速度的同时保持SQL的简洁?
26-03-14 - 【PGCCC】用于全文搜索的全新 Postgres 块存储布局
【PGCCC】用于全文搜索的全新 Postgres 块存储布局
26-03-14 - 【PGCCC】PostgreSQL 中表级锁的剖析
【PGCCC】PostgreSQL 中表级锁的剖析
26-03-14 - 【PGCCC】空闲事务导致表膨胀?等等,什么?
【PGCCC】空闲事务导致表膨胀?等等,什么?
26-03-14 - 【PGCCC】commit_delay 对性能的提升:PostgreSQL 基准测试
