添加不存在的列以进行查看

时间:2014-10-31 13:32:51

标签: sql postgresql view

如果源表中不存在该字段,是否可以将SERIAL列添加到现有视图?我想获得一个名为OID的自动增量ID字段,该字段不能为NULL。

CREATE OR REPLACE VIEW public.osm_polygon AS 

SELECT planet_osm_polygon.osm_id::text AS osm_id, 
    planet_osm_polygon.tags -> 'osm_timestamp'::text AS osm_timestamp, 
    planet_osm_polygon.tags -> 'name:en'::text AS name_en, 
    planet_osm_polygon.access, planet_osm_polygon.admin_level, 
    planet_osm_polygon.aerialway, planet_osm_polygon.aeroway, 
    planet_osm_polygon.amenity, planet_osm_polygon.area, 
    planet_osm_polygon.barrier, planet_osm_polygon.bicycle, 
    planet_osm_polygon.brand, planet_osm_polygon.bridge, way
    planet_osm_polygon.boundary, planet_osm_polygon.building FROM planet_osm_polygon
WHERE st_isvalid(planet_osm_polygon.way) AND planet_osm_polygon.way && st_makeenvelope((-17.5)::double precision, 3.0::double precision, 8.7::double precision, 26.9::double precision, 4326);

这就是我的观点。

我需要添加一个包含自动增量字段的列,如Long Integers或Serial,不能为NULL。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

使用row_number

CREATE OR REPLACE VIEW public.osm_polygon AS 

SELECT planet_osm_polygon.osm_id::text AS osm_id, 
planet_osm_polygon.tags -> 'osm_timestamp'::text AS osm_timestamp, 
planet_osm_polygon.tags -> 'name:en'::text AS name_en, 
planet_osm_polygon.access, planet_osm_polygon.admin_level, 
planet_osm_polygon.aerialway, planet_osm_polygon.aeroway, 
planet_osm_polygon.amenity, planet_osm_polygon.area, 
planet_osm_polygon.barrier, planet_osm_polygon.bicycle, 
planet_osm_polygon.brand, planet_osm_polygon.bridge, way,
planet_osm_polygon.boundary, planet_osm_polygon.building,
row_number() over() as row_num
 FROM planet_osm_polygon
WHERE st_isvalid(planet_osm_polygon.way) AND planet_osm_polygon.way && st_makeenvelope((-17.5)::double precision, 3.0::double precision, 8.7::double precision, 26.9::double precision, 4326);