sql - 空数组作为 PostgreSQL 数组列默认值

标签 sql postgresql

我在 postgresql 9.4 数据库中定义了一个数组字段:

character varying(64)[]

我可以有一个空数组吗? {} 该字段的默认值? 这样设置的语法是什么?

如果只设置括号 {},我会收到以下错误:

SQL error:

ERROR:  syntax error at or near "{"
LINE 1: ...public"."accounts" ALTER COLUMN "pwd_history" SET DEFAULT {}
                                                                     ^

In statement:
ALTER TABLE "public"."accounts" ALTER COLUMN "pwd_history" SET DEFAULT {}

最佳答案

您需要使用显式的array 初始化器并将其转换为正确的类型:

ALTER TABLE public.accounts 
    ALTER COLUMN pwd_history SET DEFAULT array[]::varchar[];

关于sql - 空数组作为 PostgreSQL 数组列默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30933266/

相关文章:

mysql - 如何从 WordPress 中的所有帖子中删除带有拉丁文本的 anchor 标记?

python - Django 多对一删除

MySQL - 如何优化长时间运行的查询

postgresql - Heroku PostgreSQL 中的 `ERROR: cannot execute TRUNCATE TABLE in a read-only transaction`

postgresql - 如何在 postgresql 中将 zip 文件解压缩到 tar

mysql - 返回计算结果集的存储过程

mysql - 带和不带窗口函数的最小值和最大值

mysql - 在 Postgresql 中模拟 MySQL 的 ORDER BY FIELD()

node.js - 安装 nodej.js pg 模块问题(无法打开包含文件 : 'pg_config.h' )

sql - 如何在 postgres 9.3 中存储少量关系数据(例如用户角色)?