database - 更改 postgresql 中的列大小

标签 database postgresql postgresql-9.1

我目前在 postgresql 中有一个名为 companies 的表。
companies 有一个长度为 30 的列 name,但现在我想将该列的长度减少到 20字符,但不幸的是,我在该列中有一些条目的长度可达 25-30。谁能建议我如何在不丢失任何数据条目的情况下减小列的大小?

这是我到目前为止尝试过的:

alter table companies alter COLUMN name type varchar(20);

但我明白了:

ERROR: value too long for type character varying(20)

最佳答案

您可以剪切超过 20 个字符的名称:

update companies
set name = substr(name, 1, 20)
where length(name) > 20

alter table companies alter column name type varchar(20);

关于database - 更改 postgresql 中的列大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31560573/

相关文章:

mysql - 有哪些前端软件可以在 Mac OS 中使用 MySql(Base 和 Filemaker 除外)?

java - 将 Hashmap 数据移至数据库

Django-Postgres : length of CharField as primary key

sql - Postgres : Statistical functions on date time intervals

sql - 如何在单个查询中使用计数和限制在 postgres 中选择数据

ubuntu - 在 Ubuntu 中从 CSV 复制到 Postgres

php - 使用未定义常量 STDOUT - 假定为 'STDOUT'

php - 如何使用用户唯一的安全代码从数据库更新密码

php - 我需要将 <p> 标记删除到仅包含 URL 的字段

postgresql - 如何在 postgresql 中强制删除索引关系?