Rename column in a view : bug or feature? - postgresql

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

Related

Replacing table and renaming primary key - Postgres

To preface, I am trying to replace an entire table with a new table with same columns, but with updated values.
I have the following SQL code:
BEGIN;
ALTER TABLE "original" RENAME TO "original_old";
ALTER TABLE "original_new" RENAME TO "original";
ALTER TABLE "original" RENAME CONSTRAINT "temp_original_id" to "original_id";
DROP TABLE "original_old";
COMMIT;
Output:
ERROR: constraint "temp_original_id" for table "original" does not exist
However, if I do the following before the last ALTER statement:
SELECT * from original;
I see temp_original_id present in the table.
I can't seem to find any other sources that lead me to updating primary key (at least that worked)
The table I am replacing also has dependencies with other tables.. So I was wondering if this would be a viable solution to even begin with
Did you mean ALTER TABLE "original" RENAME COLUMN "temp_original_id" to "original_id"; ?

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 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;

Delete column in hive table

I am working with hive version 0.9 and I need delete columns of a hive table. I have searched in several manuals of hive commands but I only I have found commands to version 0.14. Is possible to delete a column of a hive table in hive version 0.9? What is the command?
Thanks.
We can’t simply drop a table column from a hive table using the below statement like sql.
ALTER TABLE tbl_name drop column column_name ---- it will not work.
So there is a shortcut to drop columns from a hive table.
Let’s say we have a hive table.
From this table I want to drop the column Dob. You can use the ALTER TABLE REPLACE statement to drop a column.
ALTER TABLE test_tbl REPLACE COLUMNS(ID STRING,NAME STRING,AGE STRING); you have to give the column names which you want to keep in the table
There isn't a drop column or delete column in Hive.
A SELECT statement can take regex-based column specification in Hive releases prior to 0.13.0, or in 0.13.0 and later releases if the configuration property hive.support.quoted.identifiers is set to none.
That being said you could create a new table or view using the following:
drop table if exists database.table_name;
create table if not exists database.table_name as
select `(column_to_remove_1|...|column_to_remove_N)?+.+`
from database.some_table
where
...
;
This will create a table that has all the columns from some_table except the columns named column_to_remove_1, ... , to column_to_remove_N. You can also choose to create a view instead.
ALTER TABLE table_name REPLACE COLUMNS ( c1 int, c2 String);
NOTE: eliminate column from column list. It will keep matched columns and removed unmentioned columns from table schema.
we can not delete column from hive table . But droping a table(if its external) in hive and the recreating table(with column excluded) ,wont delete ur data .
so what can u do is(if u dont have table structure) run this command :
show create table database_name.table_name;
Then you can copy it and edit it (with column eliminated).Afterwards you can do as per invoke the shell
table details are empid,name,dept,salary ,address. i want remove address column. Just write REPLACE COLUMNS like below query
jdbc:hive2://> alter table employee replace columns(empid int, name string,dept string,salary int);
As mentioned before, you can't drop table using an alter statement.
Alter - replace is not guaranteed to work in all the cases.
I found the best answer for this here:
https://stackoverflow.com/a/48921280/4385453

PostgreSQL 9.3: ALTER VIEW AS

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;