Postgres-如何添加新列UUID
我需要在表上添加新列是UUID数据类型,在这里我的代码:
ALTER TABLE core.example add COLUMN newcolumn SET DATA TYPE UUID USING (uuid_generate_v4())
但是向我展示此错误:
ERROR: type modifier is not allowed for type "uuid"
LINE 1: ALTER TABLE core.example add COLUMN newsi UUID (uuid_genera...
我不想更改列,是在我的表上创建一个新列。有什么想法做到这一点吗?
问候
I need to add new column on my table would be uuid data type, here my code:
ALTER TABLE core.example add COLUMN newcolumn SET DATA TYPE UUID USING (uuid_generate_v4())
but show me this error:
ERROR: type modifier is not allowed for type "uuid"
LINE 1: ALTER TABLE core.example add COLUMN newsi UUID (uuid_genera...
I dont want to alter a column, would be to create a new column on my table. Any idea how to make this?
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加新列时,您不使用
设置数据类型
。您的语句应该看起来像:默认子句将立即用UUID填充列。
另外,如果您只想在列中填写初始数据,则可以在之后删除默认子句:
请注意,如果您使用的是Postgres 13,并且通常可以使用
gen_random_uuid()
,则最有优先方法是内置的,不依赖uuid-ossp
扩展。When adding a new column you don't use
SET DATA TYPE
. Your statement should look like:The DEFAULT clause will immediately fill the column with UUIDs.
Alternatively if you you just want to fill the column with initial data, you can drop the DEFAULT clause afterward:
Note that if you are using Postgres 13 and newer it is generally preferrable to use
gen_random_uuid()
since that method is built-in and does not rely on theuuid-ossp
extension.