PostgreSQL 9.3: ALTER VIEW AS - postgresql

I am using PostgreSQL 9.3 version.
I need to alter the view because of the table have been changed(added more columns) .
Well! I have tried the following script which does not work for me:
ALTER VIEW View1 AS SELECT * FROM Table1;
Got an error:
Syntax error near 'AS'

You have to drop and recreate the view:
DROP VIEW IF EXISTS View1;
CREATE VIEW View1 AS SELECT * FROM Table1;
If you only added columns, without renaming already existing columns or change the type, you could use CREATE OR REPLACE without DROP VIEW first, but is safer to drop explicitly and than recreate the view.
Using only CREATE OR REPLACE would throw a error if the order, the name or the type of the columns was changed.

I found a code like this and fixed the problem
CREATE OR REPLACE VIEW view_name AS
SELECT a_column FROM a_table;

Related

add a column to a table which just references an existing column

Is there a way to add a column alias to an existing table, which just references another existing column in the table? such that reads and writes to the new column name will go to the existing column name. Sort of how a view in postgres can act as a read / write alias:
create view temp_order_contacts as (select * from order_emails)
This will make read / write possible to order_emails table but by calling temp_order_contacts instead.
Is there something similar but for columns?
Assuming this is for backwards compatibility; you want to rename a column, but you also want to existing queries to still work.
You can rename the table and create a view with the original name.
-- Move the existing table out of the way.
alter table some_table rename to _some_table;
-- Create a view in its place.
create view some_table as (
select
*,
-- provide a column alias
some_column as some_other_column
from _some_table
);

How to migrate dependent views when deep copying the table

I am adding distkey/sortkey to all my tables in redshift and would like to automate this. I am doing the following:
ALTER TABLE table RENAME TO tmp_table;
CREATE TABLE table
distkey(id)
sortkey(id)
AS
select * from tmp_table;
DROP TABLE tmp_table;
This works great, except the views don't get migrated. When you ALTER TABLE, the existing views would point to the tmp_table. Ideally I want to restore the views to the way before, possibly in the same query transaction or as part of a script.
In your migration procedure:
For each view execute:
select definition from pg_views where viewname = 'my_view';
And store all the results.
Execute for all views:
drop view 'my_view'; // or rename only to have a rollback option
Alter your tables
Execute the create view commands obtained in step 2
Just drop and recreate the views. That could be part of the script.
Views might not form part of a transaction, so some testing might be required.

how to rename view column in oracle 10g?

Good evening
Hello everyone
How to rename view column in oracle.
i have two table
and create view of this table. can we rename of view column.
You can't modify the name of a column in an existing view; there is no alter view rename ... clause similar to alter table rename ....
You will need to drop and recreate the view. When you recreate it you can either specify the view column names explicitly, with whatever names you want, e.g.:
create view your_view (new_name) as select old_name from your_table;
or use column aliases in the view query, e.g.:
create view your_view as select old_name as new_name from your_table;

Rename column in a view : bug or feature?

I made a mistake on the name of a column in a Postgresql 9.4 materialized view.
I looked at the documentation, the syntax is :
ALTER MATERIALIZED VIEW my_view_name
RENAME COLUMN old_name
TO new_name;
It worked fine.
Then I had the same typo in a non-materialized view. I made a copy paste of the request, forgot to remove the key-word MATERIALIZED and... it worked!
Weirder, if I try :
ALTER VIEW my_view_name
RENAME COLUMN old_name
TO new_name;
It does not work, I get a syntax error!
In other words : it seems that I can only change the column name of a normal view by using the syntax from the materialized view.
Is that a bug or a feature?
Steps to reproduce :
1) Create a simple table
CREATE TABLE films (
code char(5) CONSTRAINT firstkey PRIMARY KEY,
title varchar(40) NOT NULL
);
2) Create a view as a simple select :
CREATE VIEW view_films AS SELECT * FROM films;
3) Try to rename a column :
ALTER VIEW view_films RENAME COLUMN title TO new_title;
ERROR: syntax error at or near "COLUMN"
4) Try the same but with the keyword MATERIALIZED :
ALTER MATERIALIZED VIEW view_films RENAME COLUMN title TO new_title;
ALTER TABLE
I am not sure with MATERIALIZED VIEW but for a normal view; the view doesn't store any data rather it's just a saved SELECT query and upon saying select * from view_name it does nothing but running the underlying SELECT statement against the target table.
So the ALTER statement to change column name should be fired against the table on which the view is created and not on the view; and it should work just fine. Try below:
CREATE TABLE films (
code char(5) CONSTRAINT firstkey PRIMARY KEY,
title varchar(40) NOT NULL
);
CREATE VIEW view_films AS SELECT * FROM films;
ALTER TABLE films RENAME COLUMN title TO new_title;
For a MATERIALIZED VIEW renaming column will be possible cause, it does store data.
On your comment: you should re-create the view definition after ALTERING the table schema. Check this fiddle http://sqlfiddle.com/#!15/9ebe1/1

How To change or alter column type in Views using PostgreSQL?

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
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.
I have also faced a similar problem while converting the column type of view.
I used the CAST() operator to convert the type from Integer to Varchar(5).
I had a column named age which is of type Integer in my table. So the view query created using that table was also having the type as Integer. So I used the CAST() operator in my view query to change the column type.
CASE
WHEN vhcl_insp_dtls.age = 0 THEN CAST('NEW' AS VARCHAR(5))
ELSE CAST(vhcl_insp_dtls.age AS VARCHAR(5))
END AS age,
So In this way, you can modify your view query without dropping it.