Introduction to MySQL GEOMETRY Data Type

GEOMETRY is one of the data types in MySQL used for storing spatial data. It can store geometric objects such as points, lines, polygons, etc., and supports storage, computation, and querying of spatial data.


In MySQL, the GEOMETRY data type can be defined using the following syntax:


Use Cases

The GEOMETRY data type is suitable for scenarios where spatial data needs to be stored and processed, such as geographic information systems (GIS), location services, map applications, etc.


Below are two examples of using the GEOMETRY data type.

Example 1

Assume there is a table called cities that contains the names and coordinates (longitude and latitude) of various cities. The GEOMETRY data type can be used to store the coordinates.

First, create the cities table:

  name VARCHAR(50) NOT NULL,

Next, insert some data:

INSERT INTO cities (name, location) VALUES
  ('New York', POINT(-73.935242, 40.730610)),
  ('Los Angeles', POINT(-118.243683, 34.052235)),
  ('Chicago', POINT(-87.629798, 41.878114)),
  ('Houston', POINT(-95.369803, 29.760427)),
  ('Phoenix', POINT(-112.074037, 33.448376));

Here, the POINT function is used to create a point object, with the first parameter being the longitude and the second parameter being the latitude.

Then, the following statement can be used to query the city closest to New York:

SELECT name, ST_DISTANCE(location, POINT(-73.935242, 40.730610)) AS distance
FROM cities
ORDER BY distance

This statement uses the ST_DISTANCE function to calculate the distance between two points. The result is:

| name      | distance      |
| New York  | 0.0000000000  |

As we can see, the result is New York itself, indicating that the distance from New York to itself is 0.

Example 2

Assume we have a table called locations that contains location names and coordinate information:

CREATE TABLE locations (
  name VARCHAR(50),
  location GEOMETRY

We can insert some data, for example:

INSERT INTO locations (name, location) VALUES
('Empire State Building', POINT(-73.9857, 40.7484)),
('Golden Gate Bridge', LINESTRING(-122.4787 37.8203, -122.4715 37.8115, -122.4665 37.8090, -122.4582 37.8084, -122.4484 37.8094)),
('Mount Everest', POINT(86.9250, 27.9881));

In this example, we use the POINT and LINESTRING subtypes to store location information, and different subtypes can be used as needed. We can use the following query to find the location closest to a certain point:

SELECT name, ST_Distance_Sphere(location, POINT(-118.409, 33.943)) AS distance
FROM locations
ORDER BY distance

The query will return the nearest location to the point (-118.409, 33.943) and its distance using the ST_Distance_Sphere function, which calculates the spherical distance between two points.


The MySQL GEOMETRY data type is a powerful data type that allows for storage and manipulation of geometric objects in a database. This data type can store various types of geometric objects such as points, lines, polygons, etc., and supports a wide range of geometric functions and operations. Although the GEOMETRY data type can efficiently store and process geometric objects, it requires higher storage space and complex query statements.

In practical applications, the GEOMETRY data type can be widely used in geographic information systems (GIS), mapping applications, and other applications that require processing of geometric data. By using the GEOMETRY data type in MySQL, applications can become more flexible and efficient, supporting various spatial queries and analysis.

In conclusion, understanding and mastering the GEOMETRY data type in MySQL is crucial for developing efficient, flexible, and powerful applications.