当前位置:Gxlcms > 数据库问题 > PostgreSQL ALTER TABLE中改变数据类型时USING的用法<转>

PostgreSQL ALTER TABLE中改变数据类型时USING的用法<转>

时间:2021-07-01 10:21:17 帮助过:34人阅读

2. 插入数据

insert into tb10 select generate_series(1,5);

3. 把id的int变为varchar

postgres=# alter table tb101 alter id type varchar;
ALTER TABLE

 

因为int转varchar有隐式的转换,故可以自动转换过去。

postgres=# \d tb101
          Table "public.tb101"
 Column |       Type        | Modifiers 
--------+-------------------+-----------
 id     | character varying |

 

 

4. 把id的varchar变为int

postgres=# alter table tb101 alter id type int;
ERROR:  column "id" cannot be cast automatically to type integer
HINT:  Specify a USING expression to perform the conversion.

 

在没有隐式的转换下,就需要指定Using来显示的转换。

5. 使用Using进行类型转换

postgres=# alter table tb101 alter id type int using id::int;
ALTER TABLE
postgres=# \d tb101
     Table "public.tb101"
 Column |  Type   | Modifiers 
--------+---------+-----------
 id     | integer | 

id::int 也可以使用cast(id as int)

 

PostgreSQL ALTER TABLE中改变数据类型时USING的用法<转>

标签:ber   转换   数据类型   varchar   需要   view   convert   for   sim   

人气教程排行