1

INPUT:

CREATE TABLE dist
-> SELECT ST_DISTANCE(POINT(x1,y1),POINT(x2,y2))
-> FROM config;

OUTPUT:

+------------------------------------------+
| ST_DISTANCE(POINT(x1,y1) , POINT(x2,y2)) |
+------------------------------------------+
|                        140.0071426749364 |
|                       139.30183056945089 |
|                        138.6001443000692 |
|                       137.90213921473443 |
|                       137.20787149431334 |
+------------------------------------------+

RENAME COLUMN INPUT

> ALTER TABLE dist
-> RENAME COLUMN ST_DISTANCE(POINT(x1,y1),POINT(x2,y2)) TO Values;

ERROR: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(POINT(x1,y1),POINT(x2,y2)) TO Values' at line 2

2 Answers 2

1

Besides that Values is a very bad choice for a column name, as are all reserved words

You can give the column a name by creation. like

CREATE TABLE dist (`Values` DECIMAL(12,11))
 SELECT ST_DISTANCE(POINT(x1,y1),POINT(x2,y2)) as 'values'
 FROM config;
4
  • Hi, I had already tried that. It adds another column "values" will NULL for each of the value. And the ST_DISTANCE column remains the same with same column name.
    – Titiksha
    Commented Jul 22, 2021 at 11:16
  • @Titiksha i changed myanswer after testing it with mysql 8.0.25
    – nbk
    Commented Jul 22, 2021 at 11:34
  • Thanks, this worked
    – Titiksha
    Commented Jul 22, 2021 at 11:57
  • Don't forget to accept the answer
    – nbk
    Commented Jul 22, 2021 at 12:00
0

Use backtics around the expression (which is the column name in the new table.)

ALTER TABLE dist
    RENAME COLUMN `ST_DISTANCE(POINT(x1,y1),POINT(x2,y2))` TO Values;

Note RENAME COLUMN was added in 8.0 and 10.5; if you have an older version, you must use the more verbose CHANGE COLUMN syntax.

(Oh, and be cautious about VALUES; it may be a reserved word.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.