Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a number be used to name a MySQL table column?

Tags:

mysql

ddl

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?

like image 780
Daniel Hunter Avatar asked Nov 02 '11 03:11

Daniel Hunter


People also ask

Can MySQL table name be a number?

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.

Can column name have numbers?

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 (`).

Can SQL column names start with a number?

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 (_).

Can a table name start with number?

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.


1 Answers

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' 
like image 111
Matthew Avatar answered Sep 28 '22 09:09

Matthew