重命名视图中的列:错误或功能?

时间:2015-07-31 08:27:53

标签: postgresql views materialized-views

我在Postgresql 9.4物化视图中的列名称上犯了一个错误。

我看了一下文档,语法是:

ALTER MATERIALIZED VIEW my_view_name
RENAME COLUMN old_name 
TO new_name;

工作正常。

然后我在 - 物化视图中遇到了相同的拼写错误。我对请求进行了复制粘贴,忘记删除关键字MATERIALIZED并且......它有效!

Weirder,如果我尝试:

ALTER VIEW my_view_name
RENAME COLUMN old_name 
TO new_name;

它不起作用,我收到语法错误!

换句话说:我似乎只能使用物化视图中的语法来更改普通视图的列名。

这是一个错误还是一个功能?

重现的步骤:

1)创建一个简单的表

CREATE TABLE films (
  code        char(5) CONSTRAINT firstkey PRIMARY KEY,
  title       varchar(40) NOT NULL
);

2)创建一个简单的选择视图:

CREATE VIEW view_films AS SELECT * FROM films;

3)尝试重命名一列:

ALTER VIEW view_films RENAME COLUMN title TO new_title;
  

错误:语法错误在或附近" COLUMN"

4)尝试相同但使用关键字MATERIALIZED

ALTER MATERIALIZED VIEW view_films RENAME COLUMN title TO new_title;
  

ALTER TABLE

1 个答案:

答案 0 :(得分:1)

我对Usage: si viewcps options... issue|issue:change package id...; options are: --fields=field1[:width1],field2[:width2]... where fieldn can be any of: closeddate,cptype,creationdate,deployrequestid,deployrequeststate,deploytarget,description,id,issue,propagated,propagatedby,siserver,stage,stagingsystem,state,summary,user The fields to be displayed --filter=user:name issueid:issue state[:closed|:open|:submitted|:accepted|:rejected|:discarded|:commitfailed] closeddate:<date> creationdate:<date> membertype[:member|:subproject] member:<expression> project:<expression> variant:<expression> mainline description:<expression> summary:<expression> typemodifier[:committed|:pending] type[:add|:addfromarchive|:drop|:import|:exclusivelock|:nonexclusivelock|:renamefrom|:renameto|:movememberfrom|:movememberto|:update|:updatearchive|:updaterevision|:createsubproject|:addsubproject|:addsharedsubproject|:configuresubprojectfrom|:configuresubprojectto|:movesubprojectfrom|:movesubprojectto|:dropsubproject] hasissue pendingreviewby:name acceptedby:name[;<date>] rejectedby:name[;<date>] cptype[:development|:propagation|:deploy|:staging|:resolution] stagingsystem:<expression> stage:<expression> deploytarget:<expression> deployrequeststate[|:cancelled|:cleanedup|:cleaningup|:cleanupfailed|:created|:deployed|:executed|:executing|:packageactionsfailed|:packagecontentfailed|:packagingactions|:packagingcontent|:prepared|:preparing|:queuedonsource|:queuedontarget|:readytodeploy|:readytotransfer|:rollbackfailed|:rolledback|:rollingback|:stopped|:transferfailed|:transferring] deployrequestid:<expression> The filter used to select change packages --height=value The height in pixels of the windows --myReviews Show the change packages awaiting review by current user --query=value The query used to select change packages --width=value The width in pixels of the windows -x value The x location in pixels of the window -y value The y location in pixels of the window -? Shows the usage for a command --[no]batch Control batch mode (no user interaction in batch mode) --cwd=value Act as if command executed in specified directory -F value Read the selection from a specified file --forceConfirm=[yes|no] Specify an answer to all confirmation questions -g User interaction should happen via the GUI --gui User interaction should happen via the GUI --hostname=value Hostname of server -N Responds to all confirmations with "no" --no Responds to all confirmations with "no" --password=value Credentials (e.g., password) to login with --[no]persist Control persistence of CLI views --port=value TCP/IP port number of server --quiet Control status display --selectionFile=value Read the selection from a specified file --settingsUI=[gui|default] Control UI for command options --status=[none|gui|default] Control status display --usage Shows the usage for a command --user=value Username to login to server with -Y Responds to all confirmations with "yes" --yes Responds to all confirmations with "yes" 不确定,但对于正常观点;视图不存储任何数据,而只是一个保存的MATERIALIZED VIEW查询,并且在说SELECT时它除了对目标表运行基础select * from view_name语句外什么都不做。

因此,应该针对创建视图而不是视图的表触发更改列名的SELECT语句;它应该工作得很好。请尝试以下:

ALTER

对于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; 重命名列,可能会导致存储数据。

关于您的评论:您应该在MATERIALIZED VIEW表架构之后重新创建视图定义。检查这个小提琴http://sqlfiddle.com/#!15/9ebe1/1