Skip to content

SQL-291 What is the ideal data type to use when storing latitude / longitude in a MySQL database? #366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- Create example table
CREATE TABLE Coordinates (
id INT AUTO_INCREMENT PRIMARY KEY,
location VARCHAR(255),
latitude DECIMAL(10, 8) NOT NULL,
longitude DECIMAL(11, 8) NOT NULL
);
-- Add example data
INSERT INTO Coordinates (location, latitude, longitude)
VALUES ('Eiffel Tower', 48.8584, 2.2945);
-- Query example data
SELECT * FROM Coordinates;
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- Create example table
CREATE TABLE Locations (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255),
coordinates POINT NOT NULL SRID 4326,
SPATIAL INDEX(coordinates)
);
-- Add example data
INSERT INTO Locations (name, coordinates)
VALUES ('Eiffel Tower', ST_PointFromText('POINT(2.2945 48.8584)', 4326));
INSERT INTO Locations (name, coordinates)
VALUES ('Arc de Triomphe', ST_PointFromText('POINT(2.2950 48.8738)', 4326));
-- Query example data
SELECT
ST_Distance_Sphere(t1.coordinates, t2.coordinates) AS Distance_in_Meters
FROM
Locations AS t1,
Locations AS t2
WHERE
t1.name = 'Eiffel Tower' AND t2.name = 'Arc de Triomphe';