Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hyphens in column names in MySQL DB

May be this question has been answered before but I couldn't find it.

I am using a 2/3 yr old MySQL database which has hyphens in its column names. When I try to use these names from my Java code, the names are broken at the hyphen (e.g. air_port becomes air) and thus are not found. I tried replacing hyphens to underscores in my code hoping that the DB might treat them equally but that doesn't work.

How can I escape the hyphen or how can I access these columns ? Could this be an issue of the character set being used ?

like image 855
euphoria83 Avatar asked May 19 '09 23:05

euphoria83


People also ask

Can you have dashes in SQL column names?

Under SQL-92 table and field names should not: Contain SQL special characters like the hyphen/dash/minus character.

Is hyphen allowed in SQL?

No, the hyphen is an operator, and you can't use that in the middle of an identifier.

How do you insert a hyphen in SQL?

To set a string with hyphen and numbers, you need to use single quotes. For example, 'Customer-1234-899', 'Customer-9383-901', etc.

Can we use special characters in column name in SQL?

Using special characters in column names can create a problem for the SQL queries that are used by Netcool/Impact. Netcool/Impact, in most cases, surrounds the column that contains the special characters with double quotation marks to avoid this issue. However, if the particular character is not listed in the impact.


1 Answers

enclose the names within `back-ticks`

like image 177
dar7yl Avatar answered Sep 19 '22 02:09

dar7yl