Postgresql流水帳(第六天):view

來源:互聯網
上載者:User

標籤:

CREATE OR REPLACE view_name

AS

query

DROPVIEW[IFEXISTS]view_name;

一個複雜的 query:

SELECT cu.customer_id AS id,

????(((cu.first_name)::text || ‘ ‘::text)||(cu.last_name)::text) AS name,

????a.address,

????a.postal_code AS "zip code",

????a.phone,

????city.city,

????country.country,

????????CASE

????????????WHEN cu.activebool THEN ‘active‘::text

????????????ELSE ‘‘::text

????????END AS notes,

????cu.store_id AS sid

?? FROM (((customercu

???? JOIN addressa ON ((cu.address_id=a.address_id)))

???? JOIN city ON ((a.city_id=city.city_id)))

???? JOIN country ON ((city.country_id=country.country_id)));

?

將複雜的 query 儲存為 view:

CREATE VIEW customer_master AS

SELECT cu.customer_id AS id,

????(((cu.first_name)::text || ‘ ‘::text)||(cu.last_name)::text) AS name,

????a.address,

????a.postal_code AS "zip code",

????a.phone,

????city.city,

????country.country,

????????CASE

????????????WHEN cu.activebool THEN ‘active‘::text

????????????ELSE ‘‘::text

????????END AS notes,

????cu.store_id AS sid

?? FROM (((customercu

???? JOIN addressa ON ((cu.address_id=a.address_id)))

???? JOIN city ON ((a.city_id=city.city_id)))

???? JOIN country ON ((city.country_id=country.country_id)));

?

A PostgreSQL view is updatable when it meets the following conditions:

  • The defining query of the view must has exactly one entry in the?FROM?clause, which can be a table or another updatable view.
  • The defining query must not contain one of the following clauses at top level:?GROUP BY,HAVING,?LIMIT, OFFSET,?DISTINCT, WITH,?UNION, INTERSECT, and EXCEPT.
  • The selection list must not contain any window function or?set-returning function?or any aggregate function such as?SUM,?COUNT,?AVG,?MIN,?MAX, etc.

?

CREATE VIEW usa_cities AS SELECT

city,

country_id

FROM

city

WHERE

country_id=103;

?

?

PostgreSQL Materialized Views

CREATE MATERIALIZED VIEW view_name

AS

query

WITH [NO] DATA;

?

REFRESHMATERIALIZEDVIEWview_name;

REFRESHMATERIALIZEDVIEWCONCURRENTLYview_name;

When you refresh data for a materialized view, PosgreSQL locks the entire table therefore you cannot query data against it. To avoid this, you can use the?CONCURRENTLY?option.

1

REFRESH MATERIALIZED VIEW CONCURRENTLY view_name;

With?CONCURRENTLY?option, PostgreSQL creates a temporary updated version of the materialized view, compares two versions, and performs?INSERT?and?UPDATE?only the differences. You can query against the materialized view while it is being updated. One requirement for usingCONCURRENTLY?option is that the materialized view must have a?UNIQUE?index. Notice thatCONCURRENTLY?option is only available from PosgreSQL 9.4.

Postgresql流水帳(第六天):view

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.