简体   繁体   English

如何改变PostgreSQL中的视图

[英]How to ALTER a view in PostgreSQL

PostgreSQL does not allow altering a view (ie adding column, changing column orders, adding criterie etc.) if it has dependent objects. 如果PostgreSQL具有依赖对象,则不允许更改视图(即添加列,更改列顺序,添加标准等)。 This is really getting annoying since you have to write a script to: 这真的很烦人,因为你必须写一个脚本:

  1. Drop all the dependent objects, 删除所有依赖对象,
  2. Alter the view, 改变观点,
  3. Recreate all the dependent objects back again. 再次重新创建所有依赖对象。

I understand that postgreSQL developers have very reasonable concerns to prevent altering views. 我知道postgreSQL开发人员有非常合理的顾虑来防止改变视图。 But do you guys have any scripts/shot-cuts to do all those manual stuff in a single run? 但是你们有没有任何脚本/镜头切换来一次性完成所有这些手动的东西?

Adding new columns isn't a problem, changing datatypes or changing the order of the columns, that's where you get problems. 添加新列不是问题,更改数据类型或更改列的顺序,这是您遇到问题的地方。

  1. Don't change the order, it's not that important anyway, just change your query: 不要改变顺序,无论如何都不重要,只需更改您的查询:

    SELECT a, b FROM view_name; SELECT a,b FROM view_name;

    SELECT b, a FROM view_name; SELECT b,FROM view_name;

  2. When you have to change a datatype of a column, you have to check the depend objects as well. 必须更改列的数据类型时,还必须检查depend对象。 These might have problems with this new datatype. 这些新数据类型可能存在问题。 Just get the definition of this object and recreate after the changes. 只需获取此对象的定义并在更改后重新创建。 The information_schema and pg_catalog help you out. information_schema和pg_catalog帮助您解决问题。

  3. Make all changes within a single transaction. 在单个事务中进行所有更改。

If I place a addtional "drop view xyz; commit;" 如果我放置一个addtional“drop view xyz; commit;” before the "create or replace view xyz as ..." statement, at least in many cases I resolve the blocking problem described above. 在“创建或替换视图xyz为...”语句之前,至少在很多情况下我解决了上面描述的阻塞问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM