PostgreSQL ALTER TABLE中改变数据类型时USING的用法
来源:互联网 发布:美术软件 编辑:程序博客网 时间:2024/05/17 01:09
在修改表字段类型的时候使用Using来进行显示的转换类型。
原文说明:
SET DATA TYPE
This form changes the type of a column of a table. Indexes and simple table constraints involving the column willbe automatically converted to use the new column type by reparsing the originally supplied expression. The optional COLLATE clause specifies a collation for the new column; if omitted, the collation is the default for the new column type. The optional USING clause specifies how to compute the new column value from the old; if omitted, the default conversion is the same as an assignment cast from old data type to new. A USING clause must be provided if there is no implicit or assignment cast from old to new type.
大致意思是:转换类型的时候有隐含类型转换的时候,会自动转换,如果没有,那么就必须使用using指定一下转换规则。
1. 建表
create table 101(id integer);
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 integerHINT: Specify a USING expression to perform the conversion.
在没有隐式的转换下,就需要指定Using来显示的转换。
5. 使用Using进行类型转换
postgres=# alter table tb101 alter id type int using id::int;ALTER TABLEpostgres=# \d tb101 Table "public.tb101" Column | Type | Modifiers --------+---------+----------- id | integer |
- PostgreSQL ALTER TABLE中改变数据类型时USING的用法
- PostgreSQL ALTER TABLE中改变数据类型时USING的用法
- hive中alter table的用法详解
- hive中alter table的用法详解
- ALTER TABLE的用法
- SQL ALTER TABLE的用法
- mssql alter table 的用法
- SQL ALTER TABLE的用法
- ALTER TABLE用法详解
- MySQL简单操作之alter table改变表的结构
- MySQL简单操作之alter table改变表的结构
- 在db2数据库中,如何使用命令改变列(alter table, alter column)长度
- MySQL中alter的用法
- MySQL中alter的用法
- Oracle中 Alter Table 语句的使用
- Oracle中 Alter Table 语句的使用
- Oracle中 Alter Table 语句的使用
- 支持alter table move 的数据类型 :raw blob clob
- juery实战4:封装拖拽插件
- 讲项目实战:网站会员功能开发+登录/注册表单
- logrotate进行日志管理
- 30讲项目实战:网站面包屑导航+表单功能开发
- 29讲项目实战:404页面制作+网站友情链接调用
- PostgreSQL ALTER TABLE中改变数据类型时USING的用法
- ActiveMQ
- 28讲项目实战:网站搜索功能+单页面开发
- 疯狂Java学习笔记(79)---------- ConcurrentModificationException异常
- 二叉堆
- 27讲项目实战:调用指定分类目录下文章+签证列表页和内容页调用
- ubuntu14.04 给android 创建wifi
- vs 2013已停止工作和axml页面design不能正常加载
- 26讲项目实战:分页插件+图集插件+文章详细页调用