Mysql single and double quotation marks and backquotes

反引号通常用于表示安全,identifier并且不会意外地使用保留关键字。

例如:

Use `database`;
这里的反引号将帮助服务器理解database实际上是数据库的名称,而不是数据库标识符。

可以对表名和字段名执行相同的操作。如果用反引号包装数据库标识符,这是一个非常好的习惯。
MySQL中有两种类型的引号:

' 用于包含字符串文字

` 用于包含表和列名称等标识符

然后有"一个特例。根据MySQL服务器的不同,它可以一次用于上述目的之一sql_mode:

默认情况下,该"字符可用于包含字符串文字'

在ANSI_QUOTES模式中,该"字符可用于包围标识符`

以下查询将根据SQL模式生成不同的结果(或错误):
SELECT "column" FROM table WHERE foo = "bar"
ANSI_QUOTES已禁用
查询将选择"column"列foo等于string 的字符串文字"bar"

ANSI_QUOTES已启用
查询将选择column列foo等于列的列bar

什么时候用
我建议您避免使用,"以便您的代码独立于SQL模式

总是引用标识符,因为这是一个很好的做法(关于SO的很多问题讨论这个)

Reprinted:

1 Single quotation marks, double quotation marks When
quoting string constants in MySQL, you need to use a pair of English single quotation marks "'" or English double quotation marks "" "to enclose the string constants. For example:

'an apple'
 
"a book"
1.1 Single quotation marks
should generally be English single quotation marks. If single quotation marks need to be included in the string, in addition to using escaping methods, we can use a pair of double quotation marks to include the string, such characters The single quotes in the string are treated as ordinary characters and do not need special processing.

E.g. Johnson's mother

select "Johnson's mother",'Johnson''s mother','Johnson\'s mother';


1.2 Double quotation
marks correspond to single quotation marks. When double quotation marks need to be included in the string quoted by English double quotation marks, in addition to using escaping, we can use a pair of single quotation marks to include the string, so that the double quotation marks in the string It is treated as a normal character without special processing.

例如  He said: "Go away" 

select 'He said: "Go away"' ,"He said: ""Go away""" ,"He said: \"Go away\"" ;


2 Backquote ( where to add the backquote: library name, table name, field name (column name) )
backquote, usually under the ESC key. It is a symbol introduced to distinguish between reserved words and ordinary characters in MYSQL.

MySQL uses a pair of back-tick "` "to enclose the identifier. Generally, it is not necessary. It must be used only if there is a keyword conflict in the identifier or the wording of the identifier may be ambiguous. E.g:

create table t1 (id int primary key ,desc varchar(10)); - This sentence will report an error
 
create table t1 (id int primary key,`desc` varchar(10)); - This sentence runs successfully.
In addition, if SQL The server mode includes the ANSI_QUOTES mode option, and you can also use double quotes instead of back-ticks to enclose identifiers:

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); Query OK, 
0 rows affected (0.00 sec)
reported an error before, after setting the SQL server mode to include ANSI_QUOTES mode, the operation was successful.

for example:

SELECT `select` FROM `test` WHERE select='field value'
In the test table, there is a select field. If you don't use the backquote, MYSQL will treat select as a reserved word and cause an error. Therefore, there is a MYSQL reserved word as a field , Must be distinguished by adding backquotes.
In addition: When creating a table, the table name and library name are usually added with backquotes to ensure the execution of the statement. Backquote `, the symbol to the left of the number 1.
Reserved words cannot be used for table names, such as desc. In this case, you need to add backticks to distinguish them, but you can omit the backticks when using table names.

create table desc report error
create table `desc` success
create table `test` success
drop table test success
reserved words cannot be used for field names, such as desc, you also need to add backquotes at this time, and also add backquotes when using inserts, etc. .

create table `test`(`desc` varchar(255)) succeeded
insert into test(desc) values('fxf') failed
insert into test(`desc`) values('fxf') succeeded

Guess you like

Origin blog.csdn.net/weixin_45433031/article/details/115274019