PostgreSQL alter column type 1 to type 2 using express or auto cast

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云原生数据库 PolarDB 分布式版,标准版 2核8GB
简介:

在使用数据库时,有些应用开发人员可能喜欢使用数值来表示布尔逻辑值,或者在最初定义一个字段的状态时使用的类型,将来不能表达所有的值。
未来则可能需要对字段进行转换,例如数值转换为布尔,或者布尔转换为数值。
还有的时候,一开始可能使用了大量的重复文本,在进行统计时,文本比整型的效率低,在进入仓库后可能需要字典化这些文本(例如APPNAME) , 也会涉及字段类型的转换。
例子:

postgres=# create table tbl(id int, stat numeric(1));
CREATE TABLE

postgres=# insert into tbl select id,0 from generate_series(1,1000) t(id);
INSERT 0 1000
postgres=# insert into tbl select id,1 from generate_series(1001,2000) t(id);
INSERT 0 1000

postgres=# create or replace function n_to_b(numeric) returns boolean as 
$$

  select $1::int::boolean;

$$
 language sql;
CREATE FUNCTION
postgres=# select n_to_b(1);
 n_to_b 
--------
 t
(1 row)

postgres=# select n_to_b(10);
 n_to_b 
--------
 t
(1 row)

postgres=# select n_to_b(0);
 n_to_b 
--------
 f
(1 row)

postgres=# select n_to_b(-1);
 n_to_b 
--------
 t
(1 row)

postgres=# alter table tbl alter column stat type boolean using stat::int::boolean;
ALTER TABLE

postgres=# select * from tbl limit 10;
 id | stat 
----+------
  1 | f
  2 | f
  3 | f
  4 | f
...

字典化

postgres=# create table test(id int, info text);
CREATE TABLE
postgres=# insert into test select id,'string a' from generate_series(1,100000) t(id);
INSERT 0 100000
postgres=# insert into test select id,'string b' from generate_series(1,100000) t(id);
INSERT 0 100000
postgres=# insert into test select id,'string c' from generate_series(1,100000) t(id);
INSERT 0 100000

postgres=# create or replace function fun(text) returns int as 
$$

declare
begin  
case $1 
  when 'string a' then return 0;
  when 'string b' then return 1;
  when 'string c' then return 2; 
  else return 9999;
  end case;
end;

$$
 language plpgsql strict;
CREATE FUNCTION
postgres=# select fun('a');
 fun  
------
 9999
(1 row)

postgres=# select fun('string a');
 fun 
-----
   0
(1 row)

postgres=# alter table test alter column info type int using fun(info);
ALTER TABLE
postgres=# select * from test where id=1 limit 5;
 id | info 
----+------
  1 |    0
  1 |    1
  1 |    2
(3 rows)

还有时,会涉及文本转数值,也可以使用类似的方法:
你可能需要用到to_number或者自定义函数(例如对于带有非数值的字符串,返回一个固定值)

postgres=# select to_number('123ab2','999')
postgres-# ;
 to_number 
-----------
       123
(1 row)

postgres=# select to_number('123ab2','999');
 to_number 
-----------
       123
(1 row)

postgres=# select to_number('1a123ab2','999');
 to_number 
-----------
        11
(1 row)

postgres=# select to_number('1a123ab2','999999999999');
 to_number 
-----------
     11232
(1 row)
相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
5天前
|
关系型数据库 MySQL
mysql 5.5.62版本建表语句报错: Index column size too large. The maximum column size is 767 bytes
mysql 5.5.62版本建表语句报错: Index column size too large. The maximum column size is 767 bytes
|
关系型数据库 MySQL
Mysql 建表时报错 invalid ON UPDATE clause for 'create_date' column
原文:Mysql 建表时报错 invalid ON UPDATE clause for 'create_date' column 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.
3237 0
|
数据库
Incorrect table definition; there can be only one auto column and it must be defined as a key
Incorrect table definition; there can be only one auto column and it must be defined as a key
125 0
Incorrect table definition; there can be only one auto column and it must be defined as a key
|
存储 关系型数据库 MySQL
MySQL出现Data too long for column...(错误号1406)和 Data truncated for column...(错误号1265)
MySQL出现Data too long for column...(错误号1406)和 Data truncated for column...(错误号1265)
612 0
MySQL出现Data too long for column...(错误号1406)和 Data truncated for column...(错误号1265)
|
关系型数据库 MySQL 数据库
MySQL - Column 'id' in where clause is ambiguous
MySQL - Column 'id' in where clause is ambiguous
595 0
|
关系型数据库 MySQL
MYSQL性能调优02_Explain概述、详解id、select_type、table、type、possible_keys、key、key_len、ref、rows、Extra列(二)
MYSQL性能调优02_Explain概述、详解id、select_type、table、type、possible_keys、key、key_len、ref、rows、Extra列(二)
143 0
MYSQL性能调优02_Explain概述、详解id、select_type、table、type、possible_keys、key、key_len、ref、rows、Extra列(二)
|
关系型数据库 MySQL 索引
MYSQL性能调优02_Explain概述、详解id、select_type、table、type、possible_keys、key、key_len、ref、rows、Extra列(五)
MYSQL性能调优02_Explain概述、详解id、select_type、table、type、possible_keys、key、key_len、ref、rows、Extra列(五)
175 0
MYSQL性能调优02_Explain概述、详解id、select_type、table、type、possible_keys、key、key_len、ref、rows、Extra列(五)
|
存储 关系型数据库 MySQL
MYSQL性能调优02_Explain概述、详解id、select_type、table、type、possible_keys、key、key_len、ref、rows、Extra列(四)
MYSQL性能调优02_Explain概述、详解id、select_type、table、type、possible_keys、key、key_len、ref、rows、Extra列(四)
164 0
MYSQL性能调优02_Explain概述、详解id、select_type、table、type、possible_keys、key、key_len、ref、rows、Extra列(四)
|
SQL 关系型数据库 MySQL
MYSQL性能调优02_Explain概述、详解id、select_type、table、type、possible_keys、key、key_len、ref、rows、Extra列(一)
MYSQL性能调优02_Explain概述、详解id、select_type、table、type、possible_keys、key、key_len、ref、rows、Extra列(一)
136 0
MYSQL性能调优02_Explain概述、详解id、select_type、table、type、possible_keys、key、key_len、ref、rows、Extra列(一)
|
关系型数据库 MySQL 索引
MYSQL性能调优02_Explain概述、详解id、select_type、table、type、possible_keys、key、key_len、ref、rows、Extra列(三)
MYSQL性能调优02_Explain概述、详解id、select_type、table、type、possible_keys、key、key_len、ref、rows、Extra列(三)
154 0
MYSQL性能调优02_Explain概述、详解id、select_type、table、type、possible_keys、key、key_len、ref、rows、Extra列(三)