rubyprince
rubyprince

Reputation: 17793

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?

Upvotes: 0

Views: 59

Answers (1)

Schwern
Schwern

Reputation: 164659

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

Upvotes: 2

Related Questions