Home > Software engineering >  What is the suitable value to initialize an empty column of type geometry
What is the suitable value to initialize an empty column of type geometry

Time:11-12

i want to set a default value to a geometry column indicates that it is null or empty.for example, a string initially is initialized to null or "" to indicate being void/empty. what is the equivalent to null/empty for geometries. what is the value to be set to a geometry column inidicates that it is empty. i tried 0 and "" but they are not allowed to be inserted in a column of type geometry

CodePudding user response:

You could add a DEFAULT in the geometry column with an empty geometry, e.g. for POINT:

CREATE TABLE t (
  id int,
  geom geometry(point,4326) NOT NULL DEFAULT 'POINT EMPTY'
);

It applies also to other geometry types, e.g.:

SELECT 
  'POINT EMPTY'::geometry,
  'POLYGON EMPTY'::geometry,
  'LINESTRING EMPTY'::geometry,
  'MULTILINESTRING EMPTY'::geometry,
  'MULTIPOLYGON EMPTY'::geometry,
  'MULTIPOINT EMPTY'::geometry;

-[ RECORD 1 ]----------------------------------------
geometry | 0101000000000000000000F87F000000000000F87F
geometry | 010300000000000000
geometry | 010200000000000000
geometry | 010500000000000000
geometry | 010600000000000000
geometry | 010400000000000000

Demo: db<>fiddle

  • Related