暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

PG COPY 与 INSERT方式导入数据时, 表默认值表现的不同

原创 岳麓丹枫 2024-12-02
82

使用 insert 插入数据时, 字段默认值的表现

postgres=# \pset null NULL Null display is "NULL". postgres=# drop table if exists test; DROP TABLE postgres=# create table test(id int, info int default 0); CREATE TABLE postgres=# insert into test (id) values(1); INSERT 0 1 postgres=# insert into test (id) values(2); INSERT 0 1 postgres=# insert into test (id,info) values(2,NULL); INSERT 0 1 postgres=# select * from test; id | info ---- +------ 1 | 0 2 | 0 2 | NULL (3 rows) postgres=# postgres=# postgres=# insert into test (id) values(3); INSERT 0 1 postgres=# select * from test; id | info ---- +------ 1 | 0 2 | 0 2 | NULL 3 | 0 (4 rows)

使用 COPY 导入数据时,字段默认值的表现

postgres=# \pset null NULL Null display is "NULL". postgres=# drop table if exists test; DROP TABLE postgres=# create table test(id int, info int default 0); CREATE TABLE postgres=# insert into test (id) values(1); INSERT 0 1 postgres=# insert into test (id) values(2); INSERT 0 1 postgres=# insert into test (id,info) values(2,NULL); INSERT 0 1 postgres=# select * from test; id | info ---- +------ 1 | 0 2 | 0 2 | NULL (3 rows) postgres=# postgres=# postgres=# insert into test (id) values(3); INSERT 0 1 postgres=# select * from test; id | info ---- +------ 1 | 0 2 | 0 2 | NULL 3 | 0 (4 rows)

可以看到, 通过 copy 导入数据的时候, user_status_flag 的默认值虽然是 0 , 但是当 copy 导入数据时对应字段是 null 的话, 也是不会被替换为 0 的,还是 null, 而与 insert 的表现是不一样的

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论