Can a backticked identifier have a backtick in it? - mysql

Is it possible to escape a SQL identifier in either MySQL or BigQuery or any other RDBMS that allows quoting of identifiers with the ` character? For example:
select 1 as `select`
Works, but then How would I add a literal backtick to it, or is that just not allowed?
select 1 as `sel\`ect`

Yes. Use a doubled backtick, eg:
create table `my``table` (`my``id` int);
This is similar syntax for including quotes in text, eg 'O''Leary'
See live demo of
create table `my``table` (`my``id` int);
insert into `my``table` (`my``id`) values (1);
select * from `my``table`;
outputs:
my`id
1
By the way, the documentation says any UTF-8 character (except U+0000) is allowed in a back tick quoted identifier.

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.

phpmyadmin sql apostrophe not working [duplicate]

This question already has answers here:
character for single quote
(1 answer)
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 8 years ago.
hey guys was hoping you could help me out,
Not sure if I always had this problem or if its new, in phpmyadmin in the sql tab, the apostrophe it recognizes is different from what i type, for example,
when i type, it is
SELECT * FROM 'table'
this gives me an error, so instead I have to like copy/paste the inverted commas of some prebuilt query so that it looks like
SELECT * FROM `table`
see how the apostrophes are different? any way I can fix this?
also, i have seen many queries on the web, and i think even queries i call from php dont require table names to have apostrophes. But when write it in phpmyadmin, I can do queries without table names having apostrophes?
thanks in advance.
In MYSQL, table is a reserved keyword. If you want to use reserved keywords in mysql in query, you have to enclose them in backtick(`).
As table is reserved keyword you query should be
SELECT * FROM `table`
Regarding single quote ('), in mysql, it represents string value.
SELECT *, 'table' FROM `table`;
Demo
You should only need to quote table names when they conflict with a reserved word.
Also:
` = Grave accent, or (because someone needed to invent a word) backtick
' = Apostrophe, or straight single quote
You dont need apostrophe on table name.
You should use ` in cases that your table/field name is a reserve word eg:
SELECT `distinct`, myfields FROM mytable
note that distinct is an sql command so you need to put the `.
SELECT * FROM `table`
table here should be inside `.
There are two different characters, the backtick and the single quote. Table and column names can be surrounded by the backtick, strings can be surrounded by quotes. There is nothign to fix :D

error in mysql statement

I cannot figure this one out. All the variables are ok. Printed out the sql statement before executing in php... This is the statement exactly as it is sent to be ran by php
INSERT INTO 'images' ('filename', 'creator', 'date', 'notes') VALUES ('cat.sdf', 'michaelamici', '2002-07-05', 'SDfdddfdffddffdfgs')
Thank You!
You're enclosing the table/field names in single-quotes. You need to do it with back-ticks (or nothing, depending on the name).
INSERT INTO `images` (`filename`, `creator`, `date`, `notes`) VALUES ('cat.sdf', 'michaelamici', '2002-07-05', 'SDfdddfdffddffdfgs')
Just in case you're interested, a list of reserved names (which must be quoted if used as a table or column name) can be found here: http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html.
Also, to find out what characters may be included in an unquoted name and which can appear only as part of a quoted name, see here: http://dev.mysql.com/doc/refman/5.5/en/identifiers.html.
N.B. the relevant MySQL version.
Your table and field names should either be in backquotes (`) or unquoted.
INSERT INTO `images` (`filename`, `creator`, `date`, `notes`) VALUES
('cat.sdf', 'michaelamici', '2002-07-05', 'SDfdddfdffddffdfgs');
Try editing single quotes to backquotes for table name and field names

Do different databases use different name quote?

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.