Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql double-quoted table names

I'm doing a mysql query like:

Select * from "User";

and it returns:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"User"' at line 1

The error has something to do with the double-quotes ", can I keep the select statement as is, and make mysql cope with the double quotes?

like image 500
Mohamed Wagdy Khorshid Avatar asked Dec 14 '12 19:12

Mohamed Wagdy Khorshid


People also ask

Can I use double quotes in MySQL?

Double quotes are supported by MySQL for string values as well, but single quotes are more widely accepted by other RDBMS, so it is a good habit to use single quotes instead of double.

What are the rules for naming a table in MySQL?

By default, MySQL encloses column names and table names in quotation marks. Table names can use any character that is allowed in a file name except for a period or a forward slash. Table names must be 32 characters or less because SAS does not truncate a longer name.

Why does MySQL use backticks?

Backticks ( ` ) are used to indicate database, table, and column names. Unless you're using reserved or conflicting words for table and database names, you'll not need to use them. Quotes ( ' or " ) are used to delimit strings, and differentiate them from column names.

How do you escape double quotes in SQL?

Use two single quotes to escape them in the sql statement. The double quotes should not be a problem: SELECT 'How is my son''s school helping him learn?


1 Answers

Taken from this post:

SET GLOBAL SQL_MODE=ANSI_QUOTES;

Personally when I tested, I had to do it like this:

SET SQL_MODE=ANSI_QUOTES;

I don't think there's any other way.

http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html#sqlmode_ansi_quotes

ANSI_QUOTES

Treat “"” as an identifier quote character (like the “`” quote character) and not as a string quote character. You can still use “`” to quote identifiers with this mode enabled. With ANSI_QUOTES enabled, you cannot use double quotation marks to quote literal strings, because it is interpreted as an identifier.

like image 171
Wesley Murch Avatar answered Oct 04 '22 20:10

Wesley Murch