【发布时间】:2017-12-26 16:31:45
【问题描述】:
我有用户表,现在我想添加一个串行列cid:
用户:
id | username | email | createdAt
-------------------------------------------------
"uuid" | "abc" | "abc@gmail.com" | '2017-01-01'
"uuid" | "abc" | "abc@gmail.com" | '2017-02-01'
"uuid" | "abc" | "abc@gmail.com" | '2017-03-01'
我想添加一个 cid 列,其值类似于 serial 但不是主键
id |username| email | createdAt | cid
-------------------------------------------------------
"uuid" | "abc" | "abc@gmail.com" | '2017-01-01' | 1
"uuid" | "abc" | "abc@gmail.com" | '2017-02-01' | 2
"uuid" | "abc" | "abc@gmail.com" | '2017-03-01' | 3
...
我尝试了什么:
alter table user add column cid serial not null;
但它会生成:
id |username| email | createdAt | cid
-------------------------------------------------------
"uuid" | "abc" | "abc@gmail.com" | '2017-01-01' | 4
"uuid" | "abc" | "abc@gmail.com" | '2017-02-01' | 7
"uuid" | "abc" | "abc@gmail.com" | '2017-03-01' | 3
....
我可以这样做吗:
alter table user add column cid serial not null order by createdAt
这样它就可以生成预期的结果,即cid 顺序值,顺序为createdAt?
【问题讨论】:
标签: sql postgresql