Question

Java DB / Derby - white space in table (column) name - how to escape? In MySQL database it is escaped simply by `` (grave accent), but if I trying to execute query (Java DB) like:

CREATE TABLE `etc etc` (ID INT PRIMARY KEY, Title VARCHAR(12))

I get the error:

java.sql.SQLSyntaxErrorException: Lexical error at line 1, column 14. Encountered: "`" (96), after : "".

Is there any solution?

EDITED:

Thanks for comprehensive answers indeed. I found interesting thing (Java DB):

it was OK to create table like this:

CREATE TABLE "etc etc" (ID INT PRIMARY KEY, Title VARCHAR(12))

and later to get the results:

SELECT * FROM "etc etc"

But if I create the table like this (without white space and quotes):

CREATE TABLE etc_etc (ID INT PRIMARY KEY, Title VARCHAR(12))

and later get the results like this (with double quotes, just for example, they don't really needed):

SELECT * FROM "etc_etc"

I get the error:

java.sql.SQLSyntaxErrorException: Table/View 'etc_etc' does not exist.

So my conclusion is that if quotes were used while creating the table (it is not important if the name contains white space or not), later, while getting the results, quotes also should be used. And vice versa (int this case of course without white space).

I agree it is better not to use white space, instead _ character. But I liked on MySQL those ` grave accent characters...

Was it helpful?

Solution

The SQL standard requires double quotes to quote identifiers that are either reserved words or use characters otherwise not permitted in an identifier. Note that MySQL simply ignores the standard by using the dreaded backtick ` - but it could (and in my opinion should) be configured to also allow the standard double quotes (like all other DBMS do).

So your statement should look like this:

CREATE TABLE "etc etc" (ID INT PRIMARY KEY, Title VARCHAR(12));

Note that the use of "quoted identifiers" (or "delimited identifiers") also makes the name case sensitive. "etc etc" is a different table name than "Etc Etc". So after creating the table like above, you always have to write the name in lowercase.

The best thing is to completely avoid quoted identifiers. So in your case you should probably settle for etc_etc as the name using an underscore to visually separate the two words.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top