1

Using the MyISAM storage engine in MySQL, I'd like to enforce the uniqueness of a POLYGON column in a table. Basically if I attempt to insert a duplicate, I want it to result in an error, or simply be ignored. With traditional column types this can be accomplished using the UNIQUE keyword, but with a spatial column this leads to errors like:

ERROR 1170 (42000): BLOB/TEXT column 'bounding_box' used in key specification without a key length

coming from this table definition:

CREATE TABLE IF NOT EXISTS quadrants ( id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, bounding_box POLYGON NOT NULL UNIQUE, centroid POINT NOT NULL UNIQUE, SPATIAL KEY bb_idx(bounding_box), SPATIAL KEY centroid_idx(centroid) ) ENGINE=MyISAM;

Is it possible to enforce uniqueness for spatial datatypes in MySQL?

EDIT: It has been suggested that this is a duplicate of MySQL error: key specification without a key length but I believe it is different. That question and its answers make no mention of spatial datatypes. I'm left to infer that POLYGON is actually stored as BLOB and thus subject to similar constraints. But I still don't know if there is any other way to enforce uniqueness of POLYGON---or whether there are practical workarounds if that is not the case.

The other question addresses the same error, but in a different enough context that I feel a separate question is useful.

Community
  • 1
  • 1
Josh Hansen
  • 917
  • 1
  • 9
  • 20
  • there is no any search efforts – Gouda Elalfy Jan 07 '16 at 09:04
  • Possible duplicate of [MySQL error: key specification without a key length](http://stackoverflow.com/questions/1827063/mysql-error-key-specification-without-a-key-length) – Gouda Elalfy Jan 07 '16 at 09:04
  • Check: [11.5.3.6 Creating Spatial Indexes](https://dev.mysql.com/doc/refman/5.7/en/creating-spatial-indexes.html) and [13.1.11 CREATE INDEX Syntax](https://dev.mysql.com/doc/refman/5.7/en/create-index.html). – wchiquito Jan 07 '16 at 09:10

1 Answers1

0

As spatial columns are stored like blob columns, you should create two indexes: a non spatial index with a length for exact queries/uniqueness, and a spatial index for range queries. Furthermore, if your polygons aren’t overlapping, a very small length will be enough.

Marco Marsala
  • 2,332
  • 5
  • 25
  • 39