Do different databases use different name quote? - mysql

For example, mysql quote table name using
SELECT * FROM `table_name`;
notice the `
Does other database ever use different char to quote their table name

This use of quotes is called delimited identifiers. It's an important part of SQL because otherwise you can't use identifiers (e.g. table names and column names) that:
Include whitespace: "my table"
Include special characters and punctuation: "my-table"
Include international characters: "私のテーブル"
Are case-sensitive: "MyTable"
Match SQL keywords: "table"
The standard SQL language uses double-quotes for delimited identifiers:
SELECT * FROM "my table";
MySQL uses back-quotes by default. MySQL can use standard double-quotes:
SELECT * FROM `my table`;
SET SQL_MODE=ANSI_QUOTES;
SELECT * FROM "my table";
Microsoft SQL Server and Sybase uses brackets by default. They can both use standard double-quotes this way:
SELECT * FROM [my table];
SET QUOTED_IDENTIFIER ON;
SELECT * FROM "my table";
InterBase and Firebird need to set the SQL dialect to 3 to support delimited identifiers.
Most other brands of database use double-quotes correctly.

SQL Server uses [square brackets] or "double quotes" when QUOTED_IDENTIFIER option is ON.
I believe double quotes are in the SQL-92 standard.

Succinctly, yes.
The SQL standard uses double quotes around the name to indicate a 'delimited identifier'.
Informix by default uses single and double quotes interchangeably to indicate character strings. However, by setting the environment variable DELIMIDENT you can turn on the SQL standard behaviour - single quotes around strings and double quotes around delimited identifiers.
Other people have listed other behaviours for other DBMS; I don't need to repeat those.

Related

How do you write spaces in a Column name during table creation in mySQL? In MSSQL, I can write [Column Name]. I have found no way for this in mySQL

How do you write spaces in a column name during table creation in mySQL? In MSSQL, I can write [Column Name] and create columns with spaces using brackets. . I have found no way for this in mySQL? Is it possible? Not that I want to do this, but I must due to third party software.
you must put the name in backticks like this:
CREATE TABLE testtable (
`W i t h S p a c e s` VARCHAR(33)
);
Yes, any SQL implementation should support delimited identifiers. This allows you to use whitespace, punctuation, international characters, or SQL reserved words in your table names and column names.
https://dev.mysql.com/doc/refman/5.7/en/identifiers.html says:
The identifier quote character is the backtick (`):
mysql> SELECT * FROM `select` WHERE `select`.id > 100;
If the ANSI_QUOTES SQL mode is enabled, it is also permissible to
quote identifiers within double quotation marks:
mysql> CREATE TABLE "test" (col INT);
ERROR 1064: You have an error in your SQL syntax...
mysql> SET sql_mode='ANSI_QUOTES';
mysql> CREATE TABLE "test" (col INT);
Note that Microsoft SQL Server also supports double-quotes as identifier delimiters when you enable the QUOTED_IDENTIFERS option. See https://technet.microsoft.com/en-us/library/ms176027(v=sql.105).aspx
Standard SQL defines double-quotes as the only identifier delimiters. Unfortunately, both Microsoft and MySQL didn't get that memo.
I tried using apostrophes and didn't work. Back ticks Thanks for the info.

Escaping a forward slash in an SQL name? It can be "escaped", but SQL believes it to be multiple columns

The last person in my job has flooded column names with special characters such as (?,!, and /), as well as used many reserved keywords for column names (more often than not, timestamp or user is used).
Normally, I step around this by using double quotes or brackets to escape the SQL object. A subset of the full list of columns are below:
DriverID,
Department,
Odometer,
MerchantState,
MerchantCity,
gallons/Units,
timestamp,
tax
Inside my query, I wrap the two columns in question (gallons/units and timestamp) inside double quotes. Timestamp because it's a reserved keyword, and Gallons/units, because without the quotes, SQL reads the query, stops at the slash, and tells me "Gallons" is not a column inside the table.
If I do wrap double quotes around the column name, SQL returns a different error: "Operand should contain 1 column(s)".
I've tried every variant (only capturing the slash in quotes, quoting both, using brackets, mixing brackets and quotes, etc. but with to no avail).
Is there anything I can do to fix this query short of renaming the column name and changing the associated code in the program that pulls from it? (the really tedious task I'm trying to avoid).
In SQL Server, identifiers can be delimited using square brackets, e.g.
SELECT [gallons/units] ...
In MySQL, identifiers can be delimited using backticks, e.g.
SELECT `gallons/units` ...
(NOTE: If MySQL SQL_MODE includes ANSI_QUOTES, then double quotes are treated as delimiters for identifiers, similar to the way Oracle handles double quotes; absent that setting, double quotes are handled as delimiters for string literals. With ANSI_QUOTES included SQL_MODE, "gallons/units" will be interpreted as an identifier (column name). Without ANSI_QUOTES, MySQL will see it as a string literal, as if it were enclosed in single quotes.)
FOLLOWUP:
As far as an error "operand should contain only 1 column(s)", that's usually a problem with query semantics, not an issue with escaping identifiers.
A subquery in the SELECT list can return only a single expression, for example, this would throw an error:
Query: SELECT 'foo' , ( SELECT 'fee' AS fee, 'fi' AS fi )
Error Code: 1241
Operand should contain 1 column(s)
You can try backticks instead of double quotes
`gallons/units`
There are a couple of options. First, have you tried using %/ to escape the slash?
Example: "select * from 'gallons%/units';"
Second one I've found, which may or may not be helpful, is to provide an escape character definition, such as
http://blogs.msdn.com/b/zainala/archive/2008/08/17/using-and-escape-clause-in-sql-server-like-query.aspx
select * from MyTable where Description like '|[summary|]%' escape '|'
In your case
select * from 'gallons|/units' escape '|'
You indicate both mysql and sql-server in your tags, so I'm not sure which server support I should be looking for exactly.

Slick - MySQL Syntax Error

I'm using the Play Framework (2.3.1) together with Slick (play-slick version 0.8.0-M1) and a MySQL Database (5.5.28).
One of my queries results in a MySQLSyntaxErrorException:
Preparing statement: select x2."id", x2."course_id", x2."trainee_id", x2."transaction_id" from "trainee_grouptraining_GroupBooking" x2 where x2."course_id" = 1
The problem appears to be with the double quotation marks, since other queries work just fine and they use single quotation marks like the following:
Preparing statement: select x2.`id`, x2.`courseLanguage`, x2.`date`, x2.`description`, x2.`duration`, x2.`kind`, x2.`maxParticipants`, x2.`name`, x2.`courseType_id`, x2.`trainer_id` from `Course` x2 where x2.`id` = 1
What can i do about this?
I guess you are importing
scala.slick.driver.JdbcDriver.simple._
You should import
scala.slick.driver.MySQLDriver.simple._
instead.
MySQL is using per default backticks to quote identifiers. You could use the SQL Mode ANSI_QUOTES to enable double 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.
or simply use backticks.

SQL update statement with default as column name

I've got an SQL code to update values in a column.
I need to find one query to work for MS SQL and MySQL.
The main problem is that the columns which I'm using are Default and Type which are saved names in the SQL and therefore it doesn't work with the normal update statements.
I've found the following solutions, but I would like to make one query for both -
--Clearing Data logs Defualt MS SQL
UPDATE queries
SET [Default] = 0
FROM queries
WHERE [Type] = 4
--Clearing Data logs Defualt MySQL
UPDATE queries q
SET q.Default = 0
WHERE q.TYPE = 4
Thanks a lot for the help!
You've got to enable ANSI quotes for both servers. If you do that you could use double quotes to quote your identifiers.
For MySQL:
SET sql_mode = '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.
For MS SQL Server.
SET QUOTED_IDENTIFIER ON
Causes SQL Server to follow the ISO rules regarding quotation mark
delimiting identifiers and literal strings. Identifiers delimited by
double quotation marks can be either Transact-SQL reserved keywords or
can contain characters not generally allowed by the Transact-SQL
syntax rules for identifiers.
Now you can write the single query that works for both, MS SQL Server and MySQL:
UPDATE queries q
SET q."Default" = 0
WHERE q."TYPE" = 4
And before I forget it: best way out of such problems is to avoid reserved words as identifiers, see solution 1. You've got to avoid reserved words of all involved worlds (T-SQL and the sql dialect of MySQL).

Error in MySQL Query (Banned Word?)

I have an MySQL query, which returns an error message. I think it could be due to the word "out". Normally, I would just change the field name but I am working on some software that I am not used to and I don't know how much of a change that would be. So, I want to be sure if I have to.
Here is the query:
SELECT * FROM probid_bids WHERE auctionid=73 AND out=0 AND invalid=0
Here the error message:
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 'out=0 AND invalid=0' at line 1
OUT is indeed a reserved word. You can encase the column names in backticks to quote the names, and thus avoid this problem, like so:
SELECT * FROM probid_bids WHERE `auctionid`=73 AND `out`=0 AND `invalid`=0
OUT is a reserved word (it is used to specify the type of parameters -- IN, OUT, INOUT -- when creating procedures). Try enclosing it inside backticks (`).
The rules regarding how and when to quote the identifiers (table names, column names, etc) are described here.
Note: certain MySQL configurations allow you to use double quotes as well but this should be avoided; stick with using backticks to quote identifiers and single quotes to quote strings.
Escape the keys:
SELECT * FROM `probid_bids` WHERE `auctionid`=73 AND `out`=0 AND `invalid`=0