【问题标题】:Postgres: Reduce varchar size and truncatePostgres:减小 varchar 大小并截断
【发布时间】:2011-01-28 19:27:49
【问题描述】:

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

【问题讨论】:

    标签: postgresql varchar truncate


    【解决方案1】:
    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;
    

    【讨论】:

      【解决方案2】:

      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

      【讨论】:

      • 使用更新的 pgsql 似乎是:update t set col = substring(col from 1 for 255) ("for" replaces "to")
      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-05-28
        • 1970-01-01
        • 2011-10-30
        • 1970-01-01
        • 2020-01-27
        • 2012-01-10
        • 2020-04-11
        相关资源
        最近更新 更多