Skip to content

Commit f83f1f5

Browse files
authored
Merge pull request #366 from Deepak-Vohra/main
SQL-291 What is the ideal data type to use when storing latitude / longitude in a MySQL database?
2 parents ee2e993 + 781e267 commit f83f1f5

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- Create example table
2+
CREATE TABLE Coordinates (
3+
id INT AUTO_INCREMENT PRIMARY KEY,
4+
location VARCHAR(255),
5+
latitude DECIMAL(10, 8) NOT NULL,
6+
longitude DECIMAL(11, 8) NOT NULL
7+
);
8+
-- Add example data
9+
INSERT INTO Coordinates (location, latitude, longitude)
10+
VALUES ('Eiffel Tower', 48.8584, 2.2945);
11+
-- Query example data
12+
SELECT * FROM Coordinates;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-- Create example table
2+
CREATE TABLE Locations (
3+
id INT AUTO_INCREMENT PRIMARY KEY,
4+
name VARCHAR(255),
5+
coordinates POINT NOT NULL SRID 4326,
6+
SPATIAL INDEX(coordinates)
7+
);
8+
-- Add example data
9+
INSERT INTO Locations (name, coordinates)
10+
VALUES ('Eiffel Tower', ST_PointFromText('POINT(2.2945 48.8584)', 4326));
11+
INSERT INTO Locations (name, coordinates)
12+
VALUES ('Arc de Triomphe', ST_PointFromText('POINT(2.2950 48.8738)', 4326));
13+
-- Query example data
14+
SELECT
15+
ST_Distance_Sphere(t1.coordinates, t2.coordinates) AS Distance_in_Meters
16+
FROM
17+
Locations AS t1,
18+
Locations AS t2
19+
WHERE
20+
t1.name = 'Eiffel Tower' AND t2.name = 'Arc de Triomphe';

0 commit comments

Comments
 (0)