Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL issue with column name "range" [duplicate]

When I try to run this script I am getting an error at "range" if I remove or change the name I can run the script however the application need this column to store data. Any idea how to insert this into MySQL?

Error Code: 1064. 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 '"range" CHAR(5) NOT NULL, range_max_value NUMERIC(18,3)' at line 22

CREATE TABLE My_table (
  chart_id                INTEGER NOT NULL,
  u_range                 CHAR(5),
  l_range                 CHAR(5),
  "range"               CHAR(5) NOT NULL,
  range_max_val         NUMERIC(18,3),
  range_min_val         NUMERIC(18,3),
  PRIMARY KEY (chart_id)
);
like image 253
DoubleA9281 Avatar asked Feb 17 '26 13:02

DoubleA9281


2 Answers

Range is a reserved keyword that needs to be escaped with backticks.

CREATE TABLE My_table (
  chart_id                INTEGER NOT NULL,
  u_range                 CHAR(5),
  l_range                 CHAR(5),
  `range`               CHAR(5) NOT NULL,
  range_max_val         NUMERIC(18,3),
  range_min_val         NUMERIC(18,3),
  PRIMARY KEY (chart_id)
);
like image 187
ceejayoz Avatar answered Feb 19 '26 04:02

ceejayoz


You should take a look at the reserved words list. Range is one of those words. You need to escape it with tick marks:

CREATE TABLE My_table (
  chart_id                INTEGER NOT NULL,
  u_range                 CHAR(5),
  l_range                 CHAR(5),
  `range`               CHAR(5) NOT NULL,
  range_max_val         NUMERIC(18,3),
  range_min_val         NUMERIC(18,3),
  PRIMARY KEY (chart_id)
);
like image 22
Kermit Avatar answered Feb 19 '26 02:02

Kermit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!