在PostgreSQL中将列数据类型从Text更改为Integer [复制]
发布时间:2020-12-13 16:19:12 所属栏目:百科 来源:网络整理
导读:参见英文答案 Rails Migrations: tried to change the type of column from string to integer6个 我使用以下查询将列的数据类型从文本更改为整数但获取错误: alter table a.attend alter column terminal TYPE INTEGER ; ERROR: column “terminal” canno
参见英文答案 >
Rails Migrations: tried to change the type of column from string to integer6个
我使用以下查询将列的数据类型从文本更改为整数但获取错误: alter table a.attend alter column terminal TYPE INTEGER ;
create table test(id varchar ); insert into test values('1'); insert into test values('11'); insert into test values('12'); select * from test --Result-- id character varying -------------------------- 1 11 12 您可以从上表中看到我使用了数据类型 – 字符因id而异 ALTER TABLE test ALTER COLUMN id TYPE integer; 但它返回:
这意味着我们不能简单地更改数据类型,因为列中已存在数据.由于数据类型为字符变化,Postgres不能指望它为整数,尽管我们只输入了整数.所以现在,正如Postgres建议我们可以使用USING表达式将数据转换为整数. ALTER TABLE test ALTER COLUMN id TYPE integer USING (id::integer); 有用. 所以你应该使用 alter table a.attend alter column terminal TYPE INTEGER USING (terminal::integer) ; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- iphone – 更改MFMailComposeViewController的标
- cocos2dx[3.2](17)——简单绘图DrawNode
- c# – MVC 5 ViewModel不像在MVC 4中那样工作
- oracle 时间参数格式
- ruby-on-rails – Paperclip – 图像上传错误:“
- cocos2d-x 3.3 Sprite3D人物换装代码解析
- c库函数获取活动线程数
- R语言:文本(字符串)处理与正则表达式
- Swift Optionals & Implicitly Unwrapped Op
- Flex Spark List with custom scroll bar and it
热点阅读