mysql - 何时在MySQL中使用单引号,双引号和反引号

标签 mysql sql quotes

我正在尝试学习编写查询的最佳方法。我也了解保持一致的重要性。到现在为止,我在没有任何实际考虑的情况下随机使用单引号,双引号和反引号。

例:

$query = 'INSERT INTO table (id, col1, col2) VALUES (NULL, val1, val2)';


另外,在上面的示例中,请考虑tablecol1val1等可能是变量。

这是什么标准?你是做什么?

我已经在这里阅读了大约20分钟的类似问题的答案,但是似乎没有确切的答案。

最佳答案

反引号用于表和列标识符,但仅在标识符为MySQL reserved keyword或标识符包含空格字符或超出限制集的字符时才需要使用反引号(请参见下文)。通常建议避免使用保留关键字在可能的情况下用作列或表的标识符,避免引用问题。

单引号应用于VALUES()列表中的字符串值。 MySQL还为字符串值支持双引号,但是单引号已被其他RDBMS广泛接受,因此使用单引号而不是双引号是一个好习惯。

MySQL还希望DATEDATETIME文字值被单引号括起来,如'2001-01-01 00:00:00'这样的字符串。有关更多详细信息,请查阅the Date and Time Literals文档,尤其是在日期字符串中使用连字符-作为分段定界符的替代方法。

因此,使用您的示例,我将对PHP字符串加双引号,并对值'val1', 'val2'使用单引号。 NULL是MySQL关键字,是一个特殊的(非)值,因此未加引号。

这些表或列标识符都不是保留字,也不使用需要引号的字符,但是无论如何我都用反引号将它们引号(稍后再说……)。

RDBMS的本机函数(例如,MySQL中的NOW())不应该被引用,尽管它们的参数要遵循已经提到的相同的字符串或标识符引用规则。

Backtick (`)
table & column ───────┬─────┬──┬──┬──┬────┬──┬────┬──┬────┬──┬───────┐
                      ↓     ↓  ↓  ↓  ↓    ↓  ↓    ↓  ↓    ↓  ↓       ↓
$query = "INSERT INTO `table` (`id`, `col1`, `col2`, `date`, `updated`) 
                       VALUES (NULL, 'val1', 'val2', '2001-01-01', NOW())";
                               ↑↑↑↑  ↑    ↑  ↑    ↑  ↑          ↑  ↑↑↑↑↑ 
Unquoted keyword          ─────┴┴┴┘  │    │  │    │  │          │  │││││
Single-quoted (') strings ───────────┴────┴──┴────┘  │          │  │││││
Single-quoted (') DATE    ───────────────────────────┴──────────┘  │││││
Unquoted function         ─────────────────────────────────────────┴┴┴┴┘    

Variable interpolation

The quoting patterns for variables do not change, although if you intend to interpolate the variables directly in a string, it must be double-quoted in PHP. Just make sure that you have properly escaped the variables for use in SQL. (It is recommended to use an API supporting prepared statements instead, as protection against SQL injection).

// Same thing with some variable replacements
// Here, a variable table name $table is backtick-quoted, and variables
// in the VALUES list are single-quoted 
$query = "INSERT INTO `$table` (`id`, `col1`, `col2`, `date`) VALUES (NULL, '$val1', '$val2', '$date')";

Prepared statements

When working with prepared statements, consult the documentation to determine whether or not the statement's placeholders must be quoted. The most popular APIs available in PHP, PDO and MySQLi, expect unquoted placeholders, as do most prepared statement APIs in other languages:

// PDO example with named parameters, unquoted
$query = "INSERT INTO `table` (`id`, `col1`, `col2`, `date`) VALUES (:id, :col1, :col2, :date)";

// MySQLi example with ? parameters, unquoted
$query = "INSERT INTO `table` (`id`, `col1`, `col2`, `date`) VALUES (?, ?, ?, ?)";


要求在标识符中使用引号引起来的字符:

According to MySQL documentation,您无需使用以下字符集引用(反引号)标识符:


  ASCII:[0-9,a-z,A-Z$_](基本拉丁字母,数字0-9,美元,下划线)


您可以使用超出设置范围的字符作为表或列标识符,例如,包括空格,但是必须将其引号(反引号)。

关于mysql - 何时在MySQL中使用单引号,双引号和反引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58826198/

相关文章:

mysql - 在 Solr(或其他分面搜索引擎)中搜索/导航,正确的方法是什么?

mysql - 获取项目的最后状态

sql - 找到保存外键的记录

bash - 在 bash 中使用其他引用中的 "$@"

mysql - MySQL 中何时使用单引号、双引号和反引号

mysql - MySQL 中何时使用单引号、双引号和反引号

mysql - 计算给定列中具有字符 A、B、C 的条目数

c# - 编程附加组件,访问 HRMS 后端

SQL 在连接时获取多个值

SQL Join 两个查询结果