3

I've table named networks:

mysql> describe networks;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| range | varchar(45) | NO   |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+

I'm trying to INSERT value in this table with the following query:

INSERT INTO networks(range) VALUES("10.10.10.10/24");

However I get this error:

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 'range) VALUES("10.10.10.10/24")' at line 1

I tried playing with the quotes (changing them form " to ', and similar stuff), however it didn't work. Any ideas what may be wrong?

Sebastian Brosch
  • 42,106
  • 15
  • 72
  • 87
Deyan Georgiev
  • 343
  • 3
  • 15

2 Answers2

4

You can use the following INSERT:

INSERT INTO `networks` (`range`) VALUES ('10.10.10.10/24');

You can enclose table and column names into backticks to avoid conflicts with reserved words on MySQL. In your case RANGE is a reserved word and can't be used as a column or table name without backticks. A string value should be enclosed in single-quotes.

Note: You should avoid using reserved words like RANGE as column and table definition. You can find a list of all reserved words on the official docs.

Sebastian Brosch
  • 42,106
  • 15
  • 72
  • 87
3

Try Back Ticks:

INSERT INTO networks(`range`) VALUES('10.10.10.10/23');

This may help.

DineshDB
  • 5,998
  • 7
  • 33
  • 49
  • 1
    The same error: mysql> INSERT INTO networks(range) VALUES('10.10.10.10/23'); 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 'range ) VALUES('10.10.10.10/23')' at line 1 – Deyan Georgiev Nov 09 '17 at 11:08
  • As the error message clearly says, the problem is at or before `range`. Both single and double quotes can be equally used to enclose strings. – axiac Nov 09 '17 at 11:09
  • Yes, @iamsankalp89. You have to use `back ticks' instead of 'quotes' – DineshDB Nov 09 '17 at 11:32
  • Ok seems you all correct, I will delete it no issue thanks for help – – iamsankalp89 Nov 09 '17 at 11:34