Answer a question

I have a view, one of he column is timetaken type integer i want to change it as numeric.For this I used below syntax

ALTER VIEW view_timesheets  ALTER COLUMN timetaken type numeric;

When I run this I got the exception as

"view_timesheets" is not a table, composite type, or foreign table

Please explain how to alter column type.Thank You

Answers

It is not possible. You will have to recreate the view by providing its complete definition. Also note that you cannot even CREATE OR REPLACE VIEW when you change the types of the columns. If you have views that depend on the view that changes you will have to DROP / CREATE them also.

In my company we use the strategy where everything that is recreatable in a database (like views, functions, etc.) is stored in a bunch of large SQL files which we execute everytime anything changes in the underlying table structures, so we don't have to care for dependant views.

The view part in these files is basically like:

DROP VIEW IF EXISTS vw_a CASCADE;
CREATE OR REPLACE VIEW vw_a AS
...;

DROP VIEW IF EXISTS vw_b_depending_on_a CASCADE;
CREATE OR REPLACE VIEW vw_b_depending_on_a AS
...;

Of course the second CASCADE as well as the OR REPLACE seems useless, but they maek it possible to copy&paste changed definitions easily into a running dev database without much thinking.

Logo

PostgreSQL社区为您提供最前沿的新闻资讯和知识内容

更多推荐