Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a table field contain a hyphen?

I have a table in a MySQL table with a fieldname 'product', and want to rename it to 'ds-product'.

The CMS type system I am using uses the id of formfields as the name of the table field to insert into.

For most this works fine, but for a particular field it prepends 'ds-' to whatever ID I give it, so I must make the table field name match.

However, when trying to do a query I get the error that

Unknown column 'sales.ds' in 'field list'

Is there any way I can have a field called ds-product?

like image 489
Jacob Avatar asked Jul 02 '10 19:07

Jacob


People also ask

What does hyphen do in SQL?

PL/SQL supports two comment styles: single-line and multi-line. A double hyphen ( - - ) anywhere on a line (except within a character literal) turns the rest of the line into a comment.

Can varchar have hyphen?

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

Can table names have underscore?

Rule 1d (Special Characters) - For table names, underscores should not be used. The underscore character has a place in other object names but, not for tables. Using PascalCase for your table name allows for the upper-case letter to denote the first letter of a new word or name.

Can we use in column name in MySQL?

The desc is a MySQL reserved word, therefore you cannot use it. But, if you still want to set the column name as 'desc', you need to use backticks. The backtick notation is (` `).


2 Answers

Try putting brackets on the last part of your call on the table. in your case:

SELECT * FROM [TABLE-NAME]; 

just make sure to put the brackets on the table name only. not on the whole database it is located

SELECT * FROM some_database.anotherdatabase.[your-table]; 

P.S. works on columns, too.

I'm using Microsoft SQL Server Management.

like image 29
user5117649 Avatar answered Sep 23 '22 04:09

user5117649


Yes, you can use punctuation, white space, international characters, and SQL reserved words if you use delimited identifiers:

SELECT * FROM `my-table`; 

In MySQL, use the back-ticks. In standard SQL, use double-quotes.

Or if you use MySQL you can set the ANSI_QUOTES SQL mode:

SET SQL_MODE = ANSI_QUOTES; SELECT * FROM "my-table"; 
like image 194
Bill Karwin Avatar answered Sep 21 '22 04:09

Bill Karwin