简体   繁体   English

如何在PostgreSQL中的视图中更改表列数据类型

[英]how to change a table column datatype in a view in postgresql

))Hi all, I have an 'Interval' datatype column in mytable that I would like to change to 'character varying' datatype in mytableview. )大家好,我在mytable中有一个“间隔”数据类型列,我想在mytableview中更改为“字符变化”数据类型。

I know I can change it using... 我知道我可以使用...进行更改

to_char(interval, 'yy-mm-dd HH24:MI:SS.MS');

But I would like to leave that column datatype as 'interval' in mytable, and make a view of that table changing the 'interval' column to 'character varying' datatype in mytableview. 但是我想在mytable中将该列数据类型保留为“ interval”,并在mytableview中对该表进行更改,以将“ interval”列更改为“ character changes”数据类型。

was so easy... 太简单了...

CREATE TABLE mytable (length INTERVAL);

INSERT INTO mytable (length) VALUES (INTERVAL '1 minute');

CREATE VIEW myview AS
  SELECT to_char(length, 'yy-mm-dd HH24:MI:SS.MS') AS length
  FROM mytable;

Is that possible? 那可能吗? Thanks Advanced. 谢谢先进。

You can specify your view just like you'd specify any other query without needing to change the underlying table types. 您可以像指定其他查询一样指定视图,而无需更改基础表类型。 Here's a Fiddle with your example. 这是您的例子的小提琴

CREATE TABLE mytable (length INTERVAL);

INSERT INTO mytable (length) VALUES (INTERVAL '1 minute');

CREATE VIEW myview AS
  SELECT to_char(length, 'yy-mm-dd HH24:MI:SS.MS') AS length
  FROM mytable;

SELECT * FROM mytable;
-- 0 years 0 mons 0 days 0 hours 1 mins 0.00 secs

SELECT * FROM myview;
-- 00-00-00 00:01:00.000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更改 Postgresql 中列的数据类型 - Change datatype of column in Postgresql 如何使用 knex 迁移更改 postgresql 数据库中列的数据类型? - How to change datatype of a column in postgresql database using knex migration? 在 postgresql 中加载 csv 时如何更改列数据类型 - How to change column datatype when loading csv in postgresql 如何在 PostgreSQL 8.4 中将列数据类型从字符更改为数字 - How to change column datatype from character to numeric in PostgreSQL 8.4 在 PostgreSQL 中将列的数据类型更改为 integer(n/a 列) - Change datatype of column to integer (column with n/a) in PostgreSQL 如何在postgresql中更新xml数据类型列 - how to update xml datatype column in postgresql 创建材料视图时,如何CAST数据类型以及使用CASE更改值? (PostgreSQL) - How to CAST datatype and also change value with CASE when creating MATERIALIZED VIEW? (PostgreSQL) 如何使用to_sql方法使用postgresql和pandas用点数据类型的列填充表? - How to use to_sql method to fill table with column with point datatype using postgresql and pandas? 如何在Postgresql数据库中创建具有一列枚举数据类型的表? - How can i create a table with one column of enum datatype in postgresql database? PostgreSQL-如何实现/将视图用作表中的计算列 - postgresql - How to implement/use a view as a calculated column into a table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM