在修改表字段类型的时候使用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 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的用法<转>的更多相关文章

  1. KingbaseES ALTER TABLE 中 USING 子句的用法

    using子句用于在修改表字段类型的时候,进行显示的转换类型. 1.建表 create table t(id integer); 2.插入数据 insert into t select generat ...

  2. 向多页TABLE中插入数据时,新增行总是在当前页的最后一行

    CODE IN CO OATableBean table = (OATableBean)webBean.findChildRecursive("LineTable"); int n ...

  3. jquery中bind事件时的命名空间用法(转)

    场景:页面上的某个元素bind多个click事件处理函数,视用户的具体交互情况来决定到底使用哪个处理函数. 问题: unbind时会解绑所有的click事件,造成误伤.如果之前bind时有定义处理函数 ...

  4. MySQL5.6 ALTER TABLE 分析和测试

    在MySQL5.5和之前版本,在运行的生产环境对大表(超过数百万纪录)执行Alter操作是一件很困难的事情.因为将重建表和锁表,影响用户者的使用.因此知道Alter操作何时结束对我们是非常重要的.甚至 ...

  5. linux之SQL语句简明教程---ALTER TABLE

    在表格被建立在资料库中后,我们常常会发现,这个表格的结构需要有所改变.常见的改变如下: 加一个栏位 删去一个栏位 改变栏位名称 改变栏位的资料种类 以上列出的改变并不是所有可能的改变.ALTER TA ...

  6. mysql alter table

    准备: create table t(x int,y int); 用法 1: 修改列的数据类 alter table t modify column y nvarchar(32); 用法2: 给表加一 ...

  7. 模拟在table中移动鼠标,高亮显示鼠标所在行

    在项目中有这样一个需求,在table中移动鼠标时,鼠标所在行高亮显示,其他行正常显示,为此做了一个模拟. 具体代码如下: <!DOCTYPE html> <html xmlns=&q ...

  8. REACT 使用antd Table 中rowSelection遇到的问题

    首先项目是尚硅谷的后台谷粒平台,在用到antd Table 中的 rowSelection时,出现了一个问题(P87时遇到的问题): 表格中的每一项前面有一个radio单选框可以选中,本来是想利用ro ...

  9. JavaScript中instanceof与typeof运算符的用法及区别详细解析

    JavaScript中的instanceof和typeof常被用来判断一个变量是什么类型的(实例),但它们的使用还是有区别的: typeof 运算符 返回一个用来表示表达式的数据类型的字符串. typ ...

随机推荐

  1. python 实验环境

    python 实验环境的搭建 刚开始在windows环境下尝试过komodo ,eclispse pydev,swing,spyder甚至limodou的编辑器,之后ipython,安装很多科学计算包 ...

  2. mysql中查询一个字段属于哪一个数据库中的哪一个表的方式

    mysql中查询一个字段具体是属于哪一个数据库的那一张表:用这条语句就能查询出来,其中 table_schema 是所在库, table_name 是所在表 --mysql中查询某一个字段名属于哪一个 ...

  3. Spring+Mybatis整合过程中找不到.properties文件

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' ...

  4. Android Gradle 引入 aar 方式

    方式 1 File -> New -> New Module -> Import .JAR/.AAR Package Choose File Finish 在 build.gradl ...

  5. 直接获取摄像头传回的图像数据(人脸、微笑、眨眼: 识别--&gt;第一步):图像识别第一步

    转:ios通过摄像头获取特定数据(http://www.2cto.com/kf/201404/290777.html) 凝视: 因为近期项目需求,须要一个可以实现对摄像头图片获取当中部分内容的功能,类 ...

  6. CentOS 7 安装中文环境

    centos升级到7后,系统设置好多和6有了很大的区别,中文支持就有很大的变化. 1.安装中文语言包. yum install kde-l10n-Chinese 2.安装(已经安装的要重新安装)gli ...

  7. [nginx]location语法

    location语法 location语法格式 location [=|~|~*|^~] uri { .... } location [=|~|~*|^~] uri {....} 指令 匹配标识 匹配 ...

  8. HHH

    https://data-artisans.com/flink-forward/resources/alibabas-common-algorithm-platform-on-flink https: ...

  9. [Windows Azure] Data Management and Business Analytics

    http://www.windowsazure.com/en-us/develop/net/fundamentals/cloud-storage/ Managing and analyzing dat ...

  10. (原创)一个和c#中Lazy<T>类似的c++ Lazy<T>类的实现

    在.net 4.0中增加一个延迟加载类Lazy<T>,它的作用是实现按需延迟加载,也许很多人用过.一个典型的应用场景是这样的:当初始化某个对象时,该对象引用了一个大对象,需要创建,这个对象 ...