KingbaseES ALTER TABLE 中 USING 子句的用法

2022-10-17,,,,

using子句用于在修改表字段类型的时候,进行显示的转换类型。

1.建表

create table t(id integer);

2.插入数据

insert into t select generate_series(1,10);

3.把id列类型修改为varchar

test=# alter table t alter id type varchar;
ALTER TABLE 因为integer转varchar有隐式的转换,所以可以自动转换过去。

4.把id列类型改回integer

test=#  alter table t alter id type integer;
错误: 字段 "id" 不能自动转换成类型 integer
提示: 您可能需要指定"USING id::integer"。 在oracle模式下有varchar到integer的cast函数,所以不会报错。上述错误是在pg模式下产生的。

5.使用Using子句进行强制类型转换

test=# alter table t alter id type integer using id::integer;
ALTER TABLE

转换类型的时候有隐含类型转换的时候,会自动转换,如果没有,那么就必须使用using指定一下转换规则。

KingbaseES ALTER TABLE 中 USING 子句的用法的相关教程结束。

《KingbaseES ALTER TABLE 中 USING 子句的用法.doc》

下载本文的Word格式文档,以方便收藏与打印。