【问题标题】:Create serial numbers in a new column在新列中创建序列号
【发布时间】:2021-10-15 18:32:21
【问题描述】:

在 postgres 中,我们可以添加一个新的增量列,例如:

ALTER TABLE <table_name> 
    ADD IF NOT EXISTS <column_name> SERIAL;

这会在表中创建带有序列号的新列。在雪花中,当我尝试相同的命令时,它会抛出错误

SQL 编译错误:不支持的数据类型“SERIAL”。

如何在 Snowflake 中实现相同的行为?

【问题讨论】:

    标签: snowflake-cloud-data-platform snowflake-schema


    【解决方案1】:

    SERIAL 伪类型是一个自动递增的整数,存储大小为 4 个字节,范围为 1 到 2,147,483,647。

    您可以使用 AUTOINCREMENT 或 IDENTITY 来获得相同的功能:

    create or replace table test ( id number AUTOINCREMENT, v varchar ) ;
    
    insert into test (v) values ('Test1'),('Test2'),('Test3');
    
    select * from test;
    
    +----+-------+
    | ID |   V   |
    +----+-------+
    |  1 | Test1 |
    |  2 | Test2 |
    |  3 | Test3 |
    +----+-------+
    

    https://community.snowflake.com/s/question/0D50Z00008xAsQ0/how-to-set-auto-identity-column-from-alter-table-statement

    PS:建表后无法添加这样的列。

    【讨论】:

    • 我只采用了这种方法。如果有人使用“复制到命令”copy into test (v) from table_name file_format**,则语法相同
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-09
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    • 2020-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多