Postgres:减少 varchar 大小并截断

发布于 2024-08-25 17:34:39 字数 91 浏览 1 评论 0原文

我目前有一个 Postgres 8.4 数据库,其中包含 varchar(10000) 列。我想将其更改为 varchar(255) 并截断任何太长的数据。我该怎么做?

I currently have a Postgres 8.4 database that contains a varchar(10000) column. I'd like to change this into a varchar(255) and truncate any data that happens to be too long. How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

紅太極 2024-09-01 17:34:39

类似于 ALTER TABLE t ALTER COLUMN c TYPE VARCHAR(255) USING SUBSTR(c, 1, 255)

Something like ALTER TABLE t ALTER COLUMN c TYPE VARCHAR(255) USING SUBSTR(c, 1, 255)

倥絔 2024-09-01 17:34:39

1) 使用子字符串方法更新列数据以截断它

update t set col = substring(col from 1 for 255)

2) 然后更改表列

alter table t alter column col type varchar(255)

此处的文档 http://www.postgresql.org/docs/8.4/static/sql-altertable.html

1) Update the column data using a substring method to truncate it

update t set col = substring(col from 1 for 255)

2) Then alter the table column

alter table t alter column col type varchar(255)

Docs here http://www.postgresql.org/docs/8.4/static/sql-altertable.html

疑心病 2024-09-01 17:34:39
BEGIN;
UPDATE table SET column = CAST(column as varchar(255));
ALTER TABLE table ALTER COLUMN column TYPE varchar(255); --not sure on this line. my memory is a bit sketchy
COMMIT;
BEGIN;
UPDATE table SET column = CAST(column as varchar(255));
ALTER TABLE table ALTER COLUMN column TYPE varchar(255); --not sure on this line. my memory is a bit sketchy
COMMIT;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文