PostgreSQL ALTER TABLE中改变数据类型时USING的用法<转>
在修改表字段类型的时候使用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的用法<转>的更多相关文章
- KingbaseES ALTER TABLE 中 USING 子句的用法
using子句用于在修改表字段类型的时候,进行显示的转换类型. 1.建表 create table t(id integer); 2.插入数据 insert into t select generat ...
- 向多页TABLE中插入数据时,新增行总是在当前页的最后一行
CODE IN CO OATableBean table = (OATableBean)webBean.findChildRecursive("LineTable"); int n ...
- jquery中bind事件时的命名空间用法(转)
场景:页面上的某个元素bind多个click事件处理函数,视用户的具体交互情况来决定到底使用哪个处理函数. 问题: unbind时会解绑所有的click事件,造成误伤.如果之前bind时有定义处理函数 ...
- MySQL5.6 ALTER TABLE 分析和测试
在MySQL5.5和之前版本,在运行的生产环境对大表(超过数百万纪录)执行Alter操作是一件很困难的事情.因为将重建表和锁表,影响用户者的使用.因此知道Alter操作何时结束对我们是非常重要的.甚至 ...
- linux之SQL语句简明教程---ALTER TABLE
在表格被建立在资料库中后,我们常常会发现,这个表格的结构需要有所改变.常见的改变如下: 加一个栏位 删去一个栏位 改变栏位名称 改变栏位的资料种类 以上列出的改变并不是所有可能的改变.ALTER TA ...
- mysql alter table
准备: create table t(x int,y int); 用法 1: 修改列的数据类 alter table t modify column y nvarchar(32); 用法2: 给表加一 ...
- 模拟在table中移动鼠标,高亮显示鼠标所在行
在项目中有这样一个需求,在table中移动鼠标时,鼠标所在行高亮显示,其他行正常显示,为此做了一个模拟. 具体代码如下: <!DOCTYPE html> <html xmlns=&q ...
- REACT 使用antd Table 中rowSelection遇到的问题
首先项目是尚硅谷的后台谷粒平台,在用到antd Table 中的 rowSelection时,出现了一个问题(P87时遇到的问题): 表格中的每一项前面有一个radio单选框可以选中,本来是想利用ro ...
- JavaScript中instanceof与typeof运算符的用法及区别详细解析
JavaScript中的instanceof和typeof常被用来判断一个变量是什么类型的(实例),但它们的使用还是有区别的: typeof 运算符 返回一个用来表示表达式的数据类型的字符串. typ ...
随机推荐
- Android编译系统环境初始化过程分析
Android源代码在编译之前,要先对编译环境进行初始化,其中最主要就是指定编译的类型和目标设备的型号.Android的编译类型主要有eng.userdebug和user三种,而支持的目标设备型号则是 ...
- 【转】在一个Job中同时写入多个HBase的table
在进行Map/Reduce时,有的业务需要在一个job中将数据写入到多个HBase的表中,下面是实现方式. 原文地址:http://lookfirst.com/2011/07/hbase-multit ...
- mysql数据导入遇到的timestamp类型问题
今天准备把最新的表导入自己以前的机子上做临时开发,在数据库导入的时候遇到一个问题:Incorrect table definition; there can be only one TIMESTAMP ...
- java、oracle对CLOB处理
oracle CLOB字段转换位VARCHAR 1.实际上处理CLOB字段的时候,直接TO_CHAR,当长度超过4000的时候,会报错,提示列被截取: CLOB转varchar2:select to_ ...
- Shell脚本大量示例
Shell基础之控制流结构 一.控制结构 几乎所有的脚本里都有某种流控制结构,很少有例外.流控制是什么?假定有一个脚本,包含下列几个命令: #!/bin/sh # make a directory ...
- 看了一下unity5.6的新功能 以及Timeline
3月31日unity5.6发布,然而timeline(前sequence模块)被delay到unity 2017.上个星期官方又发布了unity 2017的beta版本 抽空看了下 (unity5.6 ...
- [svc][cpu][jk]cpu的核心查看及什么是cpu的负载
监控的时候我们会监控cpu的负载,那么什么是负载? 编程时候有多核多线程的概念,那么cpu内部如何运作的? 搞清多少bit cpu? 有几个物理cpu?每个cpu是几核的? 之前购买内存条时候,需要关 ...
- Variance overview: Invariant, Covariant, Contravariant, 协变,逆变
- redis-3.0.0_rc5的RPM包制定
首先查看脚本: # cat /tmp/redis_before.sh #!/bin/bash if [ ! -d /apps/redis-3.0.0-rc5 ];then mkdir -p /apps ...
- HTML5学习笔记(五):CSS基础
CSS 指层叠样式表 (Cascading Style Sheets),在网页中用来定义网页的元素如何进行显示. CSS 对大小写不敏感.不过存在一个例外:如果涉及到与 HTML 文档一起工作的话,c ...