CREATE TABLE tb_geo(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(128) NOT NULL,
pnt POINT NOT NULL,
SPATIAL INDEX `spatIdx` (`pnt`) <!-- 1、创建表时创建空间索引 -->
)ENGINE=MYISAM DEFAULT CHARSET=utf8; <!-- 数据库表引擎设置为 MYISAM-->
<!-- 表描述信息-->
DESCRIBE tb_geo;
<!-- 插入数据 -->
INSERT INTO `tb_geo` VALUES(
NULL,
'a test string',
POINTFROMTEXT('POINT(15 20)'));
<!-- 查询 -->
SELECT id,NAME,ASTEXT(pnt) FROM tb_geo;
SELECT id,NAME,X(pnt),Y(pnt) FROM tb_geo;
<!-- 空间查询示例 -->
SELECT ASTEXT(pnt) FROM tb_geo WHERE MBRWITHIN(pnt,GEOMFROMTEXT('Polygon((0 0,0 30,30 30,30 0,0 0))'));
<!-- 删除表 -->
DROP TABLE tb_geo;
<!-- 几种空间对象比较方法 -->
SET @g1 = GEOMFROMTEXT('Polygon((0 0,0 3,3 3,3 0,0 0))');
SET @g2 = GEOMFROMTEXT('Point(1 1)');
<!-- 注意:比较的都是外包络几何类型对象 -->
<!-- 包含 -->
SELECT MBRCONTAINS(@g1,@g2), MBRCONTAINS(@g2,@g1), MBRCONTAINS(@g1,@g1);
<!-- 被包含 -->
SELECT MBRWITHIN(@g2,@g1),MBRWITHIN(@g1,@g2);
<!-- 不相交 -->
SELECT MBRDISJOINT(@g1,@g2);
<!-- 相等 -->
SELECT MBREQUAL(@g1,@g2);
<!-- 相交 -->
SELECT MBRINTERSECTS(@g1,@g2);
<!-- 重叠 -->
SELECT MBROVERLAPS(@g1,@g2);
<!-- 相切 -->
SELECT MBRTOUCHES(@g1,@g2);