在新列中创建序列号

Sid*_*m H 1 snowflake-cloud-data-platform

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

ALTER TABLE <table_name> 
    ADD IF NOT EXISTS <column_name> SERIAL;
Run Code Online (Sandbox Code Playgroud)

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

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

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

Gok*_*til 5

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

您可以使用 AUTOINCRMENT 或 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 |
+----+-------+
Run Code Online (Sandbox Code Playgroud)

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

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