tags:

views:

464

answers:

4

In DB2, you can name a column ORDER and write SQL like

SELECT ORDER FROM tblWHATEVER ORDER BY ORDER

without even needing to put any special characters around the column name. This is causing me pain that I won't get into, but my question is: why do databases allow the use of SQL keywords for object names? Surely it would make more sense to just not allow this?

+2  A: 

Many SQL parsers (expecially DB2/z, which I use) are smarter than some of the regular parsers which sometimes separate lexical and semantic analysis totally (this separation is mostly a good thing).

The SQL parsers can figure out based on context whether a keyword is valid or should be treated as an identifier.

Hence you can get columns called ORDER or GROUP or DATE (that's a particularly common one).

It does annoy me with some of the syntax coloring editors when they brand an identifier with the keyword color. Their parsers aren't as 'smart' as the ones in DB2.

paxdiablo
+1  A: 

Because object names are ... names. All database systems let you use quoted names to stop you from running into trouble.

If you are running into issues, the fault lies not with the practice of permitting object names to be names, but with faulty implementations, or with faulty code libraries which don't automatically quote everything or cannot be made to quote names as-needed.

Justice
Agreed, but: code that doesn't automatically quote/bracket column names is incredibly common. When I design a table, some cosmic force always stops me from naming a column "ORDER" or "DATE" or "WHY THE SPACE" even though I could. It's sure to break something if I do that.
MusiGenesis
@MusiGenesis: What do you call your date and time columns?
paxdiablo
I always use quoted names for all table/column/etc names in my own projects, and have no compunctions about naming things whatever I think makes the most sense.
Justice
@Pax: I would be pretty annoyed if I inherited a database with column names like DATE and TIME - what does that mean? When it was created? When it was modified? When it expires? I'd use meaningful names like CREATEDDATE, LASTMODIFIEDTIME etc.
MusiGenesis
@Justice: one peculiar bit about this project is that our developers have to write SQL that will run on a SQL Server, Oracle or DB2 version of the database. Since all three quote column names differently ([], "", and '' or nothing), it's not a simple problem.
MusiGenesis
@MusiGenesis: So you are a) hand-coding SQL instead of generating it and b) doing so in multiple dialects. That is a very interesting circumstance. Any issues you are running into result from your circumstance, not from database systems being convenient.
Justice
@MusiGenesis: What if you inherited a table named User or Login? What if you had naming conventions where the PK's are named the same as the table, and FK's the same as the ref'd table? 'select l.Login from Login l where l.User is null'? Some DB's will barf on that. I just always quote the names.
Justice
+3  A: 

I largely agree with the sentiment that keywords shouldn't be allowed as identifiers. Most modern computing languages have 20 or maybe 30 keywords, in which case imposing a moratorium on their use as identifiers is entirely reasonable. Unfortunately, SQL comes from the old COBOL school of languages ("computing languages should be as similar to English as possible"). Hence, SQL (like COBOL) has several hundred keywords.

I don't recall if the SQL standard says anything about whether reserved words must be permitted as identifiers, but given the extensive (excessive!) vocabulary it's unsurprising that several SQL implementations permit it.

Having said that, using keywords as identifiers isn't half as silly as the whole concept of quoted identifiers in SQL (and these aren't DB2 specific). Permitting case sensitive identifiers is one thing, but quoted identifiers permit all sorts of nonsense including spaces, diacriticals and in some implementations (yes, including DB2), control characters! Try the following for example:

CREATE TABLE "My
Tablé" ( A INTEGER NOT NULL );

Yes, that's a line break in the middle of an identifier along with an e-acute at the end... (which leads to interesting speculation on what encoding is used for database meta-data and hence whether a non-Unicode database would permit, say, a table definition containing Japanese column names).

A: 

Interestingly you can use keywords as field names in SqlServer as well. The only differenc eis that you would need to use parenthesis with the name of the field

so you can do something like

create table [order](
id int,
[order] varchar(50) )

and then :)

select 
    [order] 
from 
    [order]
order by [order]

That is of course a bit extreme example but at least with the use of parenthesis you can see that [order] is not a keyword.

The reason I would see people using names already reserved by keywords is when there is a direct mapping between column names, or names of the tables and the data presentation. You can call that being lazy or convenient.

kristof