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;
Related
It often happens that the original name something is given is not the best name. Maybe requirements shifted slightly, or maybe as time went on, a better understanding of the concept being represented developed. Sometimes one name is used in developing a feature, but testing with real users reveals that a better name is needed, and it'd be nice to have the names for things in the DB match the names used in the UI.
PostgreSQL lets you rename tables and columns via alter table, but often this isn't feasible in a production without significant downtime. If existing clients are using the old name, you can't just yank it out from under them.
I was hoping there was some way to "add a name" to a table or column, so that old_name and new_name will both work, and then at a later time remove the old name. Then a name migration could work like this:
add new_name
modify all clients to use new_name instead of old_name
once all clients have been updated, remove old_name
Is there a way to do this? If not, is there a recommended procedure for renaming a column while minimizing downtime? How about a table?
Some recipes for renaming tables and/or columns in a production system that seem to work. However, I've only tested these on a small test database, not on a large production system. Renaming and view creation are both supposed to be very fast, though.
Renaming a table
Rename the table, and temporarily add an updatable view with the old name:
begin;
alter table old_table rename to new_table;
create view old_table as select * from new_table;
commit;
Migrate clients to use the new table name.
Once all clients are migrated, drop the view:
drop view old_table;
Renaming columns without renaming the table
Renaming a column without renaming the table is a bit more complicated,
because we can't have a view shadow a table (apparently).
Rename the column(s), temporarily rename the table, and add an updatable
view that adds the old name for the column with the table's correct name:
begin;
alter table my_table rename column old_name to new_name;
alter table my_table rename to my_table_tmp;
create view my_table as
select *, new_name as old_name from my_table_tmp;
commit;
Migrate clients to use the new column name.
Once all clients are migrated, drop the view, rename the table back:
begin;
drop view my_table;
alter table my_table_tmp rename to my_table;
commit;
Renaming a table and some of its columns simultaneously
Rename the table and columns, and temporarily add an updatable view with the old name and old columns:
begin;
alter table old_table rename to new_table;
alter table new_table rename column to old_name to new_name;
create view old_table as
select *, old_name as new_name from new_table;
commit;
Instead of select *, old_name as new_name, it might be better to only have
the original set of columns, so clients have to migrate to the new table
name to get the new column names:
create view old_table as
select unchanged_name, old_name as new_name from new_table;
Migrate clients to use the new table and column names.
Once all clients are migrated, drop the view:
drop view old_table;
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
);
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
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;
I have a view view_a in my database on which several other views depend (view_b, view_c, etc.)
I need to convert view_a into a table, because I no longer want the information in this relation to be dynamic and I need the capability to edit rows manually.
Can I replace view_a with a table without doing a DROP CASCADE and redefining all views that reference view_a?
Clarification: I want view_b and view_c to continue to reference view_a (now a table). I want to replace a view with a table, not have a table in addition to a view.
I was able to resolve this without tracking down and redefining all objects that depend on view_a, at the expense of adding one level of useless redirection.
-- create a copy of the result of view_a
CREATE TABLE table_a AS SELECT * FROM view_a;
-- redefine view_a to its own result
CREATE OR REPLACE VIEW view_a AS SELECT * FROM table_a;