Postgres-如何添加新列UUID

发布于 2025-02-07 23:03:29 字数 370 浏览 0 评论 0原文

我需要在表上添加新列是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

停滞 2025-02-14 23:03:29

添加新列时,您不使用设置数据类型。您的语句应该看起来像:

ALTER TABLE core.example ADD COLUMN newcolumn UUID DEFAULT (uuid_generate_v4());

默认子句将立即用UUID填充列。

另外,如果您只想在列中填写初始数据,则可以在之后删除默认子句:

ALTER TABLE core.example ALTER COLUMN newcolumn DROP DEFAULT;

请注意,如果您使用的是Postgres 13,并且通常可以使用gen_random_uuid(),则最有优先方法是内置的,不依赖uuid-ossp扩展。

When adding a new column you don't use SET DATA TYPE. Your statement should look like:

ALTER TABLE core.example ADD COLUMN newcolumn UUID DEFAULT (uuid_generate_v4());

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:

ALTER TABLE core.example ALTER COLUMN newcolumn DROP DEFAULT;

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 the uuid-ossp extension.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文