Name

AddGeometryColumn — 将几何图形列添加到现有表格。

Synopsis

text AddGeometryColumn(varchar table_name, varchar column_name, integer srid, varchar type, integer dimension, boolean use_typmod=true);

text AddGeometryColumn(varchar schema_name, varchar table_name, varchar column_name, integer srid, varchar type, integer dimension, boolean use_typmod=true);

text AddGeometryColumn(varchar catalog_name, varchar schema_name, varchar table_name, varchar column_name, integer srid, varchar type, integer dimension, boolean use_typmod=true);

描述

将几何图形列添加到现有属性表中。这个 schema_name 表架构的名称。这个 srid 必须是对SPATIAL_REF_SYS表中条目的整数值引用。这个 type 必须是与几何类型对应的字符串,例如‘Polygon’或‘MULTILINESTRING’。如果架构名称不存在(或在当前SEARCH_PATH中不可见),或者指定的SRID、几何类型或尺寸无效,则会引发错误。

[Note]

已更改:2.0.0此函数不再更新GEOMETRY_COLUMNS,因为GEOMETRY_COLUMNS是从系统目录读取的视图。默认情况下,它也不创建约束,而是使用PostgreSQL的内置类型修饰符行为。因此,例如,使用此函数构建一个WGS84点列现在相当于: ALTER TABLE some_table ADD COLUMN geom geometry(Point,4326);

已更改:2.0.0如果需要旧的约束行为,请使用缺省值 use_typmod ,但将其设置为False。

[Note]

已更改:2.0.0视图不能再在GEOMETRY_COLUMNS中手动注册,但是,针对几何类型模表几何构建并在没有包装函数的情况下使用的视图将正确注册自身,因为它们继承了其父表列的类型模式行为。使用输出其他几何图形的几何函数的视图需要转换为类型模化几何图形,以便在GEOMETRY_COLUMNS中正确注册这些视图几何图形列。参考 Section 4.6.3, “手动注册几何图形列”

This method implements the OGC Simple Features Implementation Specification for SQL 1.1.

This function supports 3d and will not drop the z-index.

This method supports Circular Strings and Curves

增强:2.0.0引入了USE_TYPMOD参数。默认情况下创建TypeMod几何图形列,而不是基于约束。

示例

-- Create schema to hold data
CREATE SCHEMA my_schema;
-- Create a new simple PostgreSQL table
CREATE TABLE my_schema.my_spatial_table (id serial);

-- Describing the table shows a simple table with a single "id" column.
postgis=# \d my_schema.my_spatial_table
                                                         Table "my_schema.my_spatial_table"
 Column |  Type   |                                Modifiers
--------+---------+-------------------------------------------------------------------------
 id     | integer | not null default nextval('my_schema.my_spatial_table_id_seq'::regclass)

-- Add a spatial column to the table
SELECT AddGeometryColumn ('my_schema','my_spatial_table','geom',4326,'POINT',2);

-- Add a point using the old constraint based behavior
SELECT AddGeometryColumn ('my_schema','my_spatial_table','geom_c',4326,'POINT',2, false);

--Add a curvepolygon using old constraint behavior
SELECT AddGeometryColumn ('my_schema','my_spatial_table','geomcp_c',4326,'CURVEPOLYGON',2, false);

-- Describe the table again reveals the addition of a new geometry columns.
\d my_schema.my_spatial_table
                            addgeometrycolumn
-------------------------------------------------------------------------
 my_schema.my_spatial_table.geomcp_c SRID:4326 TYPE:CURVEPOLYGON DIMS:2
(1 row)

                                    Table "my_schema.my_spatial_table"
  Column  |         Type         |                                Modifiers
----------+----------------------+-------------------------------------------------------------------------
 id       | integer              | not null default nextval('my_schema.my_spatial_table_id_seq'::regclass)
 geom     | geometry(Point,4326) |
 geom_c   | geometry             |
 geomcp_c | geometry             |
Check constraints:
    "enforce_dims_geom_c" CHECK (st_ndims(geom_c) = 2)
    "enforce_dims_geomcp_c" CHECK (st_ndims(geomcp_c) = 2)
    "enforce_geotype_geom_c" CHECK (geometrytype(geom_c) = 'POINT'::text OR geom_c IS NULL)
    "enforce_geotype_geomcp_c" CHECK (geometrytype(geomcp_c) = 'CURVEPOLYGON'::text OR geomcp_c IS NULL)
    "enforce_srid_geom_c" CHECK (st_srid(geom_c) = 4326)
    "enforce_srid_geomcp_c" CHECK (st_srid(geomcp_c) = 4326)

-- geometry_columns view also registers the new columns --
SELECT f_geometry_column As col_name, type, srid, coord_dimension As ndims
    FROM geometry_columns
    WHERE f_table_name = 'my_spatial_table' AND f_table_schema = 'my_schema';

 col_name |     type     | srid | ndims
----------+--------------+------+-------
 geom     | Point        | 4326 |     2
 geom_c   | Point        | 4326 |     2
 geomcp_c | CurvePolygon | 4326 |     2

另请参阅

DropGeometryColumn, DropGeometryTable, Section 4.6.2, “几何图形_列视图”, Section 4.6.3, “手动注册几何图形列”