I have a table that has column names like 25, 50, 100, etc..
When trying to update the table I get an error, no matter how I do it
UPDATE table SET '25'='100' WHERE id = '1'
I have tried quoting and backticking every which way but without success.
The error is always along the lines of:
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 ''25'=100 WHERE id=1' at line 1
If I change the column name to twentyfive
- I don't have a problem, but that's not what I want. Is it possible to use a number as a column name?
The answer is yes, as given by karim79, as long as you take care to quote the table names. You could of course use a prefix with a numer, eg. mytable1, mytable2, ... ; that would work without quoting.
Column names can contain any valid characters (for example, spaces). If column names contain any characters except letters, numbers, and underscores, the name must be delimited by enclosing it in back quotes (`).
The rules for naming database objects (such as tables, columns, views, and database procedures) are as follows: Names can contain only alphanumeric characters and must begin with an alphabetic character or an underscore (_).
Table names shouldn't start with a number. They fall into the category of identifiers which , per Books Online, must conform to the following: The rules for the format of regular identifiers depend on the database compatibility level.
From the docs:
Identifiers may begin with a digit but unless quoted may not consist solely of digits.
Which means you must quote it with back ticks like `25`:
UPDATE table SET `25`='100' WHERE id='1'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With