Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table or column name cannot start with numeric?

Tags:

I tried to create table named 15909434_user with syntax like below:

CREATE TABLE 15909434_user ( ... ) 

It would produced error of course. Then, after I tried to have a bit research with google, I found a good article here that describe:

When you create an object in PostgreSQL, you give that object a name. Every table has a name, every column has a name, and so on. PostgreSQL uses a single data type to define all object names: the name type.

A value of type name is a string of 63 or fewer characters. A name must start with a letter or an underscore; the rest of the string can contain letters, digits, and underscores.

...

If you find that you need to create an object that does not meet these rules, you can enclose the name in double quotes. Wrapping a name in quotes creates a quoted identifier. For example, you could create a table whose name is "3.14159"—the double quotes are required, but are not actually a part of the name (that is, they are not stored and do not count against the 63-character limit). ...

Okay, now I know how to solve this by use this syntax (putting double quote on table name):

CREATE TABLE "15909434_user" ( ... ) 

You can create table or column name such as "15909434_user" and also user_15909434, but cannot create table or column name begin with numeric without use of double quotes.

So then, I am curious about the reason behind that (except it is a convention). Why this convention applied? Is it to avoid something like syntax limitation or other reason?

Thanks in advance for your attention!

like image 231
Wayan Wiprayoga Avatar asked Apr 10 '13 04:04

Wayan Wiprayoga


People also ask

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.

Can column name start with number in SQL?

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 column names be numbers?

Names in MySQL can start with a digit, but cannot be entirely digits — they cannot be confusable with an integer. So, no.

Can table name start with number in MySQL?

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.


2 Answers

It comes from the original sql standards, which through several layers of indirection eventually get to an identifier start block, which is one of several things, but primarily it is "a simple latin letter". There are other things too that can be used, but if you want to see all the details, go to http://en.wikipedia.org/wiki/SQL-92 and follow the links to the actual standard ( page 85 )

Having non numeric identifier introducers makes writing a parser to decode sql for execution easier and quicker, but a quoted form is fine too.


Edit: Why is it easier for the parser?

The problem for a parser is more in the SELECT-list clause than the FROM clause. The select-list is the list of expressions that are selected from the tables, and this is very flexible, allowing simple column names and numeric expressions. Consider the following:

SELECT 2e2 + 3.4 FROM ... 

If table names, and column names could start with numerics, is 2e2 a column name or a valid number (e format is typically permitted in numeric literals) and is 3.4 the table "3" and column "4" or is it the numeric value 3.4 ?

Having the rule that identifiers start with simple latin letters (and some other specific things) means that a parser that sees 2e2 can quickly discern this will be a numeric expression, same deal with 3.4

While it would be possible to devise a scheme to allow numeric leading characters, this might lead to even more obscure rules (opinion), so this rule is a nice solution. If you allowed digits first, then it would always need quoting, which is arguably not as 'clean'.


Disclaimer, I've simplified the above slightly, ignoring corelation names to keep it short. I'm not totally familiar with postgres, but have double checked the above answer against Oracle RDB documentation and sql spec

like image 165
rlb Avatar answered Oct 25 '22 05:10

rlb


I'd imagine it's to do with the grammar.

SELECT 24*DAY_NUMBER as X from MY_TABLE

is fine, but ambiguous if 24 was allowed as a column name.

Adding quotes means you're explicitly referring to an identifier not a constant. So in order to use it, you'd always have to escape it anyway.

like image 30
LoztInSpace Avatar answered Oct 25 '22 05:10

LoztInSpace