使用FORCE_NOT_NULL把某列中空值变成长度为0的字符串,而不是NULL值

最近更新时间: 2025-02-18 16:02:00

  • 复制
    复制成功
postgres=#truncate table t;
TRUNCATETABLE
postgres=#\! cat '/data/pgxz/t.csv' ;            
1,TDSQL PG,,7
2,"pg'"",      xc%",2017-10-28 18:24:05.643102,3
3,pgxz,2017-10-2818:24:05.645691,
4,,2017-10-30 16:14:14.954213,4
postgres=#copy t from '/data/pgxz/t.csv' (format 'csv');
ERROR:  node:16386, error null value in column"f2" violates not-null constraint
DETAIL:  Failing row contains (4, null, 2017-10-3016:14:14.954213, 4).
postgres=#select * from t where f2='';
 f1 | f2 |             f3             | f4 
----+----+----+----
(0 rows)

不使用FORCE_NOT_NULL处理的话就变成NULL值。

  • 复制
    复制成功
postgres=#truncate table t;
TRUNCATETABLE
postgres=#copy t from '/data/pgxz/t.csv' (format 'csv' ,FORCE_NOT_NULL (f2));
COPY 4
postgres=#select * from t where f2='';
 f1 | f2 |             f3             | f4 
----+----+----------------------------+----
  4 |   | 2017-10-30 16:14:14.954213 |  4
row)

使用FORCE_NOT_NULL处理就变成长度为0的字符串。