在修改表字段类型的时候使用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. 【SDOI2014】【BZOJ3529】数表

    Description 有一张N×m的数表,其第i行第j列(1 < =i < =礼.1 < =j < =m)的数值为 能同一时候整除i和j的全部自然数之和.给定a,计算数表中不 ...

  2. Java Nashorn--Part 1

    伴随 Java 8 的发布,Oracle 也一同发布了 Nashorn,它是在 Java 虚拟机上运行 Javascript 语言的一个引擎.Nashorn 的设计是为了替换最初的运行在 JVM 上的 ...

  3. C#winform实现跑马灯

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  4. Android_Bitmap_图片的二次采样并生成缩略图

    1.Bitmap概述 Android系统支持几种图片(.png (preferred), .jpg (acceptable), .gif (discouraged)), 其中Bitmap位图#ffff ...

  5. GANS 资料

    https://blog.csdn.net/a312863063/article/details/83512870 目 录第一章 初步了解GANs 3 1. 生成模型与判别模型. 3 2. 对抗网络思 ...

  6. Java 8 flatMap example

    Java 8 flatMap example In Java 8, Stream can hold different data types, for examples: Stream<Stri ...

  7. MySQL 5.6学习笔记(索引的创建与删除)

    1. 创建索引 1.1 创建新表时同时建立索引 语法: create table table_name[col_name data_type] [unique|fulltext|spatial][in ...

  8. Android 计算文件 MD5 遇到的问题

    版本下载,做 MD5 校验,使用的 MD5 算法出现了异常,当出现以 0 开头的 MD5的时候,会把 0 给忽略掉,造成 MD5 只有 31 位,造成校验失败. 转:http://blog.csdn. ...

  9. char型指针和字符串字面量和字符数组

    1.当一个char型指针指向一个字符串字面量(也就是常量字符串)时,该指针必须由const修饰,否则,系统会给出deprecated(不赞成)的警告.原因是:字符串字面量不可改变,当它被一个非cons ...

  10. centos 7安装cppman

    cppman是一个在命令行查询c和c++语法及标准库函数的工具,非常好用,python3编写,记录一下安装过程: yum update yum install python34 yum install ...