PostgreSQL中的bool类型转换为smallint的方法是什么
发表于:2025-02-23 作者:千家信息网编辑
千家信息网最后更新 2025年02月23日,这篇文章主要介绍"PostgreSQL中的bool类型转换为smallint的方法是什么",在日常操作中,相信很多人在PostgreSQL中的bool类型转换为smallint的方法是什么问题上存在疑
千家信息网最后更新 2025年02月23日PostgreSQL中的bool类型转换为smallint的方法是什么
这篇文章主要介绍"PostgreSQL中的bool类型转换为smallint的方法是什么",在日常操作中,相信很多人在PostgreSQL中的bool类型转换为smallint的方法是什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"PostgreSQL中的bool类型转换为smallint的方法是什么"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
转换过程
1.使用psql中的\help alter table命令找到修改列类型的语法:
[local:/data/run/pg12]:5120 pg12@testdb=# \help alter tableCommand: ALTER TABLEDescription: change the definition of a tableSyntax:ALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ] action [, ... ]...where action is one of: ADD [ COLUMN ] [ IF NOT EXISTS ] column_name data_type [ COLLATE collation ] [ column_constraint [ ... ] ] DROP [ COLUMN ] [ IF EXISTS ] column_name [ RESTRICT | CASCADE ] ALTER [ COLUMN ] column_name [ SET DATA ] TYPE data_type [ COLLATE collation ] [ USING expression ] ALTER [ COLUMN ] column_name SET DEFAULT expression ALTER [ COLUMN ] column_name DROP DEFAULT ALTER [ COLUMN ] column_name { SET | DROP } NOT NULL ALTER [ COLUMN ] column_name ADD GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( sequence_options ) ] ALTER [ COLUMN ] column_name { SET GENERATED { ALWAYS | BY DEFAULT } | SET sequence_option | RESTART [ [ WITH ] restart ] } [...] ALTER [ COLUMN ] column_name DROP IDENTITY [ IF EXISTS ] ALTER [ COLUMN ] column_name SET STATISTICS integer ALTER [ COLUMN ] column_name SET ( attribute_option = value [, ... ] ) ALTER [ COLUMN ] column_name RESET ( attribute_option [, ... ] ) ALTER [ COLUMN ] column_name SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }
2.执行转换
[local:/data/run/pg12]:5120 pg12@testdb=# drop table t_bool;ERROR: table "t_bool" does not exist[local:/data/run/pg12]:5120 pg12@testdb=# create table t_bool(col bool);CREATE TABLE[local:/data/run/pg12]:5120 pg12@testdb=# alter table t_bool alter column col type smallint;ERROR: column "col" cannot be cast automatically to type smallintHINT: You might need to specify "USING col::smallint".[local:/data/run/pg12]:5120 pg12@testdb=#
提示指定使用"USING col::smallint",翻看PG的语法:
ALTER [ COLUMN ] column_name [ SET DATA ] TYPE data_type [ COLLATE collation ] [ USING expression ]
USING后面跟的是expression表达式,可以理解为如何转换的表达式,因此执行以下语句:
[local:/data/run/pg12]:5120 pg12@testdb=# alter table t_bool alter column col type smallint using col::int::smallint;ALTER TABLE
DONE!成功转换。
实际上,还可以更简化:
[local:/data/run/pg12]:5120 pg12@testdb=# alter table t_bool alter column col type smallint using col::int;ALTER TABLE
因为int到smallint是有转换的,不需要明确的int::smallint,但为了说明转换过程,加上该转换会清晰许多。
附录
测试脚本如下:
drop table t_bool;create table t_bool(col bool);alter table t_bool alter column col type smallint;alter table t_bool alter column col type smallint using col::int::smallint;
到此,关于"PostgreSQL中的bool类型转换为smallint的方法是什么"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
方法
类型
学习
更多
表达式
语法
过程
帮助
实用
成功
接下来
命令
实际
实际上
文章
理论
知识
篇文章
网站
脚本
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
平山软件开发项目管理
zte服务器图文管理
江西公路服务器
网络技术和销售
平板电脑怎么获得代理服务器
华为服务器多端口ip怎么配
哪个数据库可以获取英文期刊
数据库的scott用户
长春分销软件开发公司
网络安全立法的意见建议
mc服务器保存
数据库中姓名可采用默认值吗
播放五年级网络安全手抄报简单
软件开发转嵌入式容易吗
塞班软件开发语言
方舟生存进化买服务器
网络安全几年检修
喋血复仇与服务器的连接丢失
软件开发人员年度工作小结
店淘软件开发思路
Java实现杀死数据库进程
跨平台 软件开发 新技术
宁波云软件开发需要学什么
网络服务器怎么看ip
优秀教务管理软件开发
master服务器工具
如何进入西瓜星球迪哥的服务器
mysql酒店管理数据库
天津的360网络安全培训
上海软件开发人月报价