【问题标题】:Postgresql: Why does \d+ table not show default as nullPostgresql:为什么 \d+ 表不显示默认为空
【发布时间】:2018-10-09 13:03:21
【问题描述】:

我是 MySQL 的 Postgres 新手。

我创建了一个表,其中的列“col”默认为空。

CREATE TABLE widgets (id serial primary key, col bigint default null);
CREATE TABLE
test=# \d+ widgets;
                                                Table "public.widgets"
 Column |  Type   | Collation | Nullable |               Default               | Storage | Stats target | Description 
--------+---------+-----------+----------+-------------------------------------+---------+--------------+-------------
 id     | integer |           | not null | nextval('widgets_id_seq'::regclass) | plain   |              | 
 col    | bigint  |           |          |                                     | plain   |              | 
Indexes:
    "widgets_pkey" PRIMARY KEY, btree (id)

test=# 

但是,在 Default 列中没有任何内容表明它是一个空字段。

默认值是否应该在模式输出中显示为 null?

我也尝试了上面相同的示例,但使用了default 99。键入 \d+ 时,默认列中不显示任何内容。在该示例中,我插入一行 INSERT INTO widgets (id) VALUES (1); 它获取默认值 99,因此我知道正在使用默认值(至少对于整数值)。 更新:默认列实际上显示 99。我使用了带有自动完成功能的 \d+ 并查看了错误的表格。

使用default null,当我SELECT * FROM widgets 时,该列显示为空白。

Postgres 是否没有明确显示标记为“null”的值?如果不是,如何区分空文本字段和具有空值的字段?

【问题讨论】:

    标签: postgresql


    【解决方案1】:

    当未指定任何内容时,列的默认值始终为null。显然psql 开发人员认为,将null 显示为默认值是没有意义的。可能是因为这是标准行为,或者因为 null 不是“值”。

    \d 输出中仅显示非空默认值。


    如何区分空文本字段和空值字段?

    空字符串''null 值不同,您会在输出中看到差异:

    postgres=# CREATE TABLE null_test (c1 text default null, c2 text default '');
    CREATE TABLE
    postgres=# \d null_test
                Table "public.null_test"
     Column | Type | Collation | Nullable | Default
    --------+------+-----------+----------+----------
     c1     | text |           |          |
     c2     | text |           |          | ''::text
    

    【讨论】:

    • 我想说你在Default 列中看不到NULL 默认值,因为-呃-这是默认的默认值。因此,如果您不指定默认值,它将为 NULL。
    猜你喜欢
    • 1970-01-01
    • 2016-07-29
    • 1970-01-01
    • 1970-01-01
    • 2017-11-28
    • 2018-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多